pythonslackslack-apibolt

Slack API Events are not registering in our application


Hello Fellow Slack Bot Enthusiasts.

It's my first time to start setting up a Slack Bot (and I don't have a lot of dev experience, so I wanted to inquire!) . For this, I used python and Slack bolt.

Background

I was trying to setup my Amazon EC2 Web Instance's Load Balancer to accept slack events from my Workspace. In the below photo, I am now able to have my Endpoint URL Verified in the Slack bot.

The URL I used is now Verified.

Next, I am trying to follow the instructions listed in the Slack Bolt homepage that told me to create the app.py test file. So I followed through on the instructions and ensured that I subscribed to below events:

Then I created the sample app.py file from the article, that was supposed to handle the app_home_opened event.

Now I tried to run the application in my command line (I added the section for @app.event(app_home_opened), and all the other required events ), and I tried to trigger the app_home_opened, message.channels, message.im events, by opening the home page of my bot and by dm-ing the bot and inviting it to a channel and trying to talk to it - but I don't seem to be receiving any payload in the back-end.

Running the test bolt app

I wanted to inquire about the "Verified" notification from the Slack Bot. Does this ensure that the connection between my chatbot code and the server are actually linked?

In addition, I wanted to inquire about ways to test this so that I can ensure that the connection is actually working as expected. If you could share some thoughts about what I can do to test the communication, it would be much appreciated. Thank you!

Summary

TLDR:

Problem: My chatbot is not receiving any payload from Slack. Expected: I think there should be some interaction saying HTTP / 200 response to indicate that it is OK, etc. Actual: The chatbot just remains the same saying "⚡Bolt App is Running"

What I've tried:

Sample Code:

import os
# Use the package we installed
from slack_bolt import App

# Initializes your app with your bot token and signing secret
app = App(
    token=os.environ.get("SLACK_BOT_TOKEN"),
    signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
)

# Add functionality here
# @app.event("app_home_opened") ...
# added some of the code from the guide here and the other @app.events ("")


# Start your app
if __name__ == "__main__":
    app.start(port=int(os.environ.get("PORT", 3000)))

Solution

  • slack_bolt, for some reason that escapes me, does not seem to create the event handler endpoint (/slack/events) and instead seems to prefer running the application in socket mode. It may still be worth looking into socket mode (see SocketModeHandler) but this example from slack_bolt's github repo ended up getting me to my solution: https://github.com/slackapi/bolt-python/blob/main/examples/flask/app.py

    The short-ish version is you have to utilize slack_bolts's flask adapter to create a SlackRequestHandler then create a flask app, define the slack events endpoint (/slack/events) for the flask app, and pass the result to the bolt SlackRequestHandler (Events Endpoint -> Flask -> SlackRequestHandler -> Bolt):

    import os
    from flask import Flask, request
    
    from slack_bolt import App
    from slack_bolt.adapter.flask import SlackRequestHandler
    
    bolt_app = App(
        token=os.environ.get("SLACK_BOT_TOKEN"),
        signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
    )
    flask_app = Flask(__name__)
    handler = SlackRequestHandler(bolt_app)
    
    # Add functionality here
    # @bolt_app.event("app_home_opened") ...
    # added some of the code from the guide here and the other @bolt_app.events ("")
    
    @flask_app.route("/slack/events", methods=["POST"])
    def slack_events():
        return handler.handle(request)
    
    if __name__ == "__main__":
        flask_app.run()
    

    Hope this helps, cheers!