// 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 })
// Every source can be a URL, a local path or a Buffer.
// The example.com URLs are placeholders: point them at your own files.
bot.when('photo', (ctx) => ctx.photo('https://picsum.photos/400/300', '📸 A random photo'))
bot.when('menu', (ctx) => ctx.file('https://example.com/menu.pdf', 'menu.pdf', '📄 Our menu'))
bot.when('video', (ctx) => ctx.video('https://example.com/clip.mp4', '🎬 A short clip'))
bot.when('voice', (ctx) => ctx.voice('https://example.com/note.ogg'))
bot.when('sticker', (ctx) => ctx.sticker('https://picsum.photos/512/512.webp', { pack: 'My bot', author: 'me' }))
bot.when('where', (ctx) => ctx.location(4.711, -74.0721, 'Bogotá, Colombia'))
bot.when('contact', (ctx) => ctx.contact('+573001234567', 'Support'))
// Inbound media: download the bytes only when you need them
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('That image is too large (5 MB max).')
throw err
}
if (!media) return ctx.reply('I could not download that image.')
await ctx.react('😍')
await ctx.reply(`🖼️ Got your ${media.mimetype} image${ctx.body ? `: "${ctx.body}"` : ''}`)
})
// A shared location arrives normalized on ctx.sharedLocation
bot.onType('location', (ctx) => {
const loc = ctx.sharedLocation
return ctx.reply(loc ? `📍 You are at ${loc.latitude}, ${loc.longitude}` : 'I could not read that location')
})
bot.onMessage((ctx) => ctx.reply('Try: photo, menu, video, voice, sticker, where, contact, or send an image.'))
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: 'media-bot' }))
bot.on('ready', () => console.log('✅ Connected. Send "photo" from another phone.'))
await bot.start()
}