Skip to content

Instantly share code, notes, and snippets.

@sunnygleason
Created October 26, 2016 11:39
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 sunnygleason/167cafd3f3e2a5e5cf887897135d7aaf to your computer and use it in GitHub Desktop.
Save sunnygleason/167cafd3f3e2a5e5cf887897135d7aaf to your computer and use it in GitHub Desktop.
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