102 lines
2.5 KiB
JavaScript
102 lines
2.5 KiB
JavaScript
const WickrIOBotAPI = require('wickrio-bot-api');
|
|
const WickrUser = WickrIOBotAPI.WickrUser;
|
|
const bot = new WickrIOBotAPI.WickrIOBot();
|
|
const WickrIOAPI = bot.apiService().WickrIOAPI;
|
|
|
|
const axios = require('axios');
|
|
|
|
var fs = require('fs');
|
|
|
|
process.stdin.resume(); //so the program will not close instantly
|
|
|
|
bot.processesJsonToProcessEnv()
|
|
|
|
var bot_username;
|
|
|
|
async function exitHandler(options, err) {
|
|
try {
|
|
var closed = await bot.close();
|
|
if (err || options.exit) {
|
|
console.log("Exit reason:", err);
|
|
process.exit();
|
|
} else if (options.pid) {
|
|
process.kill(process.pid);
|
|
}
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
}
|
|
|
|
//catches ctrl+c and stop.sh events
|
|
process.on('SIGINT', exitHandler.bind(null, {exit: true}));
|
|
|
|
// catches "kill pid" (for example: nodemon restart)
|
|
process.on('SIGUSR1', exitHandler.bind(null, {pid: true}));
|
|
process.on('SIGUSR2', exitHandler.bind(null, {pid: true}));
|
|
|
|
//catches uncaught exceptions
|
|
process.on('uncaughtException', exitHandler.bind(null, {exit: true}));
|
|
|
|
async function main() {
|
|
try {
|
|
var tokens = JSON.parse(process.env.tokens);
|
|
var status = await bot.start(tokens.WICKRIO_BOT_NAME.value)
|
|
if (!status) {
|
|
exitHandler(null, {
|
|
exit: true,
|
|
reason: 'Client not able to start'
|
|
});
|
|
}
|
|
|
|
//Passes a callback function that will receive incoming messages into the bot client
|
|
bot.startListening(listen);
|
|
|
|
} catch (err) {
|
|
return console.log(err);
|
|
}
|
|
}
|
|
|
|
async function listen(message) {
|
|
console.log('---------------------');
|
|
var wickrUser;
|
|
|
|
var parsedMessage = bot.parseMessage(message);
|
|
if (!parsedMessage) {
|
|
return;
|
|
}
|
|
|
|
var vGroupID = parsedMessage.vgroupid;
|
|
var userEmail = parsedMessage.userEmail;
|
|
var convoType = parsedMessage.convoType;
|
|
var personal_vGroupID = "";
|
|
console.log(userEmail);
|
|
console.log(parsedMessage.message)
|
|
|
|
if (convoType === 'personal')
|
|
personal_vGroupID = vGroupID;
|
|
|
|
try {
|
|
const agent_payload = {
|
|
question: parsedMessage.message,
|
|
requestSessionId: vGroupID,
|
|
bot: "trainerbot"
|
|
};
|
|
|
|
const agent_gateway_response = await axios.post(
|
|
'http://172.17.0.1:8000/bedrock-agent',
|
|
agent_payload
|
|
);
|
|
|
|
await WickrIOAPI.cmdSendRoomMessage(vGroupID, agent_gateway_response.data);
|
|
} catch (err) {
|
|
console.error("Error:", err.message);
|
|
await WickrIOAPI.cmdSendRoomMessage(vGroupID, err.message);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main();
|