Created
October 26, 2016 11:39
-
-
Save sunnygleason/167cafd3f3e2a5e5cf887897135d7aaf to your computer and use it in GitHub Desktop.
PubNub Text Translation with IBM Watson BLOCK
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
const console = require('console'); | |
const xhr = require('xhr'); | |
const query = require('codec/query_string'); | |
const base64 = require('codec/base64'); | |
const pubnub = require('pubnub'); | |
export default (request) => { | |
const username = '00000000-0000-0000-0000-000000000000'; | |
const password = '000000000000'; | |
const apiUrl = 'https://gateway.watsonplatform.net/language-translation/api/v2/translate'; | |
const lang = 'es'; | |
const queryParams = { | |
source: "en", | |
target: lang, | |
text: request.message.text_en | |
}; | |
const httpOptions = { | |
headers: { | |
Authorization: 'Basic ' + | |
base64.btoa(username + ':' + password) | |
} | |
}; | |
return xhr.fetch(apiUrl + '?' + query.stringify(queryParams), httpOptions) | |
.then(r => { | |
request.message['text_' + lang] = r.body; | |
return request.ok(); | |
}, e => { | |
console.log(e); | |
}) | |
.catch((e) => { | |
console.error(e); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment