I have the following Python code which works fine using the Web Socket mode. When I type a slash command (/hello-socket-mode
) on my Slash application then it very well invokes my method, handle_some_command()
:-
import os
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
# Install the Slack app and get xoxb- token in advance
app = App(token="<bot token>")
# Add functionality here
@app.command("/hello-socket-mode")
def handle_some_command(ack, body, logger):
ack()
print('testing slash command')
logger.info(body)
if __name__ == "__main__":
# Create an app-level token with connections:write scope
handler = SocketModeHandler(app,"<app token>")
handler.start()
Since I'm in the development phase of my Slack app, I want to add reload functionality to this backend code of the web socket such that it reloads automatically whenever there is a change in the code. I tried to add uvicorn
here but with that, I stopped getting invocation to my backend method, handle_some_command()
whenever I tried to enter the slash command in the my slack application. I created a separate Python script called run.py with following code:-
from uvicorn import run
if __name__ == "__main__":
run("main:app", host="0.0.0.0", port=3000, reload=True, log_level="info")
And then executed, python run.py
to run my app using uvicorn
to reload whenever there is a code change. It is not working at all, I'm not getting any slash command invocation to my backend code now.
I just need some way of reload and not necessarily uvicorn
here. I would appreciate it if someone could please guide me on this to make it work.
Code like this will work; you may delete fastapi dependency if not needed when development finished
import os
from fastapi import FastAPI
from slack_bolt.adapter.socket_mode import SocketModeHandler
from slack_bolt.app import App
# Set enviornment vars
BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"]
APP_TOKEN = os.environ["SLACK_APP_TOKEN"]
SIGNING_SECRET = os.environ["SLACK_SIGNING_SECRET"]
# Load Slack Bolt
app = App(token=BOT_TOKEN, signing_secret=SIGNING_SECRET)
# Load FastAPI
api = FastAPI()
# Load Slack bolt handlers
# Action().add_to(bolt)
SocketModeHandler(app, APP_TOKEN).connect()
@app.message("hello")
def message_hello(message, say):
say(f"Hey there <@{message['user']}>!")
# Load FastAPI endpoints
@api.get("/")
async def root():
return {"OK"}
And call this code like this:
bash -c 'uvicorn app.main:api --reload --host 0.0.0.0 --port 4000 --log-level warning'