// bot.js
import { Bot, defineBot, flow, isMainModule, slashCommandsPlugin } from '@botmesh/core'
import { BaileysProvider } from '@botmesh/baileys'
const createBot = defineBot((provider) => {
// 1. El bot: motor agnóstico de transporte + el provider que recibes
const bot = new Bot({ provider, lang: 'es' })
// 2. Comandos slash integrados
bot.plug(slashCommandsPlugin, {
greeting: '🤖 ¡Hola! Prueba: hola, foto, encuesta, registro. Comandos: /help /reset /cancel /id /ping',
})
// 3. Respuestas simples
bot.when(['hola', 'buenas'], (ctx) => ctx.reply(`👋 ¡Hola ${ctx.pushName ?? 'de nuevo'}! Envía "registro" para probar un flujo.`))
// 4. Media y encuestas
bot.when('foto', (ctx) => ctx.photo('https://picsum.photos/400/300', '📸 Aquí tienes una foto'))
bot.when('encuesta', (ctx) => ctx.poll('¿Color favorito?', ['Rojo', 'Verde', 'Azul']))
// 5. Un flujo de varios pasos con validación y un menú
bot.use(
flow('registro')
.say('📝 *Vamos a registrarte*')
.ask('¿Cómo te llamas?', 'name')
.ask('¿Cuál es tu email?', 'email', {
validate: (body) => (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(body) ? true : '❌ Eso no es un email, inténtalo de nuevo'),
})
.choose('¿Quieres recibir nuestro boletín?', { si: 'Sí, por favor', no: 'No, gracias' }, 'newsletter')
.say((_ctx, c) =>
`✅ ¡Listo!\n👤 ${c.state.name}\n📧 ${c.state.email}\n📰 Boletín: ${c.state.newsletter}`,
)
.timeout('5m'),
)
// 6. Catch-all: solo se ejecuta si nada más manejó el mensaje
bot.onMessage((ctx) => ctx.reply('🤔 No te entendí. Envía /help para ver los comandos.'))
return bot
})
export default createBot
// 7. Arranque: `node bot.js` conecta con WhatsApp; `npx botmesh dev bot.js` se salta este bloque
if (isMainModule(import.meta)) {
const bot = await createBot(new BaileysProvider({ session: 'my-bot' }))
bot.on('qr', () => console.log('📱 Escanea el QR en WhatsApp → Dispositivos vinculados'))
bot.on('ready', () => console.log('✅ Bot listo'))
await bot.start()
}