-
Notifications
You must be signed in to change notification settings - Fork 248
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
Custom endpoints for AsyncSocketModeHandler #622
Comments
Hi @ImRohan01 thanks for asking the question! If you are fine to use the AIOHTTP's web server feature, checking the example code in the PR https://github.com/slackapi/bolt-python/pull/623/files should be helpful for you. I hope this was helpful to you! Is everything clear now? |
Hi @seratch thanks for the quick response. I understand the approach and have been able to follow it for my use case. I am using FastAPI in place of aiohttp. Things seem to be working fine except for while shutting down, I get this error:
I am not sure if this is related to slack bolt or fastapi or uvicorn as such. But any help would be appreciated. Thanks! |
@ImRohan01 It seems that FastAPI also has startup callbacks. Can you try adding an async function to start a Socket Mode client in the same way? https://fastapi.tiangolo.com/ru/advanced/events/ |
@seratch This is my code for reference, I had been using the startup event all this time.
I run this using |
@ImRohan01 Can you try this example out? import os
from slack_bolt.async_app import AsyncApp
from slack_bolt.adapter.socket_mode.async_handler import AsyncSocketModeHandler
from fastapi import FastAPI
import logging
logging.basicConfig(level=logging.DEBUG)
app = AsyncApp(token=os.getenv("SLACK_BOT_TOKEN"))
handler = AsyncSocketModeHandler(app, os.getenv("SLACK_APP_TOKEN"))
@app.event({"type": "message"})
async def receive_message(event, say):
await say("Hiya")
web_app = FastAPI()
@web_app.get("/healthcheck")
async def healthcheck():
if handler.client is not None and await handler.client.is_connected():
return "OK"
return "BAD"
@web_app.on_event('startup')
async def start_slack_socket_conn():
await handler.connect_async()
@web_app.on_event('shutdown')
async def start_slack_socket_conn():
await handler.close_async() |
@seratch the above solved the issue. Thanks for the help! |
It seems that |
@tracy-ash Nothing is surprising. Just moving the event handlers into a single lifespan function works for you: import os
from slack_bolt.async_app import AsyncApp
from slack_bolt.adapter.socket_mode.async_handler import AsyncSocketModeHandler
import logging
logging.basicConfig(level=logging.DEBUG)
app = AsyncApp(token=os.getenv("SLACK_BOT_TOKEN"))
handler = AsyncSocketModeHandler(app, os.getenv("SLACK_APP_TOKEN"))
@app.event({"type": "message"})
async def receive_message(event, say):
await say("Hiya")
from fastapi import FastAPI
from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app: FastAPI):
await handler.connect_async()
yield
await handler.close_async()
web_app = FastAPI(lifespan=lifespan)
@web_app.get("/healthcheck")
async def healthcheck():
if handler.client is not None and await handler.client.is_connected():
return "OK"
return "BAD" |
I want to deploy a slack app within a container in K8s with AsyncApp client and AsyncSocketModeHandler as shown in https://github.com/slackapi/bolt-python/blob/main/examples/socket_mode_async.py. I wanted to understand how we could create custom endpoints like one for health check.
The text was updated successfully, but these errors were encountered: