Created
November 26, 2018 04:48
-
-
Save vadim-berman/de11c71c247be8abdc85a155063d8226 to your computer and use it in GitHub Desktop.
Detecting abuse in an inbound PubNub message
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
export default (request) => { | |
const xhr = require('xhr'); | |
console.log('1. original', request.message); | |
{ | |
return xhr.fetch('https://api.tisane.ai/parse', { | |
'method' : 'POST', | |
'headers' : { | |
'Ocp-Apim-Subscription-Key': '{your subscription key}' | |
}, | |
'body': JSON.stringify({ | |
'language': 'en', // or whatever language you use | |
'content': request.message, | |
'settings': {'snippets': true} // the "snippets" setting places the relevant text snippets in the response; by default, only the offset and the length are sent | |
}), | |
'timeout' : 5000 | |
}); } | |
.then((reply) => { | |
var responseJson = JSON.parse(reply.body); | |
if (responseJson.abuse) { | |
// enrich the message with the relevant information | |
request.abuse_text = responseJson.abuse.text; | |
request.abuse_type = responseJson.abuse.type; | |
console.log('2. possible abuse found', responseJson.abuse.type); | |
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