|
| 1 | +# Internal Bot Architecture |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The internal-bot helps EuroPython Society teams communicate better. Right now it connects GitHub and Zammad with Discord, but it's built to do more in the future. |
| 6 | + |
| 7 | +The app has three main parts: |
| 8 | +1. **Django Web App**: Receives webhooks, provides an admin panel, and will support more features later |
| 9 | +2. **Discord Bot**: Sends messages to Discord and responds to commands |
| 10 | +3. **Background Worker**: Handles tasks in the background without blocking the web app |
| 11 | + |
| 12 | +Besides handling webhooks, the bot can also: |
| 13 | +- Respond to user commands (like `!ping`) |
| 14 | +- Answer questions in channels and threads |
| 15 | +- React to messages and reactions from users |
| 16 | + |
| 17 | +The bot code and supported commands can be found in `intbot/core/bot/main.py`. |
| 18 | + |
| 19 | +## Project Structure |
| 20 | + |
| 21 | +The codebase is organized as follows: |
| 22 | + |
| 23 | +``` |
| 24 | +deploy/ # Deployment configuration |
| 25 | +├── playbooks/ # Ansible playbooks |
| 26 | +└── templates/ # Templates for Docker Compose and Makefiles |
| 27 | +
|
| 28 | +docs/ # Project documentation |
| 29 | +├── architecture.md # System design and structure |
| 30 | +└── deployment.md # Deployment guide |
| 31 | +
|
| 32 | +intbot/ |
| 33 | +├── core/ # Main Django app with all the business logic |
| 34 | +│ ├── bot/ # Discord bot implementation |
| 35 | +│ ├── endpoints/ # API endpoints for webhooks |
| 36 | +│ ├── integrations/ # Integration modules (GitHub, Zammad) |
| 37 | +│ ├── management/ # Django management commands |
| 38 | +│ └── models.py # Database models |
| 39 | +├── intbot/ # Django project settings |
| 40 | +│ ├── settings.py # Configuration |
| 41 | +│ └── urls.py # URL routing |
| 42 | +└── tests/ # Test suite (mirrors the application structure) |
| 43 | +``` |
| 44 | + |
| 45 | +This structure was chosen because: |
| 46 | +- It's simple and has a single `core` app instead of many small apps |
| 47 | +- It clearly separates the integration logic from the bot logic |
| 48 | +- Tests mirror the application structure, making them easy to find |
| 49 | +- It supports multiple entry points (web server, bot, worker) from one codebase |
| 50 | + |
| 51 | +## System Architecture |
| 52 | + |
| 53 | +``` |
| 54 | +┌─────────────────┐ ┌──────────────────┐ ┌────────────────┐ |
| 55 | +│ External │ │ │ │ │ |
| 56 | +│ Services │────▶│ Django App │────▶│ Discord │ |
| 57 | +│ (GitHub, │ │ (Webhook API) │ │ Channels │ |
| 58 | +│ Zammad) │ │ │ │ │ |
| 59 | +└─────────────────┘ └──────────────────┘ └────────────────┘ |
| 60 | + │ ▲ |
| 61 | + ▼ │ |
| 62 | + ┌──────────────────┐ |
| 63 | + │ │ |
| 64 | + │ Database │ |
| 65 | + │ (PostgreSQL) │ |
| 66 | + │ │ |
| 67 | + └──────────────────┘ |
| 68 | +``` |
| 69 | + |
| 70 | +### Data Flow |
| 71 | + |
| 72 | +1. External services send webhooks to our app |
| 73 | +2. We verify and save these webhooks to the database |
| 74 | +3. Our background worker processes these webhooks: |
| 75 | + - For some webhooks (like GitHub), we need to fetch more data to make them useful |
| 76 | + - We then turn the webhook data into a format we can use |
| 77 | +4. If a Discord message needs to be sent, the channel router picks the right channel |
| 78 | +5. Discord messages are saved to the database |
| 79 | +6. The Discord bot checks for new messages and sends them |
| 80 | + |
| 81 | +### Using the Admin Panel |
| 82 | + |
| 83 | +The Django Admin panel lets you: |
| 84 | +- See all webhooks and filter them by type or date |
| 85 | +- Look at the raw webhook data |
| 86 | +- Check if tasks worked or failed |
| 87 | +- Manually trigger processing for webhooks |
| 88 | +- View and manage Discord messages |
| 89 | + |
| 90 | +## Key Components |
| 91 | + |
| 92 | +### Models |
| 93 | + |
| 94 | +- **Webhook**: Stores webhook data including source, event type, and content |
| 95 | +- **DiscordMessage**: Represents a message to be sent to Discord |
| 96 | +- **Task**: (django-tasks) Stores background task execution history |
| 97 | + |
| 98 | +### Integrations |
| 99 | + |
| 100 | +#### GitHub Integration |
| 101 | +- Receives webhooks at `/webhooks/github/` |
| 102 | +- Verifies signatures using HMAC-SHA256 |
| 103 | +- Currently handles project item events and issues |
| 104 | +- Routes messages based on project ID or repository |
| 105 | + |
| 106 | +**Why GitHub Needs Extra Steps:** |
| 107 | +1. **The Problem**: GitHub webhooks often just contain IDs, not useful information |
| 108 | +2. **Getting More Data**: |
| 109 | + - We need to ask GitHub's GraphQL API for details like item names and descriptions |
| 110 | + - Without this extra step, notifications would just say "Item 12345 was moved" instead of what actually happened |
| 111 | +3. **How It Works**: |
| 112 | + - First, we save the webhook and get the extra data from GitHub |
| 113 | + - Then, we process it into a readable message |
| 114 | + - Finally, we send it to the right Discord channel |
| 115 | + |
| 116 | +This approach lets us send helpful messages with real information instead of just ID numbers. |
| 117 | + |
| 118 | +#### Zammad Integration |
| 119 | +- Receives webhooks at `/webhooks/zammad/` |
| 120 | +- Verifies signatures using HMAC-SHA1 |
| 121 | +- Processes ticket and article information |
| 122 | +- Figures out what happened (new ticket, reply to ticket) |
| 123 | +- Routes messages based on ticket group (billing, helpdesk) |
| 124 | + |
| 125 | +### Channel Router |
| 126 | +- Decides which Discord channel should receive messages |
| 127 | +- Uses different routing rules for each source (GitHub, Zammad) |
| 128 | +- Channel mappings are set in configuration |
| 129 | +- Can be extended for new message sources in the future |
0 commit comments