// bot.js
import { Bot, defineBot, isMainModule, loadMediaOf, MediaTooLargeError } from '@botmesh/core'
import { BaileysProvider } from '@botmesh/baileys'
const createBot = defineBot((provider) => {
const bot = new Bot({ provider, lang: 'es' })
// Cada fuente puede ser una URL, una ruta local o un Buffer.
// Las URLs de example.com son de relleno: apúntalas a tus propios archivos.
bot.when('foto', (ctx) => ctx.photo('https://picsum.photos/400/300', '📸 Una foto al azar'))
bot.when('carta', (ctx) => ctx.file('https://example.com/carta.pdf', 'carta.pdf', '📄 Nuestra carta'))
bot.when('video', (ctx) => ctx.video('https://example.com/clip.mp4', '🎬 Un clip corto'))
bot.when('voz', (ctx) => ctx.voice('https://example.com/nota.ogg'))
bot.when('sticker', (ctx) => ctx.sticker('https://picsum.photos/512/512.webp', { pack: 'Mi bot', author: 'yo' }))
bot.when('donde', (ctx) => ctx.location(4.711, -74.0721, 'Bogotá, Colombia'))
bot.when('contacto', (ctx) => ctx.contact('+573001234567', 'Soporte'))
// Media entrante: descarga los bytes solo cuando los necesitas
bot.onType('image', async (ctx) => {
let media
try {
media = await loadMediaOf(ctx, { maxBytes: 5 * 1024 * 1024 })
} catch (err) {
if (err instanceof MediaTooLargeError) return ctx.reply('Esa imagen es demasiado grande (máximo 5 MB).')
throw err
}
if (!media) return ctx.reply('No pude descargar esa imagen.')
await ctx.react('😍')
await ctx.reply(`🖼️ Recibí tu imagen ${media.mimetype}${ctx.body ? `: "${ctx.body}"` : ''}`)
})
// Una ubicación compartida llega normalizada en ctx.sharedLocation
bot.onType('location', (ctx) => {
const loc = ctx.sharedLocation
return ctx.reply(loc ? `📍 Estás en ${loc.latitude}, ${loc.longitude}` : 'No pude leer esa ubicación')
})
bot.onMessage((ctx) => ctx.reply('Prueba: foto, carta, video, voz, sticker, donde, contacto, o envía una imagen.'))
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: 'media-bot' }))
bot.on('ready', () => console.log('✅ Conectado. Escribe "foto" desde otro teléfono.'))
await bot.start()
}