// bot.js
import { Bot, defineBot, isMainModule } from '@botmesh/core'
import { BaileysProvider } from '@botmesh/baileys'
const createBot = defineBot((provider) => {
const bot = new Bot({ provider })
// One keyword, or any of a list (whole message, case and accents ignored)
bot.when('hi', (ctx) => ctx.reply('👋 Hello!'))
bot.when(['bye', 'goodbye', 'see you'], (ctx) => ctx.reply('👋 See you later!'))
// The word anywhere in the message
bot.when({ pattern: 'invoice', match: 'contains' }, (ctx) => ctx.reply('🧾 I see you mentioned an invoice'))
// Regular expressions
bot.when(/[\w.]+@[\w.]+\.\w+/, (ctx) => ctx.reply('📧 That looks like an email'))
bot.when(/\d{4,}/, (ctx) => ctx.reply(`🔢 Detected a long number in "${ctx.body}"`))
// Quote the user's message, or react to it
bot.when('quote', (ctx) => ctx.quote('Replying to your exact message'))
bot.when('good bot', (ctx) => ctx.react('👍'))
// Who wrote, and where
bot.when('who', (ctx) =>
ctx.reply(`You are ${ctx.senderInfo.displayName ?? 'someone'} (${ctx.sender}) in a ${ctx.isGroup ? 'group' : 'private chat'}.`),
)
// Multi-line text with WhatsApp-style formatting
bot.when('info', (ctx) => ctx.reply(['ℹ️ *Bot info*', '', '• Engine: botmesh', '• Works on: WhatsApp, Telegram'].join('\n')))
bot.onMessage((ctx) => ctx.reply('🤔 I did not recognize that. Try: hi, quote, who, info.'))
return bot
})
export default createBot
// `node bot.js` connects to WhatsApp; `npx botmesh dev bot.js` skips this block
if (isMainModule(import.meta)) {
const bot = await createBot(new BaileysProvider({ session: 'messages-bot' }))
bot.on('ready', () => console.log('✅ Connected. Send "hi" from another phone.'))
await bot.start()
}