I’m building a Slack bot using FastAPI that performs a long-running task and updates the original message in Slack after the task completes. However, instead of updating the original message, it sends a new one.
Expected Behavior:
- The bot should display an initial message (“Processing your request…”) and later update this same message with the final result.
Actual Behavior:
- The initial message is sent correctly, but when the task completes, a new message is posted instead of updating the original one.
Code Implementation:
FastAPI Endpoint:
@app.post("/greet")
async def greet_user(
background_tasks: BackgroundTasks,
user_name: str = Form(...),
response_url: str = Form(...) # Provided by Slack
):
background_tasks.add_task(
process_long_running_task,
user_name=user_name,
response_url=response_url
)
return JSONResponse(
content={
"response_type": "ephemeral",
"text": "Processing your request..."
}
)
Background Task:
async def process_long_running_task(user_name: str, response_url: str):
await asyncio.sleep(15) # Simulate long-running task
final_response = {
"response_type": "in_channel",
"text": f"Hii, {user_name}. How can I help you today?"
}
async with httpx.AsyncClient() as client:
await client.post(response_url, json=final_response)
Question:
How can I ensure the bot updates the original message instead of posting a new one? Do I need to use additional parameters or a different API method?
Thank you for any help!