2. Start an Agent from Slack
A Slack trigger starts an agent from a Slack message. In this page, a person mentions Autoheal in a channel with a pull request link. The PR Review agent reviews the pull request, and Autoheal puts the review in the Slack thread.
Before you start
- Your organization must have a Slack Integration Connection. See Slack.
- Invite Autoheal to the channel that you use. The channel must belong to your project. See Route channels to projects.
- Find the channel ID. In Slack, open the channel details. The channel ID starts with
C.
Step 1: Read a sample Slack event
Go to Agent Studio → Triggers, click New trigger, and select On an event. In the editor, change the event to slack.app_mention. The Test fire panel shows a sample event. The CLI shows the same sample with autoheal triggers catalog --event slack.app_mention --sample:
{
"type": "slack.app_mention",
"data": {
"api_app_id": "A0AUTOHEAL",
"event": {
"channel": "C0PRODHELP",
"text": "<@U0AUTOHEAL> why did checkout latency rise?",
"ts": "1760263200.000100",
"type": "app_mention",
"user": "U0PRIYA"
},
"event_id": "Ev0MENTION01",
"team_id": "T0123ABCD",
"type": "event_callback"
}
}
This is a shorter copy of the sample. The event's data is the envelope that Slack sends. The message is under event:
| Path | Value |
|---|---|
event.text | The message text. The mention of Autoheal is <@app-user-id>. Slack puts links in angle brackets, for example <https://github.com/...>. |
event.channel | The channel ID. |
event.user | The Slack user ID of the author. |
event.thread_ts | The first message of the thread, when the mention is a reply in a thread. |
team_id | The Slack workspace. |
Step 2: Write the trigger
Replace the content of the editor with this definition. Change C0PRODHELP to your channel ID:
apiVersion: triggers.autoheal.ai/v1
name: Review pull requests from Slack
on:
slack.app_mention:
- event.channel: C0PRODHELP
- event.text:
matches: 'github\.com/[^/]+/[^/]+/pull/\d+'
run:
agent: agents:pr-review
with:
prompt: "Review the pull request in this Slack message: ${{ event.data.event.text }}"
limits:
max_con: 2
rate_per_min: 5
| Part | What it does |
|---|---|
event.channel | Only mentions in this channel start the agent. |
event.text with matches | Only messages with a pull request link start the agent. A regular expression in single quotes keeps the backslashes. |
${{ event.data.event.text }} | Puts the Slack message in the prompt. The agent finds the link in the text. |
limits | A maximum of 2 runs at the same time, and 5 new runs in one minute. A message over a limit does not wait. |
Step 3: Test the trigger
In the Test fire panel, keep Schema sample. The Dry run result shows No match, because the sample text has no pull request link. The failed filter is on.slack.app_mention[1], the second filter.
Change the sample's event.text to <@U0AUTOHEAL> review <https://github.com/autohealai/astronomy-shop-template/pull/1>, and use your channel ID in event.channel. Check that the result is now Match, and that the prompt has the link.
Click Save. To start a real run with this sample, click Trigger a live run. The run starts, but nothing goes to Slack, because the sample did not come from Slack.
Step 4: Mention Autoheal in Slack
In your channel, send @Autoheal review https://github.com/autohealai/astronomy-shop-template/pull/1. Autoheal starts the agent and posts a "Working on it…" message in a thread, with a Stop button. When the run ends, Autoheal replaces that message with the agent's final answer.
Check that the review is in the thread. In Autoheal, open Runs. Check that the run shows that the trigger started it.
How the agent replies
The agent does not need a Slack tool to reply. Autoheal posts the final answer of the run in the thread. If the run fails, the thread shows why. If the run asks for an approval or a question, a card shows in the thread. Mention Autoheal again in the same thread to send the agent a follow-up.
To make the agent do more in Slack, give it Slack tools with integration_tools. These are the Slack tools:
operation_type | Tools |
|---|---|
read | slack_get_channel_history, slack_get_thread_replies, slack_get_file, slack_get_user_info, slack_get_user_by_email, slack_list_channels, slack_list_users, slack_list_usergroups, slack_test_connection |
write | slack_send_message, slack_update_message, slack_add_reaction, slack_pin_message, slack_add_bookmark, slack_create_channel, slack_archive_channel, slack_invite_users_to_channel, slack_set_channel_topic, slack_set_channel_purpose |
For example, integration_tools: integration_type == "slack" && operation_type == "read" lets the agent read the thread with slack_get_thread_replies. Give a write tool only by name. slack_send_message takes channel, text and an optional thread_ts, to reply in a thread.
Start an agent from a direct message
A direct message to Autoheal is a slack.message event. The trigger must require event.channel_type: im, or a direct message does not start it:
apiVersion: triggers.autoheal.ai/v1
name: Review pull requests from a direct message
on:
slack.message:
- event.channel_type: im
- event.text:
matches: 'github\.com/[^/]+/[^/]+/pull/\d+'
run:
agent: agents:pr-review
with:
prompt: "Review the pull request in this Slack message: ${{ event.data.event.text }}"
What to know
- One message starts one agent. If more than one trigger matches a message, no agent starts. Autoheal lists the triggers in the thread, and the person picks one with
/agent <name> <request>. - After a trigger starts an agent in a thread, the thread belongs to that agent. More mentions in the thread go to the same run. They do not start the trigger again.
- Autoheal checks the Slack triggers of every project in the organization, not only the triggers of the channel's project. Use the
event.channelfilter to limit a trigger to your channels. - A message that no trigger matches goes to the Assistant.
- At this time,
slack.messagetriggers start only from direct messages to Autoheal. Other channel messages, for example alerts from other bots, do not start them. - Autoheal never starts a trigger from its own messages.
From the CLI
autoheal triggers catalog --event slack.app_mention --sample > mention.json
autoheal triggers dry-run -f slack-trigger.yaml --event mention.json
autoheal triggers create -f slack-trigger.yaml
autoheal triggers fire review-pull-requests-from-slack --event mention-pr.json
autoheal triggers runs review-pull-requests-from-slack
autoheal agents tools pr-review
mention-pr.json is the sample with a pull request link in event.text. fire starts a real run with the sample, and refuses a sample that the filters do not match. agents tools shows which Slack tools the agent gets.
See Filters and Passing event data to the agent in the Trigger reference, and Slack.