Skip to content

Instantly share code, notes, and snippets.

@arun12209
Last active November 30, 2019 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arun12209/1fbde5ac85c104a90837e16472f347f4 to your computer and use it in GitHub Desktop.
Save arun12209/1fbde5ac85c104a90837e16472f347f4 to your computer and use it in GitHub Desktop.
ChatterFeedTranslateCntrl
public class ChatterFeedTranslateCntrl {
public static Boolean isFirstTime = true;
@Future(callout=true)
public static void translateFeed(Map<String,String> feedMap) {
String feedBody = feedMap.values()[0].replaceAll('<[/a-zAZ0-9]*>','');
feedBody = feedBody.replace(' ','%20');
system.debug('feedBody = '+feedBody);
Set<String> feedItemIds = feedMap.keySet();
List<FeedItem> fItemList = [select Id,Body from FeedItem where id IN:feedItemIds];
system.debug('fItemList: ' +fItemList);
HttpRequest reqest = new HttpRequest();
String APIKey = 'trnsl.1.1.20191130T125129Z.74852570ba317630.62b74fb91590782bc383c79786987364XXXXXXXXXX'; //APIKey
String langTranslation = 'en-hi';//english into Hindi
String endPointURL = 'https://translate.yandex.net/api/v1.5/tr.json/translate?key='+APIKey+'&text='+feedBody+'&lang='+langTranslation+'&[format=plain';
reqest.setEndpoint(endPointURL);
reqest.setMethod('GET');
reqest.setHeader('Accept','application/json');
Http h = new Http();
HTTPResponse response = h.send(reqest);
system.debug('response: '+response.getBody());
string jsonResponse = response.getBody().replace('[','');
jsonResponse = jsonResponse.replace(']','');
system.debug('jsonResponse; '+jsonResponse);
// Parse JSON response to get all the totalPrice field values.
JSONParser parser = JSON.createParser(jsonResponse);
String feedBodyStr = '';
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'text')) {
// Get the value.
parser.nextToken();
// Compute the grand total price for all invoices.
feedBodyStr = parser.getText();
}
}
system.debug('feedBodyStr=' + feedBodyStr);
fItemList[0].Body = feedBodyStr;
update fItemList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment