'Slack app not responding to message event python

def event_handler(event_type, slack_event):
    channel = slack_event["event"]["channel"]
    string_slack_event = str(slack_event)
    thread_ts=slack_event["event"]["ts"]
 
    if string_slack_event.find("{'type': 'user', 'user_id': ") != -1:
        try:
            if event_type == "message" or event_type=="app_mention":
                user_query = slack_event['event']['blocks'][0]['elements'][0]['elements'][1]['text']
                user_id = slack_event['event']['user']
                answer = action(user_id,user_query)
                result = client.chat_postMessage(channel=channel,
                                                 text=answer)
            return make_response("ok", 200, )
        except IndexError:
            pass
 
    message = "[%s] cannot find event handler" % event_type
 
    return make_response(message, 200, {"X-Slack-No-Retry": 1})

@app.route('/', methods=['POST'])
def hello_there():
    slack_event = json.loads(request.data)

    if "challenge" in slack_event:
        return make_response(slack_event["challenge"], 200, {"content_type": "application/json"})
 
    if "event" in slack_event:
        event_type = slack_event["event"]["type"]
        return event_handler(event_type, slack_event)
    return make_response("There are no slack request events", 404, {"X-Slack-No-Retry": 1})

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

    app.run(host='0.0.0.0')

I'm trying to build a slack app which responds to message, but I guess slack_event doesn't happen when the message is written without mentioning the app. What should I do to notice the app when the message post from user happens?



Solution 1:[1]

I am using like this in my project:

text = "return message: " + text

try:
    response = client.chat_postMessage(
        channel='#channel1',
        text=text)
except SlackApiError as e:
    # You will get a SlackApiError if "ok" is False
    assert e.response["ok"] is False
    assert e.response["error"]  # str like 'invalid_auth', 'channel_not_found'
    print(f"Got an error: {e.response['error']}")

Maybe you can add else parts into your code and try again.

    if string_slack_event.find("{'type': 'user', 'user_id': ") != -1:
        try:
            if event_type == "message" or event_type=="app_mention":
                user_query = slack_event['event']['blocks'][0]['elements'][0]['elements'][1]['text']
                user_id = slack_event['event']['user']
                answer = action(user_id,user_query)
                result = client.chat_postMessage(channel=channel,
                                                 text=answer)
                return make_response("ok", 200, )
            else:
                return "Event type is not message or app_mention"

        except IndexError:
            pass
     else:
         print("Else part")   

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 uozcan12