Translation using Microsoft api

reddy_vishal09
Kilo Contributor

Hi,

We are trying to translate the journal fields in an incident form using Microsoft API and we built a   business rule for the same which uses the REST message but we are facing issues with the business rule as the OAuth token generated by Microsoft API is not constant. Any input on this would be greatly helpful as it's been a while we are trying to implement this.

Thanks

3 REPLIES 3

Rushit Patel2
Tera Guru

Hi,



you can make a schedule job that runs evry 8 mins as token only lasts for 10 mins. in that schedule job make REST call and get the Token and store it in a system_property.



and use that token in your businesss rule. by doing this your businesss rule will always have fresh token.



find_real_file.png



this links will be helpful



Creating a Scheduled Job - ServiceNow Wiki


Adding a Property - ServiceNow Wiki


Obtaining an Access Token



Let me know if this works.



(please mark helpful/correct/like based on impact)


dturne83
Giga Contributor

Hello,



I had the same requirement, so here are the steps that I did to figure this out.



1) Sign-up with Microsoft to get your keys. They'll give you two, I think one is for text translation and one is for speech. I only used the text one.


The instructions are here: Announcements: Microsoft Translator Moves to the Azure portal — Customer Feedback & Ideas for Micros...



They've recently switched from an old Bing method to a new one - so this will cover the new method.



2) Once you've got your shiny new keys you're ready to make a scheduled job to get the Translation Token you'll need. This token changes every 10 minutes so you need the scheduled job like Rushit says above.


I created two new system properties, one to hold the key that you receive when you sign up, and another to hold the longer token that you receive from MS.


Now you need to send a POST message to https://api.cognitive.microsoft.com/sts/v1.0/issueToken to get your really long key that you use to translate the text.


I set the scheduled job to run every 8 minutes. Here's the code I used:



function getSetToken(){


  var subscriptionKey = gs.getProperty('ms.translator.key');


  var client = new Packages.org.apache.commons.httpclient.HttpClient();


  var post = new Packages.org.apache.commons.httpclient.methods.PostMethod("https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key=" + "your key goes here");


  var returnCode = client.executeMethod(post);


  var output = "" + post.getResponseBodyAsString();


  var btoken = output;


  if(output){


  //gs.log("my new access token is: "+ output);


  var newToken = output;


  var rec = new GlideRecord('sys_properties');


  rec.addQuery('name', 'ms.translator.token');


  rec.query();


  if(rec.next()){


  rec.value = newToken;


  rec.update();


  }


  }



}


getSetToken();






2) Now you need to make a script like a business rule to use this information to auto-translate a field you need. For me, I had to translate a 'message' field on the 'outage' table.



I created a new 'After' Insert and Update business rule with a condition of message is not empty or message changes:



The response you get of the translation has a bit of extra text you don't need, so that's stripped away in lines 18 to 22:



function translateOutageMsg(){


  var outageMsg = current.message;


  var accessToken = gs.getProperty('ms.translator.token');


  //gs.log("My access token is: " + accessToken);



  var target_url = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + encodeURIComponent(outageMsg) +


  "&from=" + encodeURIComponent("en") +


  "&to=" + encodeURIComponent("fr");



  var sm = new sn_ws.RESTMessageV2();


  sm.setHttpMethod('get');


  sm.setEndpoint(target_url);


  sm.setRequestHeader('Authorization', "Bearer " + accessToken);


  var response = sm.execute();


  var responseBody = response.getBody();



  //remove 68 characters from the responseBody variable: <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">


  var translationCleaning = responseBody.slice(68);


  //now remove the last few characters at the end of the string since they're: </string>


  var finalTranslation = translationCleaning.replace("</string>", "");


  //gs.log("The translated string is: " + finalTranslation);



  current.u_french_message = finalTranslation;


  current.update();


}


translateOutageMsg();






Your requirements may vary, so you'll have to adjust the field names a bit here or there, but this should work in most cases.


This can be used on most any field on most tables to get an automated translation using the Microsoft Translation API.



I hope this helps!



Thanks,


D


Hi, I found this thread since I'm looking for something very much alike this.

I'm looking to transform a value in Short Description from any language to English using Microsoft Translate with original language and then translated language.

Does this solution of yours still work? and is it possible to auto detect input language?

Looking for something like:

(Finnish)
Moi
----
Hi

 

or

(Swedish)
Här kommer texten
---_
Here is the text