# Slack Bot For Kubernetes > Build and deploy a Slack Bot on Kubernetes using the Slack Bolt framework, FastAPI, and DevSpace. Step-by-step guide covering Python setup, DevSpace configuration, Slack app creation, and production deployment. ## Project Overview This project showcases the development of a Slack Bot using the Slack Bolt framework and FastAPI web framework, demonstrating the use of DevSpace for efficient deployment and orchestration within a Kubernetes cluster. The Slack Bot is a solution designed to enhance team productivity and interaction within Slack channels. Developed with DevSpace, this bot is a prime example of modern, cloud-native technology in action. It's built on the versatile FastAPI web framework and utilizes a suite of powerful Kubernetes tools to ensure seamless deployment and operation. ## Technologies Used - Python Slack Bolt Framework: For simplifying Slack app development. - FastAPI: Serving as the Python web framework to run the Slack app. - Docker: For containerizing the Slack Bot FastAPI application. - Kubernetes: As the deployment environment for our containerized application. - DevSpace: Assisting in development and providing CI/CD automation for the application. - Redis: For state management and data storage. - Prometheus: For metrics and observability. ## Kubernetes Tools ### Basic Tools (Kubert basic package) - ExternalDNS: Automates DNS record management based on services and ingresses within the Kubernetes cluster. Supports AWS Route53, Google Cloud DNS, Azure DNS, Cloudflare, and more. - Cert-Manager: Native Kubernetes certificate management controller. Automates the management, issuance, and renewal of TLS certificates. Supports Let's Encrypt and other ACME-compliant CAs. - Ingress Nginx Controller: Manages external access to HTTP and HTTPS services within the cluster. Handles SSL/TLS termination, hostname routing, and URL path-based routing. ### Observability Tools The Slack Bot has been configured for metrics and logs. Prometheus scrapes metrics from the FastAPI web container. Logs are customized for collection and aggregation. Metrics and logs are used for both preventative and detective controls. ## Before Dev or Deployment ### Kubernetes Manifest Configuration #### Ingress Setup Update `devspace-deployment/kube-proxy/ingress.yaml`: - Replace `external-dns.alpha.kubernetes.io/hostname` annotation with your domain name - Update host in Rules and TLS sections - Configure `cert-manager.io/cluster-issuer` annotation with your cluster issuer #### Service Monitor Update `devspace-deployment/manifest/service_monitor.yaml` with the correct Prometheus label. Query for the Prometheus resource to find the serviceMonitorSelector matchLabels. ### DevSpace Images Create `.env.devspace` file from `.env.devspace_bak` template with: - `IMAGE`: Main Docker image for production deployment - `IMAGE_DEV`: Docker image for development - `IMAGE_REDIS_STACK`: Redis deployment image ### Slack Credentials Create `.env` file from `.env_bak` template with: - `SLACK_BOT_TOKEN`: Authenticates bot with Slack API - `SLACK_SIGNING_SECRET`: Verifies incoming requests from Slack ## Visual Studio Code (IDE) Customized VS Code files: - `.vscode/settings.json`: Project-specific settings - `.vscode/extensions.json`: Recommended extensions for team consistency - `.vscode/launch.json`: Debugger configuration for unit tests ## Python Setup ### Tools - pyenv: Manages multiple Python versions on a single system - pyenv-virtualenv: Plugin for virtual environment management - Poetry: Dependency management and packaging tool - pyproject.toml: Project configuration following PEP 518 ### Setup Steps 1. Create virtual environment: `pyenv virtualenv 3.9.12 webinar-slackbot` 2. Activate: `pyenv activate webinar-slackbot` 3. Set local: `pyenv local webinar-slackbot` 4. Install Poetry: `python -m pip install poetry` 5. Install dependencies: `poetry install` ### Project Structure - `src`: Main source directory - `tests`: Test directory - `[tool.poetry.dependencies]`: Required Python libraries - `[tool.poetry.dev-dependencies]`: Development Python libraries ## DevSpace Configuration ### devspace.yaml Sections 1. Variables (vars): Environment variables from `.env.devspace` 2. Pipelines: dev and deploy pipelines with image patching 3. Images: Build configuration using BuildKit for concurrent image builds 4. Deployments: Kubernetes manifest deployment with image patches 5. Development (dev): Pod selection, image replacement, file sync, terminal, SSH, port forwarding ### DevSpace Commands Configure Kubernetes context, then use: - Dev: `devspace dev --kube-context=your-context --namespace=webinar` - Build: `devspace build --kube-context=your-context` - Deploy: `devspace build -b slack-bot-server --kube-context=your-context --namespace=webinar && devspace deploy --kube-context=your-context --namespace=webinar` ## Slack Configuration ### App Creation Create a Slack App at https://api.slack.com/apps?new_app=1, choose "from scratch", set app name and workspace. ### OAuth and Permissions Required Bot Token Scopes: - `app_mentions:read` - `chat:write` ### Events and Interactivity - Enable Event Subscriptions with public URL from Ingress - Enable Interactivity with the same request URL ## Application Architecture ### Layered Architecture - Presentation Layer: Handles HTTP requests/responses, interfaces with Slack APIs - Service Layer: Core functionality and business logic - Data Access Layer: Data persistence and external API interactions - App Factory: create_app factory pattern for FastAPI ### Testing Strategy - Unit Testing: Individual component testing with mocking - Integration Testing: Cross-layer validation with real network calls - Test Containers: Simulates real-world environments for integration tests - Framework: pytest ## Redis ETL Job ### Redis Deployment Redis is deployed with DevSpace using Bitnami Kubernetes assets from the Bitnami Redis Helm Chart. ### ETL Process - Located in the `etl` folder with CSV data files from HubSpot (popular quotes) - Run from dev container: `make indexer_job` - Code: `src/jobs/redis_job.py` ### Application Usage Redis is queried via search index to randomly select quotes in response to Slack user interactions. ## Source Code GitHub Repository: https://github.com/TranslucentComputing/webinar-slack-bot ## Webinar Video walkthrough: https://youtu.be/9WGCo30w5zc