// bot.js
import { Bot, defineBot, isMainModule, slashCommandsPlugin } from '@botmesh/core'
import { BaileysProvider } from '@botmesh/baileys'
// 1. Un árbol de mensajes por idioma (en un bot más grande: locales/es.json, locales/en.json)
const es = {
greeting: '👋 ¡Hola {name}! Envía "pedido" para un café, "cuenta" para ver cuántos llevas, o /lang en.',
order: { size: '¿De qué tamaño?', small: 'Pequeño', large: 'Grande', done: '☕ ¡Un café {size}, en camino!' },
cups: { zero: 'Todavía no has pedido.', one: 'Has pedido {count} café.', other: 'Has pedido {count} cafés.' },
lang: { changed: '🇪🇸 Desde ahora te respondo en español.', usage: 'Usa /lang es o /lang en.' },
fallback: 'Envía "hola", "pedido" o "cuenta".',
}
const en = {
greeting: '👋 Hello {name}! Send "order" for a coffee, "stats" for your count, or /lang es.',
order: { size: 'Which size?', small: 'Small', large: 'Large', done: '☕ One {size} coffee, coming up!' },
cups: { zero: 'You have not ordered yet.', one: 'You have ordered {count} coffee.', other: 'You have ordered {count} coffees.' },
lang: { changed: '🇬🇧 From now on I will answer in English.', usage: 'Use /lang es or /lang en.' },
fallback: 'Send "hi", "order" or "stats".',
}
const createBot = defineBot((provider) => {
// 2. Atiende dos idiomas; deduce el inglés por la primera palabra del mensaje
const bot = new Bot({
provider,
i18n: {
defaultLocale: 'es',
locales: ['es', 'en'],
detect: (ctx) => (/^(hi|hello|order|stats)\b/i.test(ctx.body) ? 'en' : undefined),
},
})
bot.locale('es', es)
bot.locale('en', en)
// 3. Recuerda el idioma detectado, para que los mensajes siguientes lo mantengan
bot.before('message', async (ctx, next) => {
if (ctx.langSource === 'detector') await ctx.setLang(ctx.lang)
await next()
})
// Los textos integrados (/help, /cancel, timeouts...) también siguen a ctx.lang
bot.plug(slashCommandsPlugin)
// 4. Cambio a petición: se guarda en el perfil del usuario (ctx.user)
bot.command('lang', async (ctx, [code]) => {
if (code !== 'es' && code !== 'en') return ctx.reply(ctx.t('lang.usage'))
await ctx.setLang(code)
await ctx.reply(ctx.t('lang.changed'))
})
bot.when(['hola', 'hi', 'hello'], (ctx) => ctx.reply(ctx.t('greeting', { name: ctx.pushName ?? '' })))
// 5. Una conversación en el idioma del usuario: c.t() sigue al último mensaje
bot.conversation(
'pedido',
async (c, ctx) => {
const size = await c.choose(c.t('order.size'), { small: c.t('order.small'), large: c.t('order.large') })
await ctx.user.set('cups', ((await ctx.user.get('cups')) ?? 0) + 1)
await c.say(c.t('order.done', { size: c.t(`order.${size}`).toLowerCase() }))
},
{ trigger: ['pedido', 'order'] },
)
// 6. Formas de plural: el número elige zero / one / other
bot.when(['cuenta', 'stats'], async (ctx) => ctx.reply(ctx.t('cups', { count: (await ctx.user.get('cups')) ?? 0 })))
bot.onMessage((ctx) => ctx.reply(ctx.t('fallback')))
return bot
})
export default createBot
// `node bot.js` se conecta a WhatsApp; `npx botmesh dev bot.js` se salta este bloque
if (isMainModule(import.meta)) {
const bot = await createBot(new BaileysProvider({ session: 'bot-cafe' }))
bot.on('ready', () => console.log('✅ Conectado'))
await bot.start()
}