Skip to content

Instantly share code, notes, and snippets.

@Srushtika
Created May 13, 2021 09:41
Show Gist options
  • Save Srushtika/2c5eb07d9f294aab8065fdf3dc42a7fb to your computer and use it in GitHub Desktop.
Save Srushtika/2c5eb07d9f294aab8065fdf3dc42a7fb to your computer and use it in GitHub Desktop.
//code from the lambda function
const request = require("request");
const axios = require("axios");
const ABLY_API_KEY = "<YOUR-ABLY-API-KEY>";
let textData = "";
let originalData;
let originalMessagePayload;
const publishMessageToAbly = async (textData, originalData, msgTimestamp) => {
console.log("pub to ably with axios");
let ablyMsgPayload = [
{
fields: {
clientId: originalData.clientId,
msgId: originalData.msgId,
username: originalData.username,
originalChatMessage: originalData.messageContent,
filteredChatMessage: textData,
timestamp: msgTimestamp,
},
}
];
return new Promise((resolve, reject) => {
axios({
method: "post",
url: "https://rest.ably.io/channels/filtered-chat/messages",
headers: {
"Content-Type": "application/json",
accept: "application/json",
authorization: "<AUTH HEADER>",
},
data: {
name: "hive",
data: { records: ablyMsgPayload },
},
})
.then(function (response) {
console.log("published to ably");
resolve();
})
.catch(function (error) {
console.log(error);
reject(error);
});
});
};
var hiveRequestOptions = {
method: "POST",
url: "https://api.thehive.ai/api/v2/task/sync",
headers: {
accept: "application/json",
authorization: "token pObOOlEhuqK5a0jr7Ax2nppMfLwneUfk",
},
form: { text_data: textData },
};
async function getAnalysisByHive(textData) {
let textAnalysis;
hiveRequestOptions.form.text_data = textData;
return new Promise((resolve, reject) => {
request(hiveRequestOptions, function (error, response, body) {
if (error) reject(error);
textAnalysis = body;
resolve(textAnalysis);
});
});
}
async function extractProfanityFilteredWords(textData) {
let analysedText;
try {
analysedText = await getAnalysisByHive(textData);
} catch (error) {
console.log(error);
}
let profanitiesDetected = JSON.parse(analysedText).status[0].response
.text_filters;
return profanitiesDetected;
}
async function rebuildMessage(textData, originalData, msgTimestamp) {
let profanitiesDetected = await extractProfanityFilteredWords(textData);
profanitiesDetected.forEach((profanity) => {
let regext = new RegExp(profanity.value, "i");
textData = textData.replace(regext, "***");
});
console.log(textData);
try {
await publishMessageToAbly(textData, originalData, msgTimestamp);
} catch (error) {
console.log(error);
}
return true;
}
exports.handler = async function (event) {
console.log(event.messages[0].data);
let myLog = JSON.parse(event.messages[0].data);
console.log(event.messages[0].timestamp)
let msgTimestamp = event.messages[0].timestamp
originalMessagePayload = myLog.originalChatMessage;
textData = originalMessagePayload.messageContent;
originalData = originalMessagePayload;
await rebuildMessage(textData, originalData, msgTimestamp);
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment