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
Built Distribution
File details
Details for the file telegram-handler-1.4.4.tar.gz
.
File metadata
- Download URL: telegram-handler-1.4.4.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7fba9f0c3c535ccb6f9eb759949e7f14336ff40a824a7b946a370d5f7fce086 |
|
MD5 | 3fcde3992734a873356d94ff28042e33 |
|
BLAKE2b-256 | 2ebf349e55b84e28aecaf79fe4c6db863406f245d12dbdb012734d447919dccf |
File details
Details for the file telegram_handler-1.4.4-py3-none-any.whl
.
File metadata
- Download URL: telegram_handler-1.4.4-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 573d473b91b5eae35a67d41b54e69f908662c5e7c0ea4cc4ef44d669b7c13036 |
|
MD5 | 6e3c755eca4e9a7c9c5d5dc840ad0c87 |
|
BLAKE2b-256 | a9348e12c43b9f815f851ed17a18665c3c152febd6a99b6deb0aff53ca0aedf6 |