butorov / sentry-telegram

Plugin for Sentry which allows sending notification via Telegram messenger.
MIT License
250 stars 64 forks source link
crash-reporting crash-reports error-monitoring hacktoberfest sentry sentry-plugin telegram telegram-api telegram-bot

Sentry Telegram Build Status Coverage Status PyPI PyPI - Downloads

Plugin for Sentry which allows sending notifications via the Telegram messenger.

As any plugins installation is only available for self-hosted Sentry instances, this plugin is unavailable for cloud-hosted Sentry.

The plugin has been tested with the most recent version of Sentry available at the time - 24.5.1.

Features

How will it look

How will it look

Compatible versions

DISCLAIMER: Sentry API is under development and is not frozen. I'm trying to keep up with the latest changes, but I can't guarantee compatibility with every version of Sentry. If you have any issues, please create an issue on GitHub.

Table of compatibility:

Plugin version Compatible Sentry versions Tested in Sentry versions
0.6.0 24.5.1, 24.6.x 24.5.1
0.5.0 24.4.x to 24.5.0 24.4.1, 24.4.2, 24.5.0
0.4.0 8.x to 9.x 8.9, 9.1.1
0.3.0 8.x to 9.x 8.21, 8.22, 9.0.0, 9.1.0
0.2.2 8.x to 9.x 8.21, 8.22, 9.0.0
0.2.1 8.x to 9.x 8.21, 8.22, 9.0.0
0.2.0 8.x 8.9, 8.10, 8.11, 8.12, 8.13, 8.14, 8.15, 8.16, 8.17
0.1.2 8.x 8.9.0, 8.12.0
0.1.1 8.x 8.9.0

Installation

[!TIP] You can initiate the plugin installation both before and after setting up a Sentry instance.

  1. On the host machine, where you have the self-hosted Sentry repository cloned, copy sentry/enhance-image.example.sh file to sentry/enhance-image.sh:
    cp sentry/enhance-image.example.sh sentry/enhance-image.sh
  2. Open sentry/enhance-image.sh file in your favorite text editor and add the following line to the end of the file:

    pip install sentry-telegram

    Or apply this command:

    echo "pip install sentry-telegram" >> sentry/enhance-image.sh

    So, the file will look like:

    #!/bin/bash
    # Enhance the base $SENTRY_IMAGE with additional dependencies, plugins - see https://github.com/getsentry/self-hosted#enhance-sentry-image
    # For example:
    # apt-get update
    # apt-get install -y gcc libsasl2-dev python-dev libldap2-dev libssl-dev
    # pip install python-ldap
    
    pip install sentry-telegram

    If you want to install a specific version of the plugin, you can specify it in the command (for example, version 0.6.0):

    echo "pip install sentry-telegram==0.6.0" >> sentry/enhance-image.sh
  3. Run ./install.sh script to build the Sentry image with the plugin installed:
    ./install.sh
  4. After the script finishes, you can start the Sentry instance, or if it was already running, restart it.
    • For the first start, you need to run the following command:
      docker compose up -d
    • If the Sentry instance was already running, it's enough to restart several services:
      docker compose restart web worker cron sentry-cleanup

Configuration

Adding the plugin to a project

From the Organization Settings

  1. Go to your Sentry web interface. Open Settings page, that located in the left sidebar (<your-sentry-installation-url>/settings/sentry/).
  2. In the "Organization" section of the second sidebar, go to the "Integrations" page (<your-sentry-installation-url>/settings/sentry/integrations/).
  3. Find the "Telegram Notifications" plugin in the list of integrations and click on it.
  4. Press the "Add to Project" button. You will be prompted to select a project to which you want to add the plugin (unless you have only one project).
  5. You will be redirected to the plugin configuration page for the selected project.

From the Project Settings

  1. Go to your Sentry web interface. Open the Settings page of one of your projects (<your-sentry-installation-url>/settings/sentry/projects/<project-name>/).
  2. In the second sidebar, go to the "Legacy Integrations" section (<your-sentry-installation-url>/settings/sentry/projects/<project-name>/plugins/).
  3. Find the "Telegram Notifications" plugin in the list of integrations and select the "Configure plugin" link at the bottom of the plugin card.
  4. Press the "Enable Plugin" button.

Configuring the plugin

  1. Assume you are on the plugin configuration page.
  2. You need to create a Telegram bot to get the Bot API token. To do this, you need to talk to the BotFather in Telegram. You can find more detailed instructions on how to create a bot in the official Telegram documentation.
  3. After you have created a bot, copy the Bot API token and paste it into the "Bot API token" field on the plugin configuration page.
  4. In the Receivers field, you need to specify the list of Telegram users and/or groups that will receive notifications. You can specify multiple receivers, each on a new line. You can use user/chat IDs, but not usernames.
    • To obtain the user ID, use can use one of the bots that can provide you with your user ID. For example, this one or this one (I don't have any relation to these bots) or any other, which you trust. Of course, you can use your own Bot API token to obtain the user ID.
    • To obtain the chat ID the easies way is to use "Share" button in the Telegram app and get the chat ID from the link. For example, if you share the chat with yourself, you will get the link like https://t.me/c/123456789. The chat ID in this case is -123456789. According to the Telegram Bot API requirements, you need to specify the chat ID with the minus sign (-) at the beginning.
    • If you have a chat with Topics, you can get the chat ID from the link as well. You need to share the particular Topic. For example, if you got a link https://t.me/c/123456789/5, the chat ID is 123456789 and the Topic ID is 5. The receiver in this case will be -123456789/5.
    • Don't forget that all the receivers should allow the bot to send messages to them.
      • For personal chats, it's enough to start a chat with the bot.
      • For groups, the bot should be added to the group and have a permission to send messages.
  5. Set the "Message Template" as you want, or start with the default one. You can use the following placeholders in the message template:

    • {project_name} - the name of the project where the issue occurred.
    • {url} - the URL to the issue in the Sentry web interface.
    • {title} - the title of the issue.
    • {message} - the error message of the issue.
    • tag[<tag_name>] - the value of the tag with the name <tag_name>. For example, tag[level]. If the tag is not found, the placeholder will be replaced with [NA].

    Note that the Telegram message will be sent in Markdown format, so you can use Markdown formatting in the message template.

  6. Press the "Save Changes" button.
  7. Now you can test the plugin by pressing the "Test Plugin" button. You should receive a test message in the Telegram chat(s).
  8. If you received the test message to Telegram and got Test Results "No errors returned" message at the top of the plugin configuration page, you are all set up!

[!TIP] Don't forget to check the Sentry's alert rules to make sure that the Sentry will send notifications for the events you want (new issues only, or all issues, etc.).

Support This Project

If you find this project useful and would like to contribute financially, you can find several ways to do so on my GitHub Sponsors page. Your support will assist in covering the costs of the servers used for testing the plugin and will serve as motivation for further improvements.

Your support is highly appreciated!