slackapi / bolt-python

A framework to build Slack apps using Python
https://tools.slack.dev/bolt-python/
MIT License
1.07k stars 246 forks source link

unresponsiveness observed sometime while using app mention #1120

Closed SnghDaman closed 3 months ago

SnghDaman commented 3 months ago

(Filling out the following details about bugs will help us solve your issue sooner.)

Reproducible in:

pip freeze | grep slack
python --version
sw_vers && uname -v # or `ver`

The slack_bolt version

slack-sdk==3.26.1 slack_bolt==1.19.0

Python runtime version

python 3.12

OS info

Linux ubunto

Steps to reproduce:

(Share the commands to run, source code, and project settings (e.g., setup.py))

  1. first step while we run our app is calling the app_mention function
@app.event("app_mention")
def mention_handler(body, say, ack):

    ack()

    print(f"the body event text is : {body['event']['text']}")
    thread_ts = body['event']['ts']
    print(thread_ts)

    jsonpath_expr = parse("$.event.blocks[*].elements[*].elements[*].text")
    for reply_data in jsonpath_expr.find(body):
        print("*** message is " + reply_data.value)
        msg = reply_data.value

        # Post a temporary message saying "I am working on your request!.."
        try:
            working_message = client.chat_postMessage(
                channel=body['event']['channel'],
                text="Stay tuned, I'm working on your request... :hourglass_flowing_sand:",
                thread_ts=thread_ts
            )
            # Extract the timestamp of the working message
            working_message_ts = working_message['ts']
            print(working_message_ts)
        except SlackApiError as e:
            print(f"Error posting working message: {e.response['error']}")
            return

        # Make the API request and get the actual response
        response = send_post_request(msg, api_url)
        print(f"response returned from send post request {response}")
def send_post_request(msg_value, api_endpoint):
    api_url = api_endpoint

    # Payload for the POST request
    payload = {
        "query": msg_value,
        "channel": "#",
        "workflow": "#"
    }

    headers = {
        'Content-Type': 'application/json'
    }

    response = requests.post(api_url, json=payload, headers=headers, verify=False)

Expected result:

We expect that when we mention or call the bot using @bot_name and ask a query, it should reply to all messages. For example:

Message: @botname hello Reply: chat_postMessage in the same thread -> Hello, my name is ...

Actual result:

Message: @botname hello no replies sometime

Requirements

Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you are agreeing to those rules.

seratch commented 3 months ago

Hi @SnghDaman, thanks for asking the question.

It seems your code has a few print statements. When you mention the bot user, do you observe the print outputs on your console? If not, your app_mention event subscription might not be properly configured (or re-installing the app might be necessary if you haven't done it). If you see the outputs but the chat.postMessage API call is missing, your code might not be working as expected for some reason.

If you mean some of the mentions are ignored by your app, there are a few possibilities:

I'd recommend narrowing down the possible cause by checking the request logs to your app, enabling debug-level logging, and adding more debug prints. During the debugging process, please feel free to ask questions whenever you find something unclear or unexpected on the Bolt framework side. We are unable to assist with your infrastructure settings or app settings, but we're happy to share information about this SDK!

SnghDaman commented 3 months ago

Thank you so much, @seratch. We reinstalled our app but hadn't revoked the app-level token initially. After refreshing the app-level token, everything worked smoothly. However, I have one question: when we send multiple questions to our application simultaneously, we see the temporary message getting posted for only one question rarely, but the API endpoint response doesn't come through, and nothing appears in the app-level logs. How can we rectify such issues?

seratch commented 3 months ago

If you mention the bot twice, you will receive two event delivery for sure and usually there is no race condition. Your question seems to be associated to how you implement your app.

SnghDaman commented 3 months ago

i have mentioned the bot about 10 times in 10-15 secs and observed like out of 10 one message shows me temporary message as per the above code i shared like ("stay tuned, working on your request"). but doesn't get a response from the llm api, i can rectify this at my end . but at least i should see that temporary message logged in the app logs, so does it have to do anything with slack sdk app_mention event like the rate limit or something?

seratch commented 3 months ago

does it have to do anything with slack sdk app_mention event like the rate limit or something?

No, I don't think so. This level of traffic is totally fine. I guess some parts of your code might be causing the unexpected behavior.

SnghDaman commented 3 months ago

thanks @seratch , also how many concurrent messages this app would handle at one time, can you please tell?

seratch commented 3 months ago

If your app receives other events at a high volume, this rate limit may affect it:

Event deliveries to your server via the Events API are rate limited to 30,000 deliveries per workspace per hour. https://api.slack.com/apis/rate-limits#events

If your app subscribes only to app_mention events, I am sure that there is no limit for concurrency.

SnghDaman commented 3 months ago

Awesome thanks for the help

seratch commented 3 months ago

Le me close this issue now, but if you have any questions about the SDK, please feel free to write in at any time!