Skip to content

trigger event when slack bot is added to a channel #710

Description

@jgframoz

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:

@app.event('bot_added')
def bot_added(event, say):
    say('hellloooooo')
    print('hello')

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

Activity

  1. seratch commented on Aug 25, 2022

    @seratch
    Contributor

    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.

  2. jgframoz commented on Aug 26, 2022

    @jgframoz
    Author

    @seratch that's super useful, thank you!
    I'll try it and give you a follow up 🙏

  3. ArturFortunato commented on Aug 27, 2022

    @ArturFortunato

    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 message and subtype channel_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 😄

  4. seratch commented on Aug 27, 2022

    @seratch
    Contributor

    Your app needs to subscribe to member_joined_channel events as well.

  5. ArturFortunato commented on Aug 27, 2022

    @ArturFortunato

    Amazing, thank you!! 🙏

  6. jgframoz commented on Sep 2, 2022

    @jgframoz
    Author

    @seratch Thank you for all the help! You can close this issue now! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions