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
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 theBotFather
when creating the bot.
channel_name
- Each chat in Telegram havechat ID
.- Channel name is the
chat ID
for public channels. So for the public channelexample_channel_logger
thechat id
will beexample_channel_logger
- The
channel_name
can be anychat id
, you can see how to obtain chat id here.
- Channel name is the
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)
Built Distribution
Close
Hashes for telegram_handler-1.4.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 573d473b91b5eae35a67d41b54e69f908662c5e7c0ea4cc4ef44d669b7c13036 |
|
MD5 | 6e3c755eca4e9a7c9c5d5dc840ad0c87 |
|
BLAKE2b-256 | a9348e12c43b9f815f851ed17a18665c3c152febd6a99b6deb0aff53ca0aedf6 |