// bot.js
import { Bot, defineBot, isMainModule } from '@botmesh/core'
import { BaileysProvider } from '@botmesh/baileys'
// Each answer with the phrases that lead to it
const FAQ = [
{ ask: ['hours', 'opening hours', 'when are you open'], answer: '🕘 Monday to Saturday, 9:00 to 19:00.' },
{ ask: ['address', 'where are you', 'location'], answer: '📍 42 Baker Street, downtown.' },
{ ask: ['prices', 'price', 'how much'], answer: '💲 Coffee $2 · Cake $4 · Combo $5' },
]
const createBot = defineBot((provider) => {
const bot = new Bot({ provider })
// Whole-message match, normalized: "HOURS", "Hours " and "hóurs" all match 'hours'
for (const { ask, answer } of FAQ) bot.when(ask, (ctx) => ctx.reply(answer))
bot.when(['help', 'faq', 'menu'], (ctx) =>
ctx.reply('❓ Ask me about: hours · address · prices · delivery\nOr send an order number like #1234.'),
)
// `contains`: the word anywhere in the message
bot.when({ pattern: 'delivery', match: 'contains' }, (ctx) =>
ctx.reply('🛵 We deliver within 3 km. Orders over $10 ship free.'),
)
// A regex: an order number such as #1234
bot.when(/#\d{4,}/, (ctx) => ctx.reply(`📦 Order ${/#\d{4,}/.exec(ctx.body)[0]} is on its way.`))
// Runs only when no trigger above matched
bot.onMessage((ctx) => ctx.reply('🤷 I do not know that one yet. Send "help" to see the topics.'))
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: 'faq-bot' }))
bot.on('ready', () => console.log('✅ Connected. Send "help" from another phone.'))
await bot.start()
}