// bot.js
import { Bot, defineBot, flow, isMainModule, slashCommandsPlugin } from '@botmesh/core'
import { BaileysProvider } from '@botmesh/baileys'
const createBot = defineBot((provider) => {
const bot = new Bot({ provider })
const users = new Map() // replace with your database
// /cancel and /reset leave any flow
bot.plug(slashCommandsPlugin)
bot.use(
flow('register')
.say('📝 *Let us register you*')
.ask('What is your name?', 'name', {
validate: (body) => (body.trim().length >= 2 ? true : '❌ At least 2 letters, please.'),
})
.ask('What is your email?', 'email', {
validate: (body) => (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(body) ? true : '❌ Invalid email. Try again.'),
})
.ask('How old are you?', 'age', {
validate: (body) => (/^\d+$/.test(body) && Number(body) >= 18 ? true : '❌ You must be 18 or older (numbers only).'),
})
.do((ctx, c) => {
users.set(ctx.sender, { name: c.state.name, email: c.state.email, age: Number(c.state.age) })
})
.say((_ctx, c) => ['✅ *Registration complete!*', '', `👤 ${c.state.name}`, `📧 ${c.state.email}`, `🎂 ${c.state.age}`].join('\n'))
.timeout('5m'),
)
bot.when('profile', (ctx) => {
const user = users.get(ctx.sender)
return ctx.reply(user ? `👤 ${user.name} · 📧 ${user.email} · 🎂 ${user.age}` : 'You are not registered yet. Send "register".')
})
bot.onMessage((ctx) => ctx.reply('Send "register" to sign up or "profile" to see your data.'))
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: 'register-bot' }))
bot.on('ready', () => console.log('✅ Connected. Send "register" from another phone.'))
await bot.start()
}