Guides
Build an auto-reply bot
Combine webhooks with bot commands: the bot tells your server when someone IMs it, and your server answers through the API. This example also decides which group invites to accept.
1. Register the webhook
With the bots:callbacks scope, point the bot at your server — one URL per bot, with a secret in it. See Webhooks & events.
2. Handle the events
server.js
// npm install express
import express from 'express'
const app = express()
app.use(express.json())
const HOOK_SECRET = process.env.HOOK_SECRET // long random string
app.post('/hooks/:botId/:secret', async (req, res) => {
if (req.params.secret !== HOOK_SECRET) return res.sendStatus(404)
const { botId } = req.params
const event = req.body
// Accept group invites from people we trust - the reply is the decision
if (event.action === 'group_invite') {
return res.json({ accept: TRUSTED.includes(event.avatar_uuid) })
}
// Everything else: say "got it" first, then do the work
res.sendStatus(200)
if (event.action === 'instant_message' && event.speaker_type === 'AGENT') {
await fetch(`https://api.lifebots.cloud/api/v1/bots/${botId}/im`, {
method: 'POST',
headers: {
Authorization: `Bearer ${tokenFor(botId)}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
slname: event.speaker_uuid,
message: 'Thanks for your message! A human will reply soon.'
})
})
}
})
app.listen(3000)Notes
- Reply within 10 seconds, or the delivery counts as failed. For
group_inviteyou have 30 seconds, and your reply is the decision. - Replying to the IM needs the
bots:chatscope. - Skip messages from objects (
speaker_type: "OBJECT") so two bots don't answer each other forever. - Use
cidto ignore an event you've already handled.