Slash command that updates Slack bot code in Python?

81 views Asked by At

I'm trying to write a Slack bot in Python. One of the functionalities I'm looking for is a way to toggle on/off a daily report message using a slash command, or some other kind of interactive message system in a Slack DM or channel with the bot.

Right now I have a boolean variable, post which is set to True. The idea is that when a user sends a slash command message, say /post-off to the Slack bot, post will be set to False. I'm developing in Socket Mode, so what this looks like is:

from slack_sdk import WebClient
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler

slack_client = WebClient(token=SLACK_TOKEN)
handler = SocketModeHandler(app, SLACK_APP_TOKEN)
handler.start()

post = True

if post:
     slack_client.chat_postMessage(channel=CHANNEL, text=TEXT)

However, even without trying to insert code changing post (which I plan on doing via a slash command, thus me inserting the handler code), my message isn't posting to the channel. When I comment out the two lines of handler code, though, the message sends. I'm not sure why this is happening, and I'm also not sure how to manipulate the variable value with my slash command.

1

There are 1 answers

2
Rafael On

According to the documentation of the .start() method, this is a blocking operation. If you don't want this to happen, use connect instead.

"Establishes a new connection and then blocks the current thread to prevent the termination of this process. If you don't want to block the current thread, use #connect() method instead."