Files
simplex-chat/packages/simplex-chat-nodejs/examples/squaring-bot.ts
T
sh 42dd36bf09 simplex-chat-nodejs: typed DbConfig (#6875)
* simplex-chat-nodejs: typed DbConfig for ChatApi.init and BotDbOpts

* simplex-chat-nodejs: regenerate typedoc docs for DbConfig

* simplex-chat-nodejs: rename DbConfig.kind to .type
2026-04-24 16:43:43 +01:00

43 lines
1.9 KiB
TypeScript

import {T} from "@simplex-chat/types"
import {bot, util} from "../dist"
(async () => {
const welcomeMessage = "Hello! I am a simple squaring bot.\n\nIf you send me a number, I will calculate its square."
const [chat, _user, _address] = await bot.run({
profile: {displayName: "Squaring bot example", fullName: ""},
dbOpts: {type: "sqlite", filePrefix: "./squaring_bot"},
options: {
addressSettings: {autoAccept: true, welcomeMessage, businessAddress: false},
commands: [ // commands to show in client UI
{type: "command", keyword: "help", label: "Send welcome message"},
{type: "command", keyword: "info", label: "More information (not implemented)"}
],
logContacts: true,
logNetwork: false
},
onMessage: async (ci, content) => {
const n = +content.text
const reply = typeof n === "number" && !isNaN(n)
? `${n} * ${n} = ${n * n}`
: `this is not a number`
await chat.apiSendTextReply(ci, reply)
},
onCommands: { // command handlers can be different from commands to be shown in client UI
"help": async (ci: T.AChatItem, _cmd: util.BotCommand) => {
await chat.apiSendTextMessage(ci.chatInfo, welcomeMessage)
},
// fallback handler that will be called for all other commands
"": async (ci: T.AChatItem, _cmd: util.BotCommand) => {
await chat.apiSendTextReply(ci, "This command is not supported")
}
},
// If you use `onMessage` and subscribe to "newChatItems" event, exclude content messages from processing
// If you use `onCommands` and subscribe to "newChatItems" event, exclude commands from processing
events: {
"chatItemReaction": ({added, reaction}) => {
console.log(`${util.senderName(reaction.chatInfo, reaction.chatReaction.chatDir)} ${added ? "added" : "removed"} reaction ${util.reactionText(reaction)}`)
}
},
})
})()