// 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, lang: 'es' })
const usuarios = new Map() // reemplázalo por tu base de datos
// /cancel y /reset salen de cualquier flujo
bot.plug(slashCommandsPlugin)
bot.use(
flow('registro')
.say('📝 *Vamos a registrarte*')
.ask('¿Cómo te llamas?', 'name', {
validate: (body) => (body.trim().length >= 2 ? true : '❌ Al menos 2 letras, por favor.'),
})
.ask('¿Cuál es tu email?', 'email', {
validate: (body) => (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(body) ? true : '❌ Email no válido. Inténtalo de nuevo.'),
})
.ask('¿Cuántos años tienes?', 'age', {
validate: (body) => (/^\d+$/.test(body) && Number(body) >= 18 ? true : '❌ Debes tener 18 años o más (solo números).'),
})
.do((ctx, c) => {
usuarios.set(ctx.sender, { name: c.state.name, email: c.state.email, age: Number(c.state.age) })
})
.say((_ctx, c) => ['✅ *¡Registro completo!*', '', `👤 ${c.state.name}`, `📧 ${c.state.email}`, `🎂 ${c.state.age}`].join('\n'))
.timeout('5m'),
)
bot.when('perfil', (ctx) => {
const usuario = usuarios.get(ctx.sender)
return ctx.reply(usuario ? `👤 ${usuario.name} · 📧 ${usuario.email} · 🎂 ${usuario.age}` : 'Aún no estás registrado. Envía "registro".')
})
bot.onMessage((ctx) => ctx.reply('Envía "registro" para darte de alta o "perfil" para ver tus datos.'))
return bot
})
export default createBot
// `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: 'register-bot' }))
bot.on('ready', () => console.log('✅ Conectado. Escribe "registro" desde otro teléfono.'))
await bot.start()
}