Skip to main content

async telegram handler

Project description

telegram_handler

Telegram logging handler for logging library in python.

Telegram log handler sends log messages directly to either a telegram channel or chat for your choice

Motivation

Tracking program execution state remotely - directly from your telegram account

Screenshots

screenshot

Code Examples

Basic usage example:

import logging

from telegram_handler import TelegramLoggingHandler

BOT_TOKEN = '1612485124:AAFW9JXxjqY9d-XayMKh8Q4-_iyHkXSw3N8'
CHANNEL_NAME = 'example_channel_logger'


def main():
    telegram_log_handler = TelegramLoggingHandler(BOT_TOKEN, CHANNEL_NAME)
    my_logger = logging.getLogger('My-Logger')
    my_logger.setLevel(logging.INFO)
    my_logger.addHandler(logging.StreamHandler())
    my_logger.addHandler(telegram_log_handler)

    for i in range(5):
        my_logger.error(f'iterating {i}..')


if __name__ == '__main__':
    main()

Another option is to add the handler to the root logger:

import logging

from telegram_handler import TelegramLoggingHandler

BOT_TOKEN = '1612485124:AAFW9JXxjqY9d-XayMKh8Q4-_iyHkXSw3N8'
CHANNEL_NAME = 'example_channel_logger'


def main():
    telegram_log_handler = TelegramLoggingHandler(BOT_TOKEN, CHANNEL_NAME)
    logging.basicConfig(
        handlers = [
            telegram_log_handler
        ],
        level=logging.INFO,
        format='%(asctime)s | %(levelname)s | %(name)s | %(message)s'
    )
    my_logger = logging.getLogger('My-Logger')
    for i in range(5):
        my_logger.error(f'iterating {i}..')


if __name__ == '__main__':
    main()

Installation

pip install telegram-handler

Preparation

In order to use the package you should:

  • Create a bot, you can see how this is being done here.
  • Create a channel, you can see how this is being done here.

How to use?

  • Use TelegramLoggingHandler and send messages from a different thread (recommended)

Parameters:

  • bot_token - The token that returns from the BotFather when creating the bot.
    bot_token
  • channel_name - Each chat in Telegram have chat id.
    • Channel name is the chat id for public channels. So for the public channel example_channel_logger the chat id will be example_channel_logger
    • The channel_name can be any chat id, you can see how to obtain chat id here.

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

telegram-handler-1.4.3.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

telegram_handler-1.4.3-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file telegram-handler-1.4.3.tar.gz.

File metadata

  • Download URL: telegram-handler-1.4.3.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/3.7.2 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for telegram-handler-1.4.3.tar.gz
Algorithm Hash digest
SHA256 2e77ce9ed158b649933f838df5e412cd9b6d2d9826f77a168e3193e628d9f794
MD5 b1714b686746ffec4265075f846b6179
BLAKE2b-256 0d3e12c4625dc401554c4755cbc4c58e23c5dd2050c669893d6bb9c7a350d202

See more details on using hashes here.

File details

Details for the file telegram_handler-1.4.3-py3-none-any.whl.

File metadata

  • Download URL: telegram_handler-1.4.3-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/3.7.2 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for telegram_handler-1.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 493f7b972a4834bf5595cbc25137aee107f9bdb25e4e26a299f6b6e6ade0abdc
MD5 8f83f54a0b073d7b532730789068d0ed
BLAKE2b-256 e346116a34c00c8ef7ea14af3241698f7bf20626cd5dae0e0aa6febbbfe49a78

See more details on using hashes here.

Supported by

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