Closed BS-98 closed 7 months ago
Hey @BS-98 👋 The start()
method is a blocking one so changing this to connect()
should leave the program control flow to you! There's a bit more detail on this in #558 but I found that the following snippet works alright:
import threading
import time
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
def run():
app = App(token="bot_token")
handler = SocketModeHandler(app, "app_token")
disconnect_event = threading.Event()
@app.message("hello")
def message_hello(message, say):
say(f"Hey there <@{message['user']}>!")
time.sleep(1)
handler.disconnect()
disconnect_event.set()
handler.connect()
disconnect_event.wait()
run()
I'm not sure if there's a way to accomplish this without the time.sleep
, but I found that the message event from say
is only partially received when the disconnect
event happens and would raise a JSONDecodeError
error :thinking:
Hopefully this fits into your app alright, but please let me know if any problems arise!
It works! Thank you!
Indeed, without time.sleep
I get an error:
raise SlackClientNotConnectedError(
slack_sdk.errors.SlackClientNotConnectedError: Failed to send a message as the connection is no longer active (session_id: <session_id>, error: 'NoneType' object has no attribute 'send')
So I assume that the solutions you gave, is correct and after disconnect_event.set()
all connections are close and resources are free and everything is clear, am I right?
Yeah, you're right on it. Glad to know @zimeg's suggestion was helpful! Let me close this issue now.
Hi! I want to listen for message "hello" (like in yours tutorial), respond with "Hi!" and close the connection. I need to use a WebSocket. I have a program:
Then, I execute with
python3 my_program.py
. I would like to execute functionrun()
elsewhere in my system.