Skip to main content
This guide shows how to create a Slack bot using Timbal that can:
  • Respond to messages in real-time via webhooks
  • Process user requests intelligently with AI agents
  • Handle threaded conversations

Setup Requirements

Install the Slack SDK:
For all available Slack API methods and capabilities, see the Slack API Methods Reference.
1

Create a Slack App

  1. Go to Slack API and click Create New App
  2. Choose From scratch
  3. Enter your app name and select your workspace
2

Configure Bot Token Scopes

  1. Go to OAuth & Permissions
  2. 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

  1. In OAuth & Permissions, click Install to Workspace
  2. Copy the Bot User OAuth Token (starts with xoxb-)
  3. Set as environment variable: SLACK_BOT_TOKEN
4

Enable Home Tab (for Direct Messages)

  1. Go to App Home in your Slack app settings
  2. Under Show Tabs, enable Home Tab
  3. Check Allow users to send Slash commands and messages from the messages tab
  4. This enables users to send direct messages to your bot
5

Get Bot User ID

  1. In your Slack workspace, start a direct message with your bot
  2. Click on the bot’s name at the top of the chat
  3. In the profile panel, find the Member ID (format: U12345678)
  4. 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
Then configure your Python code:

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()
Just ensure the same name is used consistently in both the agent call and the pre_hook check.

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

  1. Create an account at ngrok.com
  2. Install ngrok on your system and the Python package:
  1. 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

  1. Run the script to start your Slack bot:
Or using uv (recommended):
  1. Copy the ngrok public URL displayed in the terminal (e.g., https://abc123.ngrok-free.app)
4

Enable Event Subscriptions

  1. Go to Event Subscriptions and toggle Enable Events
  2. Set Request URL to our webhook endpoint (e.g., https://abc123.ngrok-free.app/)
  3. Subscribe to Bot Events - these determine what events Slack will send to your bot:
Your Slack bot is now ready! 🚀 Start a direct message with your bot or mention it in a channel to begin chatting with your AI assistant.

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

  • Check webhook URL is publicly accessible and returns 200 status
  • Ensure bot has correct permissions in Slack workspace
  • Confirm SLACK_BOT_USER_ID matches your bot’s actual user ID
  • Test webhook endpoint manually with curl or Postman
  • Double-check SLACK_BOT_USER_ID is 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
  • 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
  • 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/)