- Respond to messages in real-time via webhooks
- Process user requests intelligently with AI agents
- Handle threaded conversations
Setup Requirements
Installing Packages
Installing Packages
Install the Slack SDK:Or using uv (recommended):
For all available Slack API methods and capabilities, see the Slack API Methods Reference.
Creating a Slack App
Creating a Slack App
1
Create a Slack App
- Go to Slack API and click Create New App
- Choose From scratch
- Enter your app name and select your workspace
2
Configure Bot Token Scopes
- Go to OAuth & Permissions
- Add the following Bot Token Scopes based on Timbal handlers you’ll use:
After adding or modifying scopes, you’ll need to reinstall the app to your workspace for the changes to take effect.
3
Install App to Workspace
- In OAuth & Permissions, click Install to Workspace
- Copy the Bot User OAuth Token (starts with
xoxb-) - Set as environment variable:
SLACK_BOT_TOKEN
4
Enable Home Tab (for Direct Messages)
- Go to App Home in your Slack app settings
- Under Show Tabs, enable Home Tab
- Check Allow users to send Slash commands and messages from the messages tab
- This enables users to send direct messages to your bot
5
Get Bot User ID
- In your Slack workspace, start a direct message with your bot
- Click on the bot’s name at the top of the chat
- In the profile panel, find the Member ID (format:
U12345678) - Set this as environment variable:
SLACK_BOT_USER_ID=U12345678
The Bot User ID is required to prevent infinite loops by filtering out the bot’s own messages.
Implementation
Setting up the Slack Bot Configuration
First, set up the Slack configuration with your bot’s user ID and authentication token. Create a.env file in your project root:
.env
Creating the Agent
Create the Timbal agent with pre and post hooks for Slack integration:Pre-hook: Processing Incoming Messages
The pre-hook handles incoming Slack webhook events and extracts relevant information.Parameter Name Flexibility: The
_webhook parameter name is completely custom. You can use any name you prefer:await agent(slack_data=body).collect()await agent(webhook_payload=body).collect()await agent(event_data=body).collect()
Post-hook: Sending Responses
The post-hook sends the agent’s response back to Slack:Complete Example
Here’s the full implementation ready to use:agent.py
Running the Agent
Timbal provides built-in ngrok integration for easy local development. To use this feature:1
Set up ngrok account and authtoken
- Create an account at ngrok.com
- Install ngrok on your system and the Python package:
- Configure your authtoken:
2
Create FastAPI Webhook Handler
Create a simple FastAPI server to handle Slack’s URL verification and webhook events:
server.py
3
Run the Server and Get ngrok URL
- Run the script to start your Slack bot:
- Copy the ngrok public URL displayed in the terminal (e.g.,
https://abc123.ngrok-free.app)
4
Enable Event Subscriptions
- Go to Event Subscriptions and toggle Enable Events
- Set Request URL to our webhook endpoint (e.g.,
https://abc123.ngrok-free.app/) - Subscribe to Bot Events - these determine what events Slack will send to your bot:
Key Features
- Real-time Responses: Instantly processes Slack messages via webhooks
- Thread Support: Maintains conversation context in Slack threads
- Loop Prevention: Automatically ignores bot’s own messages
- Flexible AI: Uses any LLM model supported by Timbal
- Easy Integration: Minimal setup with existing Slack handlers
Troubleshooting
Bot Not Responding
Bot Not Responding
- Check webhook URL is publicly accessible and returns 200 status
- Ensure bot has correct permissions in Slack workspace
- Confirm
SLACK_BOT_USER_IDmatches your bot’s actual user ID - Test webhook endpoint manually with curl or Postman
Infinite Loop Issues
Infinite Loop Issues
- Double-check
SLACK_BOT_USER_IDis correct (found in Slack app settings) - Ensure pre-hook properly filters bot messages with
bail() - Verify webhook events aren’t being duplicated
- Check that bot doesn’t respond to its own message events
Agent Not Processing Messages
Agent Not Processing Messages
- Check that OpenAI API key is valid and has sufficient credits
- Verify agent model name is correct (e.g.,
openai/gpt-4o-mini) - Ensure pre-hook is setting
span.input["prompt"]correctly - Add logging to debug webhook payload structure
Server Won't Start
Server Won't Start
- Ensure all required packages are installed (
slack-sdk fastapi uvicorn pyngrok python-dotenv) - Verify ngrok is installed and configured with authtoken
- Make sure port 8000 is not already in use by another process
- Ensure Slack Request URL matches your POST endpoint route with ”/” (e.g.,
https://abc123.ngrok-free.app/)