Skip to main content

A Django app to send errors to telegram bot.

Project description

Django Telegram Error Notifications

At first you have to register your bot first before using it. Go to the BotFather or find it in telegram, then create new bot by sending the /newbot command. Follow the steps until you get the username and token for your bot.

To install app perform the command:

pip install django-telegram-error-notifications

Add application to your INSTALLED_APPS:

INSTALLED_APPS = [
    ...,
    'telegram_error_notifications.apps.TelegramErrorNotificationsConfig'
]

After this you should apply migrations:

python manage.py migrate

By this action you will add two new models Telegram Bot and Error Messages.

Then add to the settings.py settings for your telegram bot:

TELEGRAM_BOT_NAME = '<Your Bot Name>'
TELEGRAM_BOT_TOKEN = '<Your Bot Token>'
# If you override BotView and set up another URL provide your URL
# Also you can use ngrok utility when you work on local server. 
# Perform `ngrok http 8000` and take URL with https. For example:
# TELEGRAM_BOT_WEBHOOK_URL = https://0adb2add.ngrok.io/bot/web-hook/
# Whenever you change this variable don't forget to perform 
# `python manage.py create_telegram_bot` to reassign a webhook
TELEGRAM_BOT_WEBHOOK_URL = 'https://<your-domain>/bot/web-hook/'
TELEGRAM_BOT_PROJECT_NAME = '<Name of the current project>'
TELEGRAM_BOT_ALLOW_SEND_IN_DEBUG_MODE = False
  • TELEGRAM_BOT_NAME - Name of your bot that will be used for creating it into a database
  • TELEGRAM_BOT_TOKEN - Token that was given to you when you created your bot
  • TELEGRAM_BOT_WEBHOOK_URL - This url will receive requests from your bot
  • TELEGRAM_BOT_PROJECT_NAME - This variable is necessary to provide your project name when the error will be sent to your bot
  • TELEGRAM_BOT_ALLOW_SEND_IN_DEBUG_MODE - If you want to send errors to your bot in DEBUG mode switch this variable to True

Then you should specify url in your urls.py:

from django.urls import path, include

urlpatterns = [
    ...,
    path('bot/', include('telegram_error_notifications.urls')),
]

To create a bot into a database and create a webhook for it just use the management command:

python manage.py create_telegram_bot

This command will use TELEGRAM_BOT_NAME variable to create a bot

You should necessarily send from your bot any command. It's necessary for getting chat_id and saving it to database. Make sure you did it. For example you can send /start command. You will get I don't know this command. Don't worry. After this command your bot chat_id will be saved into a database.

You can add new commands for your bot. For example, you want to add /start command. Your view that will be responsible for handling telegram commands can be like this:

from telegram_error_notifications.utils import send_message
from telegram_error_notifications.views import BotView


class MyBotView(BotView):
    def handle_command_start(self):
        if self.bot and self.bot.chat_id:
            message = "<b>I started working</b>"
            return send_message(message, self.bot.chat_id)

To handle your own command you should create handle_command_<command> method.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-telegram-error-notifications-0.0.2.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django-telegram-error-notifications-0.0.2.tar.gz.

File metadata

  • Download URL: django-telegram-error-notifications-0.0.2.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.3

File hashes

Hashes for django-telegram-error-notifications-0.0.2.tar.gz
Algorithm Hash digest
SHA256 bdb549de65a98e6cb7e6fd007743f5de4709395d560fae00258294e559dbdb4d
MD5 88e43178ae6563f5c051a27a3f6069e8
BLAKE2b-256 59d01caa8a4486811157e7a65da10cafa2dc17c6a45e01d5099f30900d6699a1

See more details on using hashes here.

File details

Details for the file django_telegram_error_notifications-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: django_telegram_error_notifications-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.3

File hashes

Hashes for django_telegram_error_notifications-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c5d0a65096039b82d049d97263192a28cbc1a877f80ef45aeafb19d477a9e180
MD5 4089f33d0b89504d1ad08ca7f622c86a
BLAKE2b-256 85b90fe600a17aada9d4973530a58a92989b59eccba08b2d441dacb0cb17bda5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page