Repository navigation
trigger event when slack bot is added to a channel #710
Description
Activity
- addedquestionFurther information is requestedFurther information is requested
on Aug 25, 2022 Hi @jgframoz, thanks for asking the question!
The "bot_added" events are only available in RTM API, which is a legacy way to connect to Slack platform. With Bolt framework, you can use Events API instead and "member_joined_channel" events are available for the same purpose.
Something like this should work for you:
@app.event("member_joined_channel") def handle_member_joined_channel_events(event, client, say): joined_user_id = event.get("user") bot_user_id = client.auth_test().get("user_id") if joined_user_id == bot_user_id: say("Hey, I am here to help you :wave:")
I hope this was helpful to you.
Reacted by Damien Storm and Burkhard ReffelingReacted by João Ramos@seratch that's super useful, thank you!
I'll try it and give you a follow up 🙏Hello @seratch, I'm working with @jgframoz on this project :)
I tried that approach and it didn't work, the event does not fire.
I did manage to add a Flask adapter to capture all events, and the event that fired was of type
messageand subtypechannel_join. Here is the body of that request:{ "token": "XXXXX", "team_id": "TXXXX", "api_app_id": "AXXXXX", "event": { "type": "message", "subtype": "channel_join", "ts": "1661602966.006369", "user": "UXXXXX", "text": "<@UXXXXX> entrou no canal", "inviter": "UXXXXX", "channel": "CxXXXXX", "event_ts": "1661602966.006369", "channel_type": "channel" }, "type": "event_callback", "event_id": "Ev040Q7SHXDW", "event_time": 1661602966, "authorizations": [ { "enterprise_id": None, "team_id": "TXXXX", "user_id": "UXXXXX", "is_bot": True, "is_enterprise_install": False } ], "is_ext_shared_channel": False, "event_context": "4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDNNRTA3RlkyMiIsImFpZCI6IkEwM1VWQk0zNkY4IiwiY2lkIjoiQzA0MDBHU1FSTVkifQ" }But I tried to forward that to the event handle
@app.event({ 'type': 'message', 'subtype': 'channel_join' }) def handle_message_events(body, logger): print('channel join!!!!!!') logger.info(body)like bellow and it didn't work
from flask import Flask, request from slack_bolt.adapter.flask import SlackRequestHandler flask_app = Flask(__name__) app = App( token=os.environ.get('SLACK_BOT_TOKEN'), signing_secret=os.environ.get('SLACK_SIGNING_SECRET') ) handler = SlackRequestHandler(app) @flask_app.route("/slack/events", methods=["POST"]) def slack_events(): return handler.handle(request)Can you please explain what am I doing wrong?
Thank you 😄
Reacted by Burkhard ReffelingYour app needs to subscribe to member_joined_channel events as well.
Amazing, thank you!! 🙏
@seratch Thank you for all the help! You can close this issue now! 🙏
I'm trying to use a slack bot to through a conversation finding specific messages. But I also want to process it when the bot is added to a new channel/conversation.
I already developed my logic using a command and works fine but now I wanted to switch to an event based action (just once when the bot enters a new channel)
I tried something simple like:
When I add the bot to a new channel, I get a 200 response but not message saying 'helloooooo' nor a print saying 'hello'
This is the standard log response:
"POST /slack/events HTTP/1.1" 200