Skip to main content
The Chat class is the central orchestrator that manages adapters, event handlers, and webhook processing.

Constructor

ChatConfig<TAdapters>
required
Configuration object for the Chat instance
Chat<TAdapters, TState>
Initialized Chat instance with type-safe adapter access and webhook handlers

Example

Properties

webhooks

Type-safe webhook handlers keyed by adapter name. Each webhook handler processes incoming requests from the platform.

Event Handler Methods

onNewMention()

Register a handler for new @-mentions of the bot in unsubscribed threads only.
(thread: Thread<TState>, message: Message) => void | Promise<void>
required
Handler called when the bot is @-mentioned in an unsubscribed thread
This handler is ONLY called for mentions in unsubscribed threads. Once a thread is subscribed via thread.subscribe(), subsequent messages (including @-mentions) go to onSubscribedMessage handlers instead.

onNewMessage()

Register a handler for messages matching a regex pattern in unsubscribed threads.
RegExp
required
Regular expression to match against message text
(thread: Thread<TState>, message: Message) => void | Promise<void>
required
Handler called when pattern matches

onSubscribedMessage()

Register a handler for all messages in subscribed threads.
(thread: Thread<TState>, message: Message) => void | Promise<void>
required
Handler called for all messages in subscribed threads
Does NOT fire for:
  • The message that triggered the subscription (e.g., the initial @mention)
  • Messages sent by the bot itself

onReaction()

Register a handler for reaction events (emoji added/removed).
Array<EmojiValue | string>
Optional array of emoji to filter. Empty or omitted means all emoji.
(event: ReactionEvent) => void | Promise<void>
required
Handler called when reaction is added/removed

onAction()

Register a handler for action events (button clicks in cards).
string | string[]
Optional action ID(s) to filter. Empty or omitted means all actions.
(event: ActionEvent) => void | Promise<void>
required
Handler called when button is clicked

onSlashCommand()

Register a handler for slash command events.
string | string[]
Optional command(s) to filter (e.g., “/help” or [“help”, “/help”]). Empty or omitted means all commands.
(event: SlashCommandEvent<TState>) => void | Promise<void>
required
Handler called when command is invoked

onModalSubmit()

Register a handler for modal/dialog form submissions.
string | string[]
Optional callback ID(s) to filter. Empty or omitted means all modals.
(event: ModalSubmitEvent) => void | Promise<ModalResponse | undefined>
required
Handler called when modal is submitted. Can return ModalResponse to update/close the modal.

onModalClose()

Register a handler for modal close/cancel events.
string | string[]
Optional callback ID(s) to filter. Empty or omitted means all modals.
(event: ModalCloseEvent) => void | Promise<void>
required
Handler called when modal is closed/cancelled

Utility Methods

initialize()

Manually initialize the chat instance and all adapters. This is called automatically when handling webhooks, but can be called manually for non-webhook use cases (e.g., Gateway listeners).

shutdown()

Gracefully shut down the chat instance and disconnect from the state backend.

getAdapter()

Get an adapter by name with type safety.
K extends keyof TAdapters
required
Adapter name (key from the adapters config)
TAdapters[K]
The adapter instance

openDM()

Open a direct message conversation with a user. The adapter is automatically inferred from the userId format.
string | Author
required
Platform-specific user ID string, or an Author object from message.author or event.user
Thread<TState>
A Thread that can be used to post messages
User ID Formats:
  • Slack: U... (e.g., “U00FAKEUSER1”)
  • Teams: 29:... (e.g., “29:198PbJuw…”)
  • Google Chat: users/... (e.g., “users/100000000000000000001”)
  • Discord: numeric snowflake (e.g., “1033044521375764530”)

channel()

Get a Channel by its channel ID. The adapter is automatically inferred from the channel ID prefix.
string
required
Channel ID (e.g., “slack:C123ABC”, “gchat:spaces/ABC123”)
Channel<TState>
A Channel that can be used to list threads, post messages, etc.

reviver()

Get a JSON.parse reviver function that automatically deserializes Thread, Channel, and Message objects.
(key: string, value: unknown) => unknown
Reviver function for JSON.parse

registerSingleton()

Register this Chat instance as the global singleton. Required for Thread deserialization via @workflow/serde.
Chat<TAdapters, TState>
Returns the Chat instance for chaining

Static Methods

getSingleton()

Get the registered singleton Chat instance. Throws if no singleton has been registered.

hasSingleton()

Check if a singleton has been registered.

Type Parameters

Record<string, Adapter>
default:"Record<string, Adapter>"
Map of adapter names to Adapter instances for type-safe webhook access
object
default:"Record<string, unknown>"
Custom state type stored per-thread and per-channel

See Also