Sending Alert Notifications from Alertmanager to Telegram Bot

  ·   3 min read

Alerting is a critical component of a reliable monitoring strategy in any DevOps environment. Prometheus, an open-source monitoring and alerting toolkit, uses Alertmanager to handle alerts generated by Prometheus servers. Integrating Alertmanager with a Telegram bot allows you to receive instant notifications in your messaging platform, making it easier to respond to incidents on the fly. In this article, I’ll guide you through the process of setting up Alertmanager to send notifications to a Telegram bot.

Prerequisites

  1. Prometheus: Ensure you have Prometheus and Alertmanager installed and configured.
  2. Telegram Account: You need a Telegram account to create a bot and access your chat.

Step 1: Create a Telegram Bot

  1. Open your Telegram app and search for the BotFather.
  2. Start a chat with the BotFather and send the command /newbot.
  3. Follow the prompts to choose a name and a username for your bot.
  4. Once created, the BotFather will provide you with a token. Save this token as it will be needed in the configuration.

Step 2: Find Your Chat ID

To send messages to a specific chat, you need the chat ID. Here’s how to get it:

  1. Search for your bot in Telegram and start a conversation with it.

  2. Send a message (e.g., “Hello”).

  3. Use a browser to call the Telegram API to retrieve your chat ID. Replace <YOUR_BOT_TOKEN> with the token you received from BotFather:

    https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
    
  4. Look for the chat object in the JSON response. The id field inside this object is your chat ID.

Step 3: Configure Alertmanager

Now, let’s configure Alertmanager to send alerts to your Telegram bot.

  1. Open the Alertmanager configuration file (alertmanager.yml).

  2. Add a new receiver under the receivers section to configure Telegram notifications:

    receivers:
    - name: 'telegram'
      telegram_configs:
      - chat_id: '<YOUR_CHAT_ID>'  # The chat ID you retrieved earlier
        bot_token: '<YOUR_BOT_TOKEN>'  # The bot token from BotFather
        send_resolved: true  # Optional: Send notifications when alerts resolve
    
  3. Next, associate the receiver with a route in the route section:

    route:
      group_by: ['alertname']  # Group alerts by alertname
      group_wait: 30s  # Time to wait to group alerts
      group_interval: 5m  # Time to wait before sending a new notification
      repeat_interval: 3h  # Time to wait before resending the same notification
      routes:
      - match:
          severity: critical
        receiver: 'telegram'
    

Step 4: Reload Alertmanager Configuration

After saving your changes in the alertmanager.yml, you can reload the configuration by either restarting the Alertmanager service or by sending a POST request to the Alertmanager API:

curl -X POST http://<ALERTMANAGER_IP>:<PORT>/api/v1/reload

Step 5: Test Your Setup

Create a test alert in Prometheus to ensure that your Telegram bot receives notifications. Here is a simple example of a test alert rule:

groups:
- name: example
  rules:
  - alert: TestAlert
    expr: vector(1)  # Always true for testing
    for: 10s
    labels:
      severity: critical
    annotations:
      summary: "Test Alert"
      description: "This is a test alert sent to Telegram."

Add this alert rule to your prometheus.yml configuration and reload Prometheus.

Conclusion

Integrating Alertmanager with a Telegram bot not only enhances your monitoring capabilities but also provides a straightforward way to receive notifications directly in your messaging tool. This setup allows DevOps teams to react quickly to potential incidents, minimizing downtime and improving overall service reliability.

References

By following the steps outlined above, you’ll be well on your way to making your alerting more effective while utilizing your preferred communication platforms.