I want to deploy a Slack bolt using Cloud Function, but since I can't use lazy listener, I use Thread for parallel processing. When I deploy to Cloud Function with the following code, I get duplicate messages the first time. After that, it only needs to be done once. However, after some time passes, duplicate messages are sent again.
Therefore, I would like to implement a process that only returns a response if there is an X-Slack-Retry-Num in the header, but I do not know how to get the header.
I implemented the following code in Python this way,
if"X-Slack-Retry-Num"inevent["headers"]:
return {'statusCode': 200, 'body': 'this request is retry'}Ended with an error result.
if "X-Slack-Retry-Num" in event["headers"]:
KeyError: 'headers'
Please let me know if there is a better solution.
### This is my code:
importosimportloggingfromslack_boltimportAppfromslack_boltimportSayfromslack_bolt.adapter.socket_modeimportSocketModeHandlerfromslack_sdkimportWebClientimportfunctions_frameworkimportdeeplimportlangidimportjsonfromslack_bolt.adapter.flaskimportSlackRequestHandlerlogging.basicConfig(level=logging.DEBUG)
DEEPL_API_TOKEN=os.environ["DEEPL_API_TOKEN"]
SLACK_BOT_TOKEN=os.environ["SLACK_BOT_TOKEN"]
app=App(
token=SLACK_BOT_TOKEN,
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
process_before_response=True
)
handler=SlackRequestHandler(app)
client=WebClient(SLACK_BOT_TOKEN)
translator=deepl.Translator(DEEPL_API_TOKEN)
@app.middlewaredeflog_request(logger, body, next):
logger.debug(body)
returnnext()
@app.event("reaction_added")defshow_datepicker(event,say: Say):
reaction=event["reaction"]
replies=say.client.conversations_replies(channel=event["item"]["channel"], ts=event["item"]["ts"])
print("replies is:",replies)
message=replies["messages"][0]["text"]
print("message is:",message)
lang=langid.classify(replies["messages"][0]["text"])
print("languages[0] is:",lang[0])
ifreaction=="eyes":
iflang[0] =="ja":
target_lang="EN-US"eliflang[0] =="en":
target_lang="JA"else:
returnresult=translator.translate_text(message, target_lang=target_lang)
query=""forminmessage.split("\n"):
query+=f">{m}\n"say(
thread_ts=replies["messages"][0].get("ts"),
text=f"{query}{result.text}"
) returnfromthreadingimportThread@functions_framework.httpdefslack_bot(request):
request_data=request.formt=Thread(target=show_datepicker, kwargs={'request_data': request_data})
t.start()
returnhandler.handle(request)
if__name__=="__main__":
app.start(port=int(os.environ.get("PORT", 3000)))The slack_bolt version
slack-bolt==1.14.3
slack-sdk==3.18.3
Python runtime version
Python 3.9.12
Expected result:
I want to prevent duplicate messages.
Actual result:
Duplicate messages more than three times
I want to deploy a Slack bolt using Cloud Function, but since I can't use lazy listener, I use Thread for parallel processing. When I deploy to Cloud Function with the following code, I get duplicate messages the first time. After that, it only needs to be done once. However, after some time passes, duplicate messages are sent again.
Therefore, I would like to implement a process that only returns a response if there is an X-Slack-Retry-Num in the header, but I do not know how to get the header.
I implemented the following code in Python this way,
Ended with an error result.
Please let me know if there is a better solution.
### This is my code:
The
slack_boltversionslack-bolt==1.14.3
slack-sdk==3.18.3
Python runtime version
Python 3.9.12
Expected result:
I want to prevent duplicate messages.
Actual result:
Duplicate messages more than three times