1
Create the Slack app
In Slack’s app dashboard, create an app from scratch, then set it up:
- OAuth & Permissions: add your redirect URI, such as
https://slackbot.example.com/oauth/callback, and the bot token scopesapp_mentions:readandchat:write. - Basic Information: copy the client ID, client secret, and signing secret.
channels:history to watch every message in a channel, or im:history to watch direct messages.Then set the app’s settings as environment variables for your agent:2
Declare the connection and trigger
slackConnection() (slack_connection() in Python) reads the client ID, secret, and redirect URI from the environment. It takes the bot token’s scopes under bot, and a user token’s scopes under user if you need a user token. It also takes a token store, which here is store.slackTriggers() (slack_triggers() in Python) takes the connection and returns the ready-made triggers, and the skill picks the ones it offers:3
Reply to each mention
Each run’s
task.event.data holds the message’s channel, ts, thread_ts, user, text, and team. The bot token is the connection’s default, so task.credentials(slack) returns it:4
Serve Slack's requests
Your server answers two kinds of requests from Slack:
- Events, such as mentions. Pass each one to
receiveEvents()withslackEvents()(receive_events()andslack_events()in Python). Slack signs the raw body, so pass the body exactly as it arrived. - OAuth callbacks at the redirect URI, once a human installs the app. Pass each one to
receiveOAuth()(receive_oauth()in Python), which stores the workspace’s tokens and returns a page for the human.
5
Subscribe the app to events
With your server running, open Event Subscriptions in the app dashboard, turn events on, and set the Request URL, such as
https://slackbot.example.com/slack/events. slackEvents() answers Slack’s URL check for you.Subscribe to the bot events app_mention, app_uninstalled, and tokens_revoked. Add message.channels for the message trigger, or message.im for directMessage.6
Schedule the trigger
A requester schedules the trigger by name. The requester’s human installs the app through the OAuth link, and from then on each mention starts a run:
Available Slack triggers
Every schedule watching an event gets its own run.
slackEvents() leaves out the app’s own messages, and any message edits or deletions.
Uninstalls and revoked tokens
When a workspace uninstalls the app or revokes its tokens,receiveEvents() ends every grant of that workspace. The tasks and schedules using those grants end with the code connection_revoked, as described in Revoked access.
Other Slack events
To run schedules on other Slack event types, such asreaction_added, pass a map function to slackEvents(). The function receives the event and the workspace’s id, and returns the event’s keys. An event with no keys is dropped. Then write a push trigger whose hook returns the same keys.
Adjust the OAuth flow
slackProfile (slack_profile in Python) is the profile Slack connections use. To change one of its hooks, such as to turn on PKCE, see Adjust a platform profile.