app_mention event not triggered when bot posts @mention in a thread via chat_postMessage
When the bot posts a message containing an @mention of itself in a thread using chat_postMessage, the app_mention event is not triggered.
Reproduction Steps:
The bot listens for the followup button action. In the action handler, the bot posts a message with an @mention of itself in the same thread, e.g.:
await client.chat_postMessage(
channel=channel_id,
text=f"<@{bot_id}> {follow_up_question}",
thread_ts=ts
)
Expect the app_mention event handler to trigger for this new message. However, the app_mention event is never triggered.
Expected Behavior:
Posting a new message mentioning the bot (even inside a thread) should trigger the botβs app_mention event handler.
Actual Behavior:
No app_mention event is received by the bot when the message is posted in a thread.
Environment:
- slack_bolt==1.23.0
- slack_sdk==3.36.0
- Bolt for Python (async)
- Slack App with necessary permissions and subscriptions
- Slack workspace where the bot is installed
Notes & Checks performed:
- Verified that the bot is subscribed to app_mention events in Slack App's event subscriptions.
- Confirmed the bot has app_mentions:read and chat:write scopes.
- Tested posting the mention as a top-level message (without thread_ts) β the app_mention event does not trigger
- Concluded that Slack does not trigger app_mention events for mentions inside and outside thread replies.
Sample Code
import asyncio
import json
from slack_bolt.async_app import AsyncApp
from slack_bolt.adapter.socket_mode.async_handler import AsyncSocketModeHandler
from slack_sdk import WebClient
Slack_app_token = "xapp..."
Slack_bot_token = "xoxb..."
client = WebClient(token=Slack_bot_token)
app = AsyncApp(token=Slack_bot_token)
@app.event("app_mention")
async def bot_mention(event, body, say, client, logger, ack):
await ack()
print("@app_mention done")
print(body)
# user_id = event["user"]
message_channel = event["channel"]
thread = body['event']['event_ts']
await client.chat_postMessage(
channel=message_channel,
text="Followup block",
thread_ts=thread,
blocks=[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Need clarification? Try one of these questions:*\n1. What are some examples of general-purpose EC2 instance types?\n2. How does Compute Optimizer determine right-sizing recommendations?\n3. What happens to my data if I donβt delete the EBS volume before terminating an EC2 instance\n\n*Choose a question below to get started:*"
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Question 1"
},
"value": "What are some examples of general-purpose EC2 instance types?",
"action_id": "followup"
}
]
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Question 2"
},
"value": "How does Compute Optimizer determine right-sizing recommendations?",
"action_id": "followup"
}
]
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Question 3"
},
"value": "What happens to my data if I don't delete the EBS volume before terminating an EC2 instance?",
"action_id": "followup"
}
]
}
]
)
@app.action("followup")
async def followup(event, body, say, client, logger, ack):
await ack()
print(f"followup body = {body}")
bot_id = body['message']['user']
follow_up_question = body['actions'][0]['value']
ts = body['message']['ts']
channel_id=body['channel']['id']
follow_up_query = f"<@{bot_id}> {follow_up_question}"
await client.chat_postMessage(
channel=channel_id,
text=follow_up_query,
thread_ts=ts
)
async def main():
handler = AsyncSocketModeHandler(app, Slack_app_token)
await handler.start_async()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Keyboard Interruption")
Hey @PrajwalNaik07! π Thanks for writing in with a question πΎ β¨
At a glance this might be expected behavior - Bolt apps ignore "self" events as a default. As a first step I'd recommend setting the ignoring_self_events_enabled to false and restarting your app based on these docs:
ignoring_self_events_enabled
False if you would like to disable the built-in middleware (Default: True).
AsyncIgnoringSelfEventsis a built-in middleware that enables Bolt apps to easily skip the events generated by this app's bot user (this is useful for avoiding code error causing an infinite loop).
π https://docs.slack.dev/tools/bolt-python/reference/app/async_app.html
Hi @zimeg,
I tried:
app = AsyncApp(token=Slack_bot_token, ignoring_self_events_enabled=False)
Still when the followup action posts a message with @app_mention to the bot app_mention event is not triggered.
The current bot scopes of my application are:
- app_mentions:read
- chat:write
- im:write
- users.profile:read
- users:read
Is it an issue with the scopes?
@PrajwalNaik07 I'm still not certain of the issue but a few more troubleshooting steps come to mind:
- Is the bot added to the channel where mentions are sent?
- Are mentions received when these are sent by you?
- Is the conversation used in testing a direct message?
The last point references related docs but something else still might be happening... Do let me know!
Messages sent to your app in direct message conversations are not dispatched via
app_mention, including messages sent from other apps, regardless of whether your app is explicitly mentioned or otherwise.
π https://docs.slack.dev/reference/events/app_mention/
- Yes the bot is added to the channel where mentions are sent.
- Yes, mentioned are received when we send them.
- No we are sending the message in thread thought
client.chat_postMessage
@PrajwalNaik07 Hmm... I notice Socket Mode is being used also. Is it possible that multiple connections are running, perhaps some in the background?
We sometimes find this can cause outdated code to continue running and might cause some events to be handled in an unexpected fashion ποΈβπ¨οΈ β¨
Hi @zimeg ,
No we are running the bot in Kubernetes as a deployment and running it on a single pod with the latest image. So there is no chance of multiple connections
π It looks like this issue has been open for 30 days with no activity. We'll mark this as stale for now, and wait 10 days for an update or for further comment before closing this issue out. If you think this issue needs to be prioritized, please comment to get the thread going again! Maintainers also review issues marked as stale on a regular basis and comment or adjust status if the issue needs to be reprioritized.
As this issue has been inactive for more than one month, we will be closing it. Thank you to all the participants! If you would like to raise a related issue, please create a new issue which includes your specific details and references this issue number.
Hi there π
It looks like we are likely filtering out the app_mention event on the backend when posted by the same bot user being mentioned. We're looking into whether this is the intended behaviour, or a bug
π Hi @PrajwalNaik07,
It looks like we intentionally do not send an app_mention event when a bot mentions itself as this could create an infinite loop. Could you let us know what your use case was for this functionality?
π It looks like this issue has been open for 30 days with no activity. We'll mark this as stale for now, and wait 10 days for an update or for further comment before closing this issue out. If you think this issue needs to be prioritized, please comment to get the thread going again! Maintainers also review issues marked as stale on a regular basis and comment or adjust status if the issue needs to be reprioritized.
As this issue has been inactive for more than one month, we will be closing it. Thank you to all the participants! If you would like to raise a related issue, please create a new issue which includes your specific details and references this issue number.