Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slack is posting two/three messages with chat_postMessage #693

Closed
3 of 9 tasks
kunalrc2022 opened this issue Aug 3, 2022 · 16 comments
Closed
3 of 9 tasks

slack is posting two/three messages with chat_postMessage #693

kunalrc2022 opened this issue Aug 3, 2022 · 16 comments
Labels
question Further information is requested

Comments

@kunalrc2022
Copy link

Description

Describe your issue here.
I am using slack-bolt python framework for my slack bolt which is going through some logic. I am using chat_postMessage method to send a message from bot. But currently it is sending three messages even though it's having single API call. I read about it and notices that it can be avoided with making X-Slack-Retry-Num =1 in header.(https://api.slack.com/apis/connections/events-api#graceful_retries) But i am wondering how can i change the header of the api call in res_out = app.client.chat_postMessage(channel=message['channel'],thread_ts = original_thread_id,blocks=block_final)

What type of issue is this? (place an x in one of the [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • example code related
  • testing related
  • discussion

Requirements (place an x in each of the [ ])

  • [x ] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

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

Reproducible in:

package version:

node version:

OS version(s):

Steps to reproduce:

  1. use res_out = app.client.chat_postMessage(channel=message['channel'],thread_ts = original_thread_id,blocks=block_final) in the bot's code and due to retry mechanism it tries and sends the same message thrice on single call.

Expected result:

Bot should only post single message.

Actual result:

Bot is posting two/three same messages

Attachments:

@mwbrooks
Copy link
Member

mwbrooks commented Aug 3, 2022

Transferring this issue to slackapi/bolt-python

@mwbrooks mwbrooks transferred this issue from slackapi/bolt-js Aug 3, 2022
@mwbrooks mwbrooks added the question Further information is requested label Aug 3, 2022
@mwbrooks
Copy link
Member

mwbrooks commented Aug 3, 2022

Hi @kunalrc2022 👋🏻 Thanks for reaching out!

I've transferred this issue to slackapi/bolt-python because it looks like you're using the Python framework instead of the JavaScript framework.

Can you provide a complete code example of posting a message into Slack? Thanks for the steps to reproduce, but I think it will be helpful to understand when you're posting a message. For example, are you responding to an event or action? Some requests must be acknowledged and the Slack API will re-send the request multiple times when it's not acknowledge. This could be one reason why you're sending multiple messages.

@kunalrc2022
Copy link
Author

kunalrc2022 commented Aug 3, 2022

hey @mwbrooks thanks for looking into it.
I am using it in response to app.message event. Please refer below eg i am using in the code.

@app.message(re.compile("(TECHAWAY-)"))
def message_hello(message, ack,say):
  ack()
  ''' 
  Having code logic and if else checks
  ""
  res_out = app.client.chat_postMessage(channel=message['channel'],thread_ts = original_thread_id,blocks=block_final)

@kunalrc2022
Copy link
Author

kunalrc2022 commented Aug 3, 2022

Is there a way i can limit it's retry attempt count to 1 in the the above api call ?

@kunalrc2022
Copy link
Author

kunalrc2022 commented Aug 4, 2022

hello @mwbrooks /team team, could anybody please help me with the above message ?

@kunalrc2022
Copy link
Author

kunalrc2022 commented Aug 5, 2022

Hello @mwbrooks , @objectfox @episod @seratch hope you are doing well.
Could you please have a look at the above issue and help me to resolve it ?

@objectfox
Copy link
Contributor

@kunalrc2022 have you tried putting something into app.client.headers? Like app.client.headers['X-Slack-Retry-Num'] = 1 before the chat_postMessage call?

@kunalrc2022
Copy link
Author

kunalrc2022 commented Aug 5, 2022

Hello @objectfox , thanks for looking into this request . i have tried adding app.client.headers['X-Slack-Retry-Num'] = 1 before using chat_postMessage but still sometimes it sends multiple messages.

Here is the piece of lines i am using in the code:
app.client.headers['X-Slack-Retry-Num'] = 1
res_out = app.client.chat_postMessage(channel=message['channel'],thread_ts = original_thread_id,blocks=block_final)

@seratch
Copy link
Member

seratch commented Aug 5, 2022

@kunalrc2022

Is there a way i can limit it's retry attempt count to 1 in the the above api call ?

If your Bolt app cannot respond to message events within 3 seconds for some reason, the Slack server-side can send retry requests up to 3 times. There is no way to prevent this behavior but you can check X-Slack-Retry-Num (retry_attempt in Socket Mode) value. To do this, you can use request argument in a global middleware:

@app.use # or @app.middleware
def ignore_retry_request(request, ack, next):
    if _is_retry(request):  # implement this predicate function on your own
        return ack()
    next()  # if this is not a retry, Bolt app should handle it

See also:

I think I've answered your question here. Would you mind closing this issue now?

@seratch
Copy link
Member

seratch commented Aug 15, 2022

Since we've already provided answers to the question here, let us close this issue now. That said, if you have anything further to discuss here, please don't hesitate to write in more 👋

@seratch seratch closed this as completed Aug 15, 2022
@iwakiri0104
Copy link

Hello @seratch,Thank you for the detailed explanation.
May I ask how this code is implemented?

 if _is_retry(request):  # implement this predicate function on your own

@seratch
Copy link
Member

seratch commented Oct 4, 2022

Hi @iwakiri0104, the request object has headers property. You can extract any values from it: https://slack.dev/bolt-python/api-docs/slack_bolt/request/request.html#slack_bolt.request.request.BoltRequest.headers

@iwakiri0104
Copy link

iwakiri0104 commented Oct 4, 2022

Hello @seratch,Thank you for answering!
But sorry, I have no idea how to use this information to implement the code.
Do I need to write all the class BoltRequest: statements?

@seratch
Copy link
Member

seratch commented Oct 4, 2022

@iwakiri0104 No, the request object is already an instance of the class. You can access the properties listed in the module document. More specifically, you should be able to check the existence of retry-related headers in request.headers.

@iwakiri0104
Copy link

@seratch Thanks for the detailed explanation.
I tried to check the log with this code, but I can't see the contents.
Thank you

Is the extraction not working with this code?

def _is_retry(request):
    logging.info("request is:",json.dumps(request))
    logging.info("request headers is:",json.dumps(request.headers))
    if "X-Slack-Retry-Num" in request.headers:
        return True

@seratch
Copy link
Member

seratch commented Oct 4, 2022

@iwakiri0104 This issue is already closed. I will reply to your issue #731

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants