Last active
May 14, 2019 19:41
-
-
Save ajb413/6385665bdfafec274543880656bda499 to your computer and use it in GitHub Desktop.
IBM Watson Language Translator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example languages to translate to, English, Spanish, French | |
// ['en', 'es', 'fr'] | |
// require xhr module. We'll be using 3rd party http endpoint for | |
// translation. We need to make a get request to the endpoint with | |
// parameters and we will get back the translated text in response. xhr | |
// module will help in sending out http requests. | |
const xhr = require('xhr'); | |
const query = require('codec/query_string'); | |
const base64 = require('codec/base64'); | |
// Store your API key securely in the vault using the MY SECRETS button | |
// vault get param for your IBM key should be `ibm_translate_api_key` | |
const vault = require('vault'); | |
export default (request) => { | |
//API key | |
return vault.get('ibm_translate_api_key').then((apiKey) => { | |
// translation api url | |
const apiUrl = 'https://gateway.watsonplatform.net/language-translator/api/v3/translate?version=2018-05-01'; | |
// extract from request, channel on which the message was published | |
const channel = request.channels[0]; | |
// language to translate to | |
const outputLang = 'es'; // spanish | |
// set the body's `model_id` to `input-output` format with 2 letter | |
// language codes. Example: English to Spanish would be 'en-es' | |
const body = { | |
model_id: request.message.input_lang + '-' + outputLang, | |
text: [request.message.text] | |
}; | |
const httpOptions = { | |
method: 'POST', | |
headers: { | |
'Authorization': 'Basic ' + base64.btoa('apikey:' + apiKey), | |
'Content-Type': 'application/json' | |
}, | |
body | |
}; | |
// We'll invoke xhr fetch to get the HTTP response | |
return xhr.fetch(apiUrl, httpOptions) | |
.then(r => { | |
const body = JSON.parse(r.body); | |
request.message.original_text = request.message.text; | |
request.message.output_lang = outputLang; | |
request.message.text = body.translations[0].translation; | |
return request.ok(); | |
}) | |
.catch((e) => { | |
console.error(e); | |
return request.abort(); | |
}); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"text": "Hi", | |
"input_lang": "en" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"text": "Hola", | |
"input_lang": "en", | |
"original_text": "Hi", | |
"output_lang": "es" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment