// bot.js
import { Bot, defineBot, flow, isMainModule, slashCommandsPlugin } from '@botmesh/core'
import { BaileysProvider } from '@botmesh/baileys'
const PRODUCTS = {
phone: { label: '📱 Phone ($599)', price: 599 },
shirt: { label: '👕 Shirt ($25)', price: 25 },
lamp: { label: '🏠 Lamp ($35)', price: 35 },
}
const createBot = defineBot((provider) => {
const bot = new Bot({
provider,
// Sent when a wait of the flow times out
conversation: { timeoutMessage: '⏰ Your cart expired. Send "shop" to start again.' },
})
bot.plug(slashCommandsPlugin)
// A reusable sub-flow: runs inline from the checkout and comes back
const address = flow([]).ask('🏠 Street and number?', 'street').ask('🏙️ City?', 'city')
bot.use(
flow('shop')
.timeout('2m')
.choose('🛒 *What do you want to buy?*', {
phone: PRODUCTS.phone.label,
shirt: PRODUCTS.shirt.label,
lamp: PRODUCTS.lamp.label,
}, 'product')
.choose('How many?', { one: '1 unit', two: '2 units', three: '3 units' }, 'qty')
.say((_ctx, c) => {
const qty = { one: 1, two: 2, three: 3 }[c.state.qty]
const product = PRODUCTS[c.state.product]
c.state.total = qty * product.price
return `🧾 ${qty} × ${product.label} = $${c.state.total}`
})
.choose('Confirm your order?', {
yes: { label: 'Yes ✅', then: address },
change: { label: '⬅️ Pick another product', goto: 'shop' },
})
.say((_ctx, c) => `📦 Order placed: $${c.state.total}, shipping to ${c.state.street}, ${c.state.city}.`),
)
bot.onMessage((ctx) => ctx.reply('Send "shop" to start shopping.'))
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: 'shop-bot' }))
bot.on('ready', () => console.log('✅ Connected. Send "shop" from another phone.'))
await bot.start()
}