Skip to content

Instantly share code, notes, and snippets.

@sendbird-community
Created February 11, 2022 17:13
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 sendbird-community/6377f8519b2deb8b3472f7b41931fe55 to your computer and use it in GitHub Desktop.
Save sendbird-community/6377f8519b2deb8b3472f7b41931fe55 to your computer and use it in GitHub Desktop.
testToxicity function
function testToxicity(message, channelUrl, senderId) {
var API_KEY = process.env.PERSPECTIVE_API_KEY;
var DISCOVERY_URL =
"https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1";
google
.discoverAPI(DISCOVERY_URL)
.then((client) => {
const analyzeRequest = {
comment: {
text: message,
},
requestedAttributes: {
TOXICITY: {},
},
};
client.comments.analyze(
{
key: API_KEY,
resource: analyzeRequest,
},
(err, response) => {
if (err) throw err;
console.log(JSON.stringify(response.data, null, 2));
if (
response.data.attributeScores.TOXICITY.spanScores[0].score.value >
0.5
) {
banUser(channelUrl, senderId);
}
}
);
})
.catch((err) => {
throw err;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment