Handling events

Every platform webhook is normalised into one of five event kinds before your bot sees it. Implement only the handlers you need.

Handler Fires on
onMessage A user posted a message the bot can see.
onTrigger A button was clicked or a select option chosen.
onCommand A slash command was invoked.
onReaction A reaction was added or removed.
onMembership Someone joined or left; event.isSelf marks the bot itself.
onEvent Catch-all, only for events with no specific handler.
onError A handler threw. Without it, failures are logged and silent.

Staying quiet in busy channels

onMessage fires for every visible message, not only ones addressed to the bot. Gate on isAddressedToBot, which is true for an @-mention, any direct message, a slash command or a button click:

import { isAddressedToBot } from "@wingman/core";

async onMessage(event, conversation) {
  if (!isAddressedToBot(event)) return;
  …
}

Try it: How do I stop the bot answering every message in a channel?

What a handler gets

The second argument is a Conversation — already addressed at the right channel, thread and user:

conversation.reply(msg)            // reply where the event came from
conversation.replyInThread(msg)
conversation.replyEphemeral(msg)
conversation.react("eyes")         // react to the triggering message
conversation.transcript(20)        // recent messages, oldest last
conversation.profile()             // the triggering user's profile
conversation.state.get("step")     // per-conversation scratch space
conversation.canSendButtons       // capability check

Greeting on install

async onMembership(event, conversation) {
  if (event.change === "joined" && event.isSelf) {
    await conversation.reply("Hello! Mention me with a question any time.");
  }
}

Ack-then-process

The runtime returns the platform's acknowledgement before running your handler. Slack retries any webhook not acknowledged within three seconds, and an AI answer takes longer than that — so handlers run under waitUntil and may take as long as the Worker's budget allows. A consequence: your handler cannot change the HTTP response, because it has already been sent.

Retried deliveries are de-duplicated by event id. Bind a KV namespace named BOT_STORE to make that reliable across isolates — see Deploying a bot.

Related: Sending messages, Capabilities.

← Sending messagesCapabilities →