Reputation: 4309
I have created a slack app. I'm able to read and respond to slash commands. However, the application cannot read messages. When I issue a slash command I see a web server event like this:
127.0.0.1 - - [19/Jan/2021 13:11:07] "POST /slack/events HTTP/1.1" 200 -
However, when a text message is entered into the chat there is no log on my webserver. I believe a slack message should trigger an event that sends data to my API. Note: I am using the event API.
I'm using Bolt for slack.
I'm assuming the permissions for this are in the Event Subscriptions settings under Subscribe to bot events. In this case I have only enabled app_home_opened
.
There is an option for channels:read
and channels:history
. They are both bold and not able to be added. I assume this means they are enabled by default.
Here is a simplified application that responds to /list
but doesn't respond to hello
:
import os
from slack_bolt import App
# Initializes the app
app = App(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
)
# Respond to hello
@app.message("hello")
def say_hello(message, say):
say("hi")
# List all users
@app.command("/list")
def list_users(ack, say, command):
ack()
say("list users")
# Start your app
if __name__ == "__main__":
app.start(port=int(os.environ.get("PORT", 3000)))
I believe that channels:history
is the permission I need to view the chat content. Why can't I read and respond to messages?
This is the documentation I'm trying to refer to for the events API.
Upvotes: 4
Views: 3114
Reputation: 1
Create a route to receive slack events.
from slack_bolt import App
from slack_bolt.adapter.flask import SlackRequestHandler
from flask import Flask, request
import os
app=App(token=os.environ.get("SLACK_ENTRI_LUNCH_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
)
flask_app = Flask(__name__)
handler = SlackRequestHandler(app)
@app.message("hello")
def message_hello(message, say):
# say() sends a message to the channel where the event was triggered
say(f"Hey there !")
@flask_app.route("/slack/events", methods=["POST"])
def slack_events():
if request.headers["Content-Type"] == "application/json":
data = request.json
if "challenge" in data:
return data.get("challenge"),200
return handler.handle(request)
else:
return "",200
if __name__=="__main__":
flask_app.run(port=3000, debug=True)
Go to the slack app and in Interactivity & Shortcuts and add this url with endpoint slack/events into it. Make sure the server is up when slack verifies the url. Once the challenge gets verified slack is ready to send events to the given url.
Now when an event occurs, event is sent to your specified url and according to the above code SlackRequestHandler handles the request. Make sure SlackRequestHandler handler handles the request,only then your @app.message() will be executed.
Also give necessary permissions to the app in bot events and interactivity. Make sure both is on.
Refer slack docs for more clearer explanation.
Thanks!
Upvotes: 0
Reputation: 4309
I was missing the message.channels permission under the Application Settings > Event Subscriptions > Subscribe Bot to Events.
Upvotes: 7