/slack-bot-adk-python-cloudrun

Slack Bot using Google Agent Development Kit (Python, Cloud Run)

Primary LanguagePythonMIT LicenseMIT

Slack Bot using Google Agent Development Kit (Python, Cloud Run)

Ask DeepWiki PRs Welcome

image

This repository provides a Slack bot backend implemented in Python that uses Slack Bolt and Google Cloud's Vertex AI Gemini model via the Agent Development Kit. The bot responds to text, images, PDFs, plain text files, videos, and audio messages, while maintaining conversation context within Slack threads. It is designed to run on Cloud Run.

If you want to use the Google Gen AI SDK, please refer to this repository💡

Features

  • Responds to @mention messages in Slack channels.
  • Supports text, image, PDF, text file, video, and audio inputs from Slack messages. Files are fetched via authenticated URLs and sent to Gemini for multimodal understanding.
  • Maintains conversation context by retrieving prior messages in a thread and sending them as conversation history to Gemini.
  • Formats responses using Slack-compatible Markdown for rich text output.
  • FastAPI-based web server suitable for Cloud Run.
  • Deployment script for building and deploying to Cloud Run.

Project Structure

app/
  main.py           # FastAPI app and Slack Bolt handlers
  agents/
    comedian.py     # ex: Comedian agent implementation
  tools/
    get_current_datetime.py  # ex: Date/time utility tool
scripts/
  deploy.sh         # Helper script to deploy to Cloud Run
Dockerfile          # Container definition for Cloud Run
requirements.txt    # Python dependencies
llms.txt           # ADK documentation for LLM reference
llms-full.txt      # Extended ADK documentation for LLM context

Prerequisites

  • Python 3.13
  • Google Cloud SDK with gcloud authenticated
  • Slack workspace admin privileges

Local Development

  1. Install dependencies
    python -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
  2. Configure environment variables
    cp .env.example .env
    # edit .env and set your Slack and Google Cloud credentials
    # ALLOWED_SLACK_WORKSPACE is the Slack team ID to allow requests from
  3. Run the server
    uvicorn app.main:fastapi_app --host 0.0.0.0 --port 8080 --reload
  4. Use a tunneling tool like ngrok to expose http://localhost:8080/slack/events to Slack during development.

Optional: Use the ADK Web Development UI

The Agent Development Kit includes a built-in web-based Development UI that you can run locally. It's a powerful tool for testing, debugging, and interacting with your agent during development. It provides a chat interface to send messages to your agent and inspect the results.

  1. Start the ADK web server:

    adk web
  2. Interact with your agent: Open the local URL (usually http://127.0.0.1:8000) in your browser to use the Development UI.

Slack App Configuration

  1. Create a new Slack app at https://api.slack.com/apps.
  2. Under OAuth & Permissions, add the following Bot Token scopes:
    • app_mentions:read
    • chat:write
    • channels:history
    • groups:history
    • im:history
    • mpim:history
    • files:read
  3. Install the app to your workspace to obtain SLACK_BOT_TOKEN and SLACK_SIGNING_SECRET.
  4. Enable Event Subscriptions and set the Request URL to https://<your-cloud-run-service-url>/slack/events.
  5. Subscribe to bot events: app_mention.
  6. Invite the bot to channels where you want to use it.

Deploy to Cloud Run

The repository includes a helper script to build the container and deploy to Cloud Run. Ensure your .env contains SLACK_BOT_TOKEN and SLACK_SIGNING_SECRET before running:

One-time setup (first run only)

Enable the Cloud Build API for your project:

gcloud services enable cloudbuild.googleapis.com

Then deploy:

./scripts/deploy.sh

The script will:

  1. Build the container image using Cloud Build.
  2. Deploy the image to Cloud Run.
  3. Set the required environment variables on the service.

After deployment, configure the Slack app's event subscription URL to the Cloud Run service URL.