Add post and comment to Chatter
This task use java to add post and comment to Chatter
Add post and comment to Chatter
- Create Chatter class as following
- Call postUserFeed() method and postComment() method as following
Call postUserFeed() method and postComment() method
String username = "";
String password = "";
ConnectorConfig cc = new ConnectorConfig();
cc.setAuthEndpoint("https://login.salesforce.com/services/Soap/u/19.0");
cc.setServiceEndpoint("https://login.salesforce.com/services/Soap/u/19.0");
cc.setManualLogin(true);
PartnerConnection conn = Connector.newConnection(cc);
LoginResult loginResult = conn.login(username, password);
conn.setSessionHeader(loginResult.getSessionId());
String serverUrl = loginResult.getServerUrl();
cc.setServiceEndpoint(serverUrl);
Chatter chatter = new Chatter(loginResult);
Map<String, String> data = null;
String postBody = "This is post!";
data = new HashMap<String, String>();
data.put("text", postBody);
String feedItemId = chatter.postUserFeed(data);
logger.info("Feed Item Id: " + feedItemId);
String commentBody = "This is comment!";
data = new HashMap<String, String>();
data.put("text", commentBody);
String commentId = chatter.postComment(feedItemId, data);
logger.info("Comment Id: " + commentId);
Chatter class
public class Chatter {
private static Logger logger = Logger.getLogger(Chatter.class);
private String token = "";
private String instance = "";
public Chatter(String token, String instance) {
this.token = token;
this.instance = instance;
}
public Chatter(LoginResult loginResult) {
this.token = getToken(loginResult);
this.instance = getInstance(loginResult);
}
public String postUserFeed(Map<String, String> data) throws Exception {
String feedItemId = "";
String html = request(token, "post", data, "https://" + instance + ".salesforce.com/services/data/v24.0/chatter/feeds/news/me/feed-items");
JSONObject json = (JSONObject)JSONSerializer.toJSON(html);
feedItemId = json.getString("id");
return feedItemId;
}
public String postRecordFeed(String recordId, Map<String, String> data) throws Exception {
String feedItemId = "";
String html = request(token, "post", data, "https://" + instance + ".salesforce.com/services/data/v24.0/chatter/feeds/record/" + recordId + "/feed-items");
JSONObject json = (JSONObject)JSONSerializer.toJSON(html);
feedItemId = json.getString("id");
return feedItemId;
}
public String postComment(String feedItemId, Map<String, String> data) throws Exception {
String commentId = "";
String html = request(token, "post", data, "https://" + instance + ".salesforce.com/services/data/v24.0/chatter/feed-items/" + feedItemId + "/comments");
JSONObject json = (JSONObject)JSONSerializer.toJSON(html);
commentId = json.getString("id");
return commentId;
}
public static String getToken(LoginResult loginResult) {
return loginResult.getSessionId();
}
public static String getInstance(LoginResult loginResult) {
String url = loginResult.getServerUrl();
String pat1 = "https://";
String pat2 = "-api.salesforce.com";
int pos1 = url.indexOf(pat1);
int pos2 = url.indexOf(pat2);
String instance = "ap1";
if (pos1 >= 0 && pos2 >= 0 && pos1 + pat1.length() < pos2) {
instance = url.substring(pos1 + pat1.length(), pos2);
}
return instance;
}
public static String request(String token, String method, Map<String, String> data, String link) throws Exception {
URL url = new URL(link);
URLConnection conn = url.openConnection();
HttpURLConnection http = (HttpURLConnection)conn;
http.setRequestMethod(method.toUpperCase());
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Authorization", "OAuth " + token);
String output = "";
for (String key : data.keySet()) {
if (output.length() > 0) output += "&";
output += key + "=" + URLEncoder.encode(data.get(key), "UTF-8");
}
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
out.write(output);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String response = "";
String line = null;
while ((line = in.readLine()) != null) {
if (response.length() > 0) {
response += "\r\n";
}
response += line;
}
in.close();
return response;
}
}