Skip to content

Slack Bot with NLP parsing (customised for B2B customer usage)

Notifications You must be signed in to change notification settings

sidv1711/slack-bot

Repository files navigation

🤖 AI-Powered Slack Bot - Microservice Architecture

An intelligent Slack bot with modular AI capabilities built on a scalable microservice architecture.

🏗️ Architecture Overview

┌─────────────────┐     HTTP API     ┌─────────────────┐
│   Slack Bot     │ ◄──────────────► │ AI Microservice │
│   (Port 8000)   │                  │   (Port 8001)   │
│                 │                  │                 │
│ • Slack Events  │                  │ • NL2SQL        │
│ • User Auth     │                  │ • Code Gen      │
│ • Responses     │                  │ • General Chat  │
└─────────────────┘                  └─────────────────┘

🎯 Key Benefits

  • Modularity: Independent AI and Slack services
  • Scalability: Scale AI processing independently
  • Maintainability: Update AI without touching Slack code
  • Flexibility: Easy AI provider swapping

🚀 Quick Start

Prerequisites

  • Docker & Docker Compose
  • OpenAI API Key
  • Slack App credentials

1. Environment Setup

cp env.example .env
# Edit .env with your credentials

2. Start Services

docker-compose up --build

3. Verify Services

🔧 Configuration

Required Environment Variables

# Slack Configuration
SLACK_BOT_TOKEN=<SLACK_TOKEN>
SLACK_SIGNING_SECRET=your-signing-secret

# AI Configuration  
OPENAI_API_KEY=your-openai-key

# Authentication (Optional)
AUTH_URL=your-auth-url
AUTH_API_KEY=your-auth-key

# Database (Optional - for NL2SQL)
DATABASE_URL=postgresql://user:password@localhost:5432/dbname

🌐 API Endpoints

The AI microservice provides RESTful endpoints:

  • POST /api/v1/ai/process - Main AI routing
  • POST /api/v1/nl2sql/convert - Natural Language to SQL
  • POST /api/v1/code/generate - Code generation
  • POST /api/v1/chat/respond - General conversation
  • GET /health - Health check
  • GET /docs - Interactive API documentation

🤖 Slack Commands

Slash Commands

  • /hello - Get a personalized greeting from the bot
  • /ai [question] - Ask AI anything (automatically routes to appropriate service)
  • /test-executions [test_id] [limit] - View test execution history
  • /connect-slack - Connect your Slack workspace

AI Capabilities

The bot automatically routes your requests to the most appropriate AI service:

  1. General Chat

    • Natural dialogue and conversation
    • Question answering and explanations
    • Creative brainstorming
    • Problem discussion
    • Knowledge in science, technology, history, and more
  2. Code Generation

    • Generate code in multiple languages:
      • Python, JavaScript, TypeScript
      • Java, Go, Rust
      • C++, C#, SQL
    • Features:
      • Function and class generation
      • Algorithm implementation
      • API client code
      • Data processing scripts
      • Test code generation
      • Code documentation
  3. NL2SQL (Database Queries)

    • Convert natural language to SQL queries
    • Supported operations:
      • Filtering by test_uid, status, execution_time
      • Time-based queries (last week, yesterday, etc.)
      • Status filtering (failed, passed, etc.)
      • Results in formatted tables
    • Safety features:
      • SQL injection prevention
      • Read-only operations
      • Query validation

Example Usage

# General Questions
/ai What is machine learning?
/ai Explain REST APIs to me

# Database Queries
/ai Show me failed tests from yesterday
/ai Count how many tests passed this week

# Code Generation
/ai Write a Python function to calculate fibonacci numbers
/ai Create a JavaScript function that validates emails

# Test Executions
/test-executions TEST_ID 5
/test-executions TEST_ID limit=10

🧪 Testing

Run Architecture Tests

python test_microservice_architecture.py

Run Unit Tests

pytest tests/

🐳 Deployment

The application is deployed using Docker containers and Cloudflare tunnels for secure connectivity.

📁 Project Structure

├── src/                    # Main Slack bot service
│   ├── bot/               # Slack event handling
│   ├── services/          # HTTP client for AI service
│   └── config/            # Configuration
├── ai-microservice/       # Independent AI service
│   ├── src/
│   │   ├── api/          # REST API endpoints
│   │   ├── services/     # AI processing services
│   │   └── models/       # Request/response models
│   └── Dockerfile
├── tests/                 # Unit tests
├── scripts/              # Database setup scripts
└── docs/                 # Documentation

🔄 Adding New AI Services

The microservice architecture makes it easy to add new AI capabilities:

  1. Add Service: Create new service in ai-microservice/src/services/
  2. Register: Add to AIRouterService in ai_router_service.py
  3. API Endpoint: Add REST endpoint in api/routes.py
  4. Deploy: Restart AI microservice only

📚 Documentation

🛠️ Development

Local Development

# Start main bot only
cd src && python -m uvicorn main:app --reload --port 8000

# Start AI service only  
cd ai-microservice && python -m uvicorn src.main:app --reload --port 8001

Adding Features

  • Slack Features: Modify src/ directory
  • AI Features: Modify ai-microservice/ directory
  • Independent deployment of each service

🤝 Contributing

  1. Fork the repository
  2. Create feature branch
  3. Make changes to appropriate service
  4. Test with test_microservice_architecture.py
  5. Submit pull request

📄 License

MIT License - see LICENSE file for details.


Built with ❤️ using FastAPI, Docker, and OpenAI

About

Slack Bot with NLP parsing (customised for B2B customer usage)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages