Skip to main content

Small package for working with config files

Project description

telegram_handler

Telegram logging handler for logging library in python.

The Telegram log handler sends log messages directly to either a Telegram channel or chat of your choice.

Installation

pip install telegram-handler

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()

Preparation

In order to use the package you should:

  • Create a bot, you can see how this is done here.
  • Create a channel, you can see how this is 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.4.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

telegram_handler-1.4.4-py3-none-any.whl (4.5 kB view hashes)

Uploaded Python 3

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