// bot.js
import { Bot, defineBot, flow, isMainModule, slashCommandsPlugin } from '@botmesh/core'
import { BaileysProvider } from '@botmesh/baileys'
const createBot = defineBot((provider) => {
// 1. The bot: transport-agnostic engine + the provider you receive
const bot = new Bot({ provider })
// 2. Built-in slash commands
bot.plug(slashCommandsPlugin, {
greeting: '🤖 Hi! Try: hi, photo, poll, register. Commands: /help /reset /cancel /id /ping',
})
// 3. Simple replies
bot.when(['hi', 'hello'], (ctx) => ctx.reply(`👋 Hello ${ctx.pushName ?? 'there'}! Send "register" to try a flow.`))
// 4. Media and polls
bot.when('photo', (ctx) => ctx.photo('https://picsum.photos/400/300', '📸 Here is a photo'))
bot.when('poll', (ctx) => ctx.poll('Favourite colour?', ['Red', 'Green', 'Blue']))
// 5. A multi-step flow with validation and a menu
bot.use(
flow('register')
.say('📝 *Let us register you*')
.ask('What is your name?', 'name')
.ask('What is your email?', 'email', {
validate: (body) => (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(body) ? true : '❌ That is not an email, try again'),
})
.choose('Do you want our newsletter?', { yes: 'Yes, please', no: 'No, thanks' }, 'newsletter')
.say((_ctx, c) =>
`✅ Done!\n👤 ${c.state.name}\n📧 ${c.state.email}\n📰 Newsletter: ${c.state.newsletter}`,
)
.timeout('5m'),
)
// 6. Catch-all: runs only when nothing else handled the message
bot.onMessage((ctx) => ctx.reply('🤔 I did not get that. Send /help for commands.'))
return bot
})
export default createBot
// 7. Start: `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: 'my-bot' }))
bot.on('qr', () => console.log('📱 Scan the QR with WhatsApp → Linked devices'))
bot.on('ready', () => console.log('✅ Bot ready'))
await bot.start()
}