Soporte con timeout
Una conversación de soporte con timeout por inactividad, construida con bot.conversation() para tener control total sobre el intercambio.
La conversación
ts
import { Bot } from '@botmesh/core'
import { BaileysProvider } from '@botmesh/baileys'
const bot = new Bot({
provider: new BaileysProvider({
session: 'demo-bot',
qr: { terminal: true, small: true },
}),
conversation: { timeoutMessage: '⏰ Your support session expired due to inactivity.' },
})
bot.conversation('support', async (c, ctx) => {
await ctx.reply('🛠️ *Technical support*\nDescribe your issue in one sentence (you have 2 minutes):')
const issue = await c.waitText({ timeout: '2m', maxAttempts: 3 })
const priority = await c.choose('How urgent is it?', {
high: 'Urgent 🔴',
medium: 'Can wait a few hours 🟡',
low: 'No rush 🟢',
})
await ctx.reply(`🎫 Ticket registered ("${issue}", priority: ${priority}). We will contact you soon.`)
})
await bot.start()Cómo funciona
- El usuario envía "support" y la conversación arranca —
ctxes el mensaje que la disparó,ces elConversationControlsde esta conversación ctx.reply(...)anuncia el flujo, y luegoc.waitText({ timeout: '2m', maxAttempts: 3 })espera hasta 2 minutos por una respuesta de texto- Si el usuario no responde en 2 minutos, se lanza un
ConversationTimeout, la conversación termina, y el bot envía elconversation.timeoutMessageconfigurado en elBot c.choose(...)envía un menú numerado construido a partir de susoptionsy devuelve la CLAVE elegida (high,mediumolow)- El bot confirma el ticket con el problema y la prioridad recolectados
Manejar el timeout y la cancelación explícitamente
c.waitText / c.choose lanzan errores dedicados cuando una conversación termina antes de tiempo — captúralos para personalizar la respuesta en vez de depender del conversation.timeoutMessage global:
ts
import { Bot, ConversationTimeout, ConversationCancelled } from '@botmesh/core'
import { BaileysProvider } from '@botmesh/baileys'
const bot = new Bot({
provider: new BaileysProvider({ session: 'demo-bot', qr: { terminal: true, small: true } }),
})
bot.conversation('support', async (c, ctx) => {
try {
await ctx.reply('🛠️ Describe your issue (or send "cancel"):')
const issue = await c.waitText({ timeout: '2m' })
await ctx.reply(`✅ Registered: "${issue}". We will contact you.`)
} catch (err) {
if (err instanceof ConversationTimeout) {
await ctx.reply('⏰ Session expired due to inactivity.')
} else if (err instanceof ConversationCancelled) {
await ctx.reply('🛑 Session cancelled.')
} else {
throw err
}
}
})
await bot.start()Los comandos siempre escapan
Un comando slash como /reset (ver slashCommandsPlugin) siempre gana sobre una conversación activa — los usuarios nunca quedan atrapados en un flujo.
Siguientes pasos
- Plugin: rate limiter — limitar mensajes
- Flujos — guía completa del DSL