|
1 | | -# Alertmanager webhook for Telegram (Python Version) |
| 1 | +# Alertmanager webhook for Telegram (Python version) |
2 | 2 |
|
3 | 3 |  |
4 | 4 |  |
5 | 5 |
|
6 | | -GO Version (https://github.com/nopp/alertmanager-webhook-telegram-go) |
7 | | - |
8 | | -Python version 3 |
9 | | - |
10 | | -## INSTALL |
11 | | - |
12 | | -* pip install -r requirements.txt |
13 | | - |
14 | | -Change on flaskAlert.py |
15 | | -======================= |
16 | | -* botToken |
17 | | -* chatID |
18 | | - |
19 | | -If you'll use with authentication, change too |
20 | | - |
21 | | -* XXXUSERNAME |
22 | | -* XXXPASSWORD |
23 | | - |
24 | | -Disabling authentication |
25 | | -======================== |
26 | | -On flaskAlert.py change app.config['BASIC_AUTH_FORCE'] = True to app.config['BASIC_AUTH_FORCE'] = False |
27 | | - |
28 | | -Alertmanager configuration example |
29 | | -================================== |
30 | | - |
31 | | - receivers: |
32 | | - - name: 'telegram-webhook' |
33 | | - webhook_configs: |
34 | | - - url: http://ipFlaskAlert:9119/alert |
35 | | - send_resolved: true |
36 | | - http_config: |
37 | | - basic_auth: |
38 | | - username: 'admin' |
39 | | - password: 'password' |
40 | | - |
41 | | -One way to get the chat ID |
42 | | -========================== |
43 | | -1) Access https://web.telegram.org/ |
44 | | -2) Click to specific chat to the left |
45 | | -3) At the url, you can get the chat ID(Ex: https://web.telegram.org/#/im?p=g1234567, so the chatID is 1234567) |
46 | | - |
47 | | -Running |
48 | | -======= |
49 | | -* python flaskAlert.py |
50 | | - |
51 | | -Running on docker |
52 | | -================= |
53 | | - git clone https://github.com/nopp/alertmanager-webhook-telegram.git |
54 | | - cd alertmanager-webhook-telegram/docker/ |
55 | | - docker build -t alertmanager-webhook-telegram:1.0 . |
56 | | - |
57 | | - docker run -d --name telegram-bot \ |
58 | | - -e "bottoken=telegramBotToken" \ |
59 | | - -e "chatid=telegramChatID" \ |
60 | | - -e "username=<username>" \ |
61 | | - -e "password=<password>" \ |
62 | | - -p 9119:9119 alertmanager-webhook-telegram:1.0 |
63 | | - |
64 | | -Example to test |
65 | | -=============== |
66 | | - curl -XPOST --data '{"status":"resolved","groupLabels":{"alertname":"instance_down"},"commonAnnotations":{"description":"i-0d7188fkl90bac100 of job ec2-sp-node_exporter has been down for more than 2 minutes.","summary":"Instance i-0d7188fkl90bac100 down"},"alerts":[{"status":"resolved","labels":{"name":"olokinho01-prod","instance":"i-0d7188fkl90bac100","job":"ec2-sp-node_exporter","alertname":"instance_down","os":"linux","severity":"page"},"endsAt":"2019-07-01T16:16:19.376244942-03:00","generatorURL":"http://pmts.io:9090","startsAt":"2019-07-01T16:02:19.376245319-03:00","annotations":{"description":"i-0d7188fkl90bac100 of job ec2-sp-node_exporter has been down for more than 2 minutes.","summary":"Instance i-0d7188fkl90bac100 down"}}],"version":"4","receiver":"infra-alert","externalURL":"http://alm.io:9093","commonLabels":{"name":"olokinho01-prod","instance":"i-0d7188fkl90bac100","job":"ec2-sp-node_exporter","alertname":"instance_down","os":"linux","severity":"page"}}' http://username:password@flaskAlert:9119/alert |
| 6 | +GO version: https://github.com/nopp/alertmanager-webhook-telegram-go |
| 7 | + |
| 8 | +## Project structure |
| 9 | + |
| 10 | +``` |
| 11 | +├── app/ |
| 12 | +│ ├── __init__.py # app factory (create_app) |
| 13 | +│ ├── config.py # config from env vars |
| 14 | +│ ├── alerts.py # alert message builder |
| 15 | +│ ├── telegram.py # Telegram HTTP client |
| 16 | +│ └── routes.py # Flask blueprint (/alert) |
| 17 | +├── wsgi.py # gunicorn / dev entrypoint |
| 18 | +├── requirements.txt |
| 19 | +└── docker/ |
| 20 | + ├── Dockerfile |
| 21 | + └── run.sh |
| 22 | +``` |
| 23 | + |
| 24 | +## Requirements |
| 25 | + |
| 26 | +- Python 3.11+ |
| 27 | +- pip packages: see `requirements.txt` |
| 28 | + |
| 29 | +## Configuration |
| 30 | + |
| 31 | +All config via environment variables — no hardcoded secrets. |
| 32 | + |
| 33 | +| Variable | Required | Default | Description | |
| 34 | +|---|---|---|---| |
| 35 | +| `TELEGRAM_BOT_TOKEN` | ✅ | — | Bot token from @BotFather | |
| 36 | +| `TELEGRAM_CHAT_ID` | ✅ | — | Target chat/group ID | |
| 37 | +| `BASIC_AUTH_USERNAME` | ❌ | `""` | HTTP basic auth username | |
| 38 | +| `BASIC_AUTH_PASSWORD` | ❌ | `""` | HTTP basic auth password | |
| 39 | +| `BASIC_AUTH_FORCE` | ❌ | `true` | Set `false` to disable auth | |
| 40 | +| `SECRET_KEY` | ❌ | random | Flask secret key | |
| 41 | +| `MAX_RETRIES` | ❌ | `3` | Telegram send retries | |
| 42 | + |
| 43 | +## Install & run locally |
| 44 | + |
| 45 | +```bash |
| 46 | +pip install -r requirements.txt |
| 47 | + |
| 48 | +export TELEGRAM_BOT_TOKEN=your_token |
| 49 | +export TELEGRAM_CHAT_ID=your_chat_id |
| 50 | +export BASIC_AUTH_USERNAME=admin |
| 51 | +export BASIC_AUTH_PASSWORD=secret |
| 52 | + |
| 53 | +# dev |
| 54 | +python wsgi.py |
| 55 | + |
| 56 | +# production |
| 57 | +gunicorn -w 4 -b 0.0.0.0:9119 wsgi:app |
| 58 | +``` |
| 59 | + |
| 60 | +## Docker |
| 61 | + |
| 62 | +```bash |
| 63 | +cd docker/ |
| 64 | +docker build -t alertmanager-webhook-telegram . |
| 65 | + |
| 66 | +docker run -d --name telegram-webhook \ |
| 67 | + -e TELEGRAM_BOT_TOKEN=your_token \ |
| 68 | + -e TELEGRAM_CHAT_ID=your_chat_id \ |
| 69 | + -e BASIC_AUTH_USERNAME=admin \ |
| 70 | + -e BASIC_AUTH_PASSWORD=secret \ |
| 71 | + -p 9119:9119 \ |
| 72 | + alertmanager-webhook-telegram |
| 73 | +``` |
| 74 | + |
| 75 | +## Alertmanager config example |
| 76 | + |
| 77 | +```yaml |
| 78 | +receivers: |
| 79 | + - name: telegram-webhook |
| 80 | + webhook_configs: |
| 81 | + - url: http://your-host:9119/alert |
| 82 | + send_resolved: true |
| 83 | + http_config: |
| 84 | + basic_auth: |
| 85 | + username: admin |
| 86 | + password: secret |
| 87 | +``` |
| 88 | +
|
| 89 | +## Get your Telegram chat ID |
| 90 | +
|
| 91 | +1. Go to https://web.telegram.org/ |
| 92 | +2. Open the target chat |
| 93 | +3. Copy the numeric ID from the URL (e.g. `https://web.telegram.org/#/im?p=g1234567` → ID is `-1234567` for groups) |
| 94 | + |
| 95 | +## Test |
| 96 | + |
| 97 | +```bash |
| 98 | +curl -XPOST \ |
| 99 | + -u admin:secret \ |
| 100 | + -H 'Content-Type: application/json' \ |
| 101 | + --data '{ |
| 102 | + "status": "resolved", |
| 103 | + "alerts": [{ |
| 104 | + "status": "resolved", |
| 105 | + "labels": { |
| 106 | + "alertname": "instance_down", |
| 107 | + "instance": "i-0d7188fkl90bac100", |
| 108 | + "name": "prod-server" |
| 109 | + }, |
| 110 | + "annotations": { |
| 111 | + "summary": "Instance i-0d7188fkl90bac100 down", |
| 112 | + "description": "Host has been down for more than 2 minutes." |
| 113 | + }, |
| 114 | + "endsAt": "2024-01-01T16:16:19Z", |
| 115 | + "startsAt": "2024-01-01T16:02:19Z" |
| 116 | + }] |
| 117 | + }' \ |
| 118 | + http://localhost:9119/alert |
| 119 | +``` |
0 commit comments