Event Anatomy
All events share a commonBasePayload structure:
userId: The user who triggered the event.spaceId: The space that the event occurred in.channelId: The channel that the event occurred in.eventId: The unique event ID.createdAt: The date and time the event occurred.
Message Forwarding Modes
Bots have three message forwarding modes that control which events they receive. You configure this in your bot settings at app.towns.com/developer.Available Modes
All Messages- Bot receives every event in channels it’s in
- All event handlers are available
- Use for: AI agents, moderation bots, analytics, or when you need
onTip,onChannelJoin,onChannelLeave, oronEventRevokehandlers - ⚠️ Warning: High volume in active channels
- Bot receives: messages where it’s mentioned, replies to its messages, reactions, and slash commands
- Event handlers available:
onMessage(filtered),onSlashCommand,onReaction,onMessageEdit,onRedaction - Event handlers NOT available:
onTip,onChannelJoin,onChannelLeave,onEventRevoke - Use for: Most interactive bots that respond to direct user interaction
- Bot receives no message events
- Use for: External-only bots triggered by custom webhooks or timers
Event Listeners
All event listeners are asynchronous functions that receive ahandler and an event object.
The handler is the bot instance and the event contains the BasePayload and additional fields specific to the event type.
onMessage
Fires when a message is sent in a channel the bot is in (subject to forwarding settings).message: The decrypted message text.replyId: TheeventIdof the message being replied to (if reply).threadId: TheeventIdof the thread’s first message (if in thread).mentions: List of users mentioned in the message. Each mention contains the user’s address and display name.isMentioned: True if the bot was @mentioned.
onSlashCommand
Fires when a user invokes one of your bot’s slash commands. For more information about slash commands, see the Slash Commands guide.command: The name of the command invoked by the user.args: The arguments passed to the command.mentions: List of users mentioned in the command.replyId: The message being replied to (if any).threadId: The thread context (if any).
onReaction
reaction: The reaction emoji.messageId: TheeventIdof the message being reacted to.
onMessageEdit
Fires when a message is edited.message: The new message content.refEventId: TheeventIdof the message being edited.replyId: If the edited message was a reply.threadId: If the edited message was in a thread.mentions: Array of mentioned users in the edited message.isMentioned: True if the bot is mentioned in the edited message.
onRedaction
Fires when a message is deleted by a user.refEventId: TheeventIdof the message being deleted.
onEventRevoke
Fires when a message is revoked, either by the user that sent it or by an admin with theRedact permission.
This event handler only works in “All Messages” mode. See Message Forwarding Modes.
refEventId: TheeventIdof the message being revoked.
onTip
Fires when a user sends a cryptocurrency tip on a message.This event handler only works in “All Messages” mode. See Message Forwarding Modes.
messageId: TheeventIdof the message that received the tip.senderAddress: The address of the user who sent the tip.receiverAddress: The address of the user who received the tip.amount: The amount of the tip.currency: The currency of the tip.
onChannelJoin
Fires when users join a channel.This event handler only works in “All Messages” mode. See Message Forwarding Modes.
userId: The user who joined or left the channel.channelId: The channel that the user joined or left.
onChannelLeave
Fires when users leave a channel.This event handler only works in “All Messages” mode. See Message Forwarding Modes.
userId: The user who left the channel.channelId: The channel that the user left.
Sending events
You can use bot actions to send events to Towns Network.sendMessage
Send a message to a channel.channelId: The channel to send the message to.message: The message to send.opts: The options for the message.threadId: The thread to send the message to.replyId: The message to reply to.mentions: The users to mention in the message. Accepts a list ofuserIdanddisplayName.attachments: The attachments to send with the message.ephemeral: Whether the message should be ephemeral (wont be stored in the channel history)
eventId of the sent message.
Sending attachments
The bot framework supports four types of attachments with automatic validation and encryption.Image Attachments from URLs
Send images by URL with automatic validation and dimension detection:Miniapp Attachments
You can use it to send a miniapp. It will be rendered in the message as a miniapp.Link Attachments
You can use it to send any link.Chunked Media Attachments
Send binary data (videos, screenshots, generated images). Framework automatically encrypts and chunks (1.2MB per chunk). Video:- Uint8Array requires
mimetypeparameter - Blob has automatic mimetype
- Image dimensions auto-detected for
image/*mimetypes
sendReaction
Send a reaction to a message.channelId: The channel to send the reaction to.eventId: TheeventIdof the message to react to.reaction: The reaction to send.
eventId of the sent reaction.
Example:
editMessage
Edit a message.channelId: The channel to edit the message in.eventId: TheeventIdof the message to edit.message: The new message content.opts?: Optional options for the edit.threadId: Change the thread of the message.replyId: Change the reply of the message.mentions: Change the mentions of the message. Accepts a list ofuserIdanddisplayName.attachments: Change the attachments of the message.
eventId of the edited message.
removeEvent
Remove any event that was sent by the bot.channelId: The channel to remove the event from.eventId: TheeventIdof the event to remove.
eventId of the removed event.
adminRemoveEvent
Remove any event from a channel. Requires theRedact permission.
channelId: The channel to remove the event from.eventId: TheeventIdof the event to remove.
eventId of the redaction event.
sendTip
Send a cryptocurrency tip to a user on a message.Your gas wallet needs Base ETH to pay for gas fees, and your bot treasury wallet needs to hold the ETH that will be sent as tips. See Understanding Your Bot’s Wallet Architecture for details.
userId: The user to tipmessageId: TheeventIdof the message to tipchannelId: The channel containing the messageamount: The tip amount in wei
eventId of the tip event.
Example - tip users for helpful messages:
Permissions
Check user permissions before executing operations.hasAdminPermission
Check if user is a space admin (hasModifyBanning permission).
checkPermission
Check specific permission for a user.Permission.ReadPermission.WritePermission.RedactPermission.ModifyBanningPermission.PinMessagePermission.AddRemoveChannelsPermission.ModifySpaceSettings