Created
February 11, 2022 17:13
-
-
Save sendbird-community/6377f8519b2deb8b3472f7b41931fe55 to your computer and use it in GitHub Desktop.
testToxicity function
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
function testToxicity(message, channelUrl, senderId) { | |
var API_KEY = process.env.PERSPECTIVE_API_KEY; | |
var DISCOVERY_URL = | |
"https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1"; | |
.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