Last active
July 6, 2021 18:23
-
-
Save stephenlb/c8a17b51152ca50788a6ca3005dd9dfa to your computer and use it in GitHub Desktop.
PubNub Function for Microsoft's Translate V3 API
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 PubNub = require('pubnub'); | |
const readline = require('readline'); | |
const pubnub = new PubNub({ | |
publishKey: "pub-c-716a3b86-1770-4e00-985f-f12211d9def9", | |
subscribeKey: "sub-c-535a73fe-de7d-11eb-ad9d-a277214e1d91", | |
}); | |
const channel = "translate"; | |
pubnub.addListener({ | |
message: function(event) { | |
console.dir(event.message, { depth: null }); | |
console.log(''); | |
}, | |
}) | |
pubnub.subscribe({ | |
channels: [channel], | |
}); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
terminal: false, | |
}); | |
rl.on('line', function(line){ | |
pubnub.publish({ | |
channel: channel, | |
message: { | |
phrase : line, | |
language : "es,zh", | |
}, | |
}); | |
}); |
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
// Demo URL https://codepen.io/team/PubNub/pen/rdQMaN | |
// ALERT! | |
// YOU NEED TO ADD YOUR MICROSFOT API KEY 'Ocp-Apim-Subscription-Key' | |
// | |
// 1.) Click "My Secrets" button. | |
// 2.) Add a new Secret 'Ocp-Apim-Subscription-Key' | |
// 3.) Paste in your Microsoft Translator V3 API Key | |
// 4.) Save | |
// 5.) Restart/Start your Module | |
// | |
const xhr = require('xhr'); | |
const vault = require('vault'); | |
export default request => { | |
// Capture Text and Language to Translate | |
let text = request.message.phrase; | |
let lang = request.message.language || "es"; | |
let data = request.message.data || {}; | |
// ChatEngine Compatibility Data Object | |
lang = lang || (data && data.language) || 'zh-HH'; | |
text = text || (data && data.text) || 'hello'; | |
// Translate using Microsoft V3 Translator API | |
return translate( text, lang ).then( result => { | |
request.message.data = request.message.data || {}; | |
request.message.data.translation = result; | |
request.message.data.text = result.translations[0]['text']; | |
request.message.original = text; | |
request.message.phrase = request.message.data.text; | |
return request.ok(); | |
} ).catch( error => { | |
console.error(error); | |
return request.ok(); | |
} ); | |
}; | |
// ------------------------------------ | |
// Microsoft Translator API V3 | |
// ------------------------------------ | |
// Microsoft Response Format | |
// { detectedLanguage : { language : "en", score : 1.0 }, | |
// translations : [{ text : text, to : language }] } | |
function translate( text='hello', language='zh-HH' ) { | |
return new Promise( ( resolve, reject ) => { | |
return vault.get('Ocp-Apim-Subscription-Key').then( oask => { | |
if (!oask) return reject("Missing 'Ocp-Apim-Subscription-Key'"); | |
let headers = {}; | |
let opts = { | |
host : 'https://dev.microsofttranslator.com' | |
, method : 'POST' | |
, headers : headers | |
, path : '/translate?api-version=3.0' | |
, to : '&to=' + language | |
, body : JSON.stringify([{ Text : text }]) | |
}; | |
headers['Content-Type'] = 'application/json'; | |
headers['Ocp-Apim-Subscription-Key'] = oask; | |
let uri = opts.host + opts.path + opts.to; | |
return xhr.fetch( uri, opts ) | |
.then( res => resolve(JSON.parse(res.body)[0]) ) | |
.catch( error => reject(error) ); | |
} ).catch( error => { | |
return reject(error); | |
} ); | |
} ); | |
} |
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
{ | |
"name": "demo", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js" | |
}, | |
"author": "", | |
"license": "MIT", | |
"dependencies": [ | |
"pubnub" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment