Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PubNub Text Translation with IBM Watson BLOCK
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