Search code examples
pythonflaskslackslack-api

Slack Events adapter not getting triggered


I am trying to build a basic slack bot but the bot is not picking up the message event. Have added the following scopes: "chat:write"

Have also subscribed for the event message.channels.

I have configured ngrok server. When I hit the endpoint from my browser I get a message saying "these are not the slack bots you are looking for" which means the server is running. But when I post a message in the channel in which the bot is added, the API is not triggered. can you please help with this?

Here is the code that I'm using:

import slack_sdk
import os
from pathlib import Path
from dotenv import load_dotenv
from flask import Flask
from slackeventsapi import SlackEventAdapter

env_path = Path('.') / '.env'
load_dotenv(dotenv_path = env_path)

app = Flask(__name__)
slack_event_adapter = SlackEventAdapter(os.environ['SIGNING_SECRET'],'/slack/events',app)

client = slack_sdk.WebClient(token = os.environ['SLACK_TOKEN'])

# client.chat_postMessage(channel = '#bi-announcements', text = "Hello World! ")

BOT_ID = client.api_call("auth.test")['user_id']

@slack_event_adapter.on('message')
def message(payload):
    print(payload)
    event = payload.get('event',{})
    channel_id = event.get('channel')
    user_id = event.get('user')
    text = event.get('text')

    if BOT_ID != user_id:
        client.chat_postMessage(channel= channel_id, text = text)

if __name__ == "__main__":
    app.run(debug=True)

Here are the slack configurations:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

I'm able to send messages to slack but not able to read the messages. Any help would be greatly appreciated.


Solution

  • As discussed in comments, there was a rare occurrence that the created app somehow was faulty.

    Creation of a new app with same configuration solved the issue.