Difference between revisions of "Bot Playground/Examples/AI IM Autoresponder"
From SmartBots Developers Docs
(Created page with "<syntaxhighlight> // Bots Playground script: Test (build 1 by Glaznah Gassner) const aiSettings = { instructions: "You are a bot in Second Life. Act like a greater, be polit...") |
|||
Line 1: | Line 1: | ||
− | <syntaxhighlight> | + | {{DISPLAYTITLE:{{SUBPAGENAME}}}} |
+ | ==AI IM Autoresponder== | ||
+ | |||
+ | This is a source code of simple AI autoresponder by SmartBots, available in [https://www.mysmartbots.com/store/item/100/ai_gpt_im_autoresponder/ Bot Store]. | ||
+ | |||
+ | Script listens for incoming IMs, sends them to SmartBots AI engine and responds the result back. | ||
+ | |||
+ | <syntaxhighlight lang="javascript"> | ||
// Bots Playground script: Test (build 1 by Glaznah Gassner) | // Bots Playground script: Test (build 1 by Glaznah Gassner) | ||
Latest revision as of 10:19, 1 August 2024
AI IM Autoresponder
This is a source code of simple AI autoresponder by SmartBots, available in Bot Store.
Script listens for incoming IMs, sends them to SmartBots AI engine and responds the result back.
// Bots Playground script: Test (build 1 by Glaznah Gassner)
const aiSettings = {
instructions: "You are a bot in Second Life. Act like a greater, be polite and friendly. Answer any question you can.",
};
// Init default settings
Object.assign(aiSettings, userSettings);
if(aiSettings.maxResponseTokens) {
aiSettings.maxResponseTokens = Number(aiSettings.maxResponseTokens);
}
// console.log("AI settings:", aiSettings);
// Init AI
Bot.AI.configure(aiSettings);
// Listen for IMs
Bot.on("instant_message", (event) => {
if(event.speaker_type != "AVATAR") { return; }
processMessage(event.speaker_name, event.speaker_uuid, event.message);
});
console.log(process.name + " is running");
async function processMessage(senderName, senderUUID, message) {
console.log(`[IM] ${senderName}: ${message}`);
Bot.startTyping(senderUUID);
try {
const convo = Bot.AI.getConversationByName(senderName);
let res;
try {
res = await convo.chat(message, { featureEmptyResponse: true });
} catch(e) {
console.error(`AI error: ${e.message}`);
return;
}
if(!res.text) {
console.log(`[AI] ${Bot.name}: response empty, ${res.emptyTextReason}`);
return;
}
console.log(`[AI] ${Bot.name}: ${res.text}`);
Bot.im(senderUUID, res.text);
} finally {
Bot.stopTyping(senderUUID);
}
}