Skip to content

Instantly share code, notes, and snippets.

@vadim-berman
Created March 17, 2021 07:16
Show Gist options
  • Save vadim-berman/e829356a1894796738ae760ba178fec6 to your computer and use it in GitHub Desktop.
Save vadim-berman/e829356a1894796738ae760ba178fec6 to your computer and use it in GitHub Desktop.
Detecting abuse in an inbound PubNub message from within a module
const xhr = require('xhr');
const vault = require('vault');
// Use the PubNub Functions Vault Module to store these keys securely.
// Click the MY SECRETS button on the left.
export default (request) => {
console.log('1. original', request.message.text);
const getAPIKey = new Promise((resolve) => {
vault.get('tisaneApiKey').then((key) => {
resolve(key);
});
});
return getAPIKey.then((tisaneApiKey) => {
// the "snippets" setting places the relevant text snippets in the response;
// by default, only the offset and the length are sent
return xhr.fetch('https://api.tisane.ai/parse', {
'method': 'POST',
'headers': {
'Ocp-Apim-Subscription-Key': tisaneApiKey
},
'body': JSON.stringify({
'language': 'en', // or whatever language you use
'content': request.message.text,
'settings': { 'snippets': true }
})
});
}).then((reply) => {
let responseJson = JSON.parse(reply.body);
if (responseJson.abuse) {
// enrich the message with the relevant information
request.message.tisane_abuse_detected = responseJson.abuse;
console.log('2. possible abuse found', responseJson);
return request.abort(); // return back to the sender
} else {
return request.ok();
}
}).catch((err) => {
console.error(err);
return request.abort();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment