async telegram handler
Project description
telegram_handler
Logging handler that sends log messages directly to either a telegram channel or chat
Motivation
Tracking program execution state remotely - directly from your telegram account
Screenshots
Code Examples
Basic usage example:
import logging
from telegram_handler import AsyncTelegramHandler
BOT_TOKEN = '1612485124:AAFW9JXxjqY9d-XayMKh8Q4-_iyHkXSw3N8'
CHANNEL_NAME = 'example_channel_logger'
def main():
telegram_log_handler = AsyncTelegramHandler(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 AsyncTelegramHandler
BOT_TOKEN = '1612485124:AAFW9JXxjqY9d-XayMKh8Q4-_iyHkXSw3N8'
CHANNEL_NAME = 'example_channel_logger'
def main():
telegram_log_handler = AsyncTelegramHandler(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?
There are 2 possibilities:
- Use
AsyncTelegramHandler
and send messages from a different thread (recommended) - Use
BlockingTelegramHandler
and send messages from the same thread.
:warning: warning: sometimes the logger should wait for a cooldown (telegram constrains), using this handler will block the entire program
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.3.tar.gz
(3.6 kB
view details)
Built Distribution
File details
Details for the file telegram-handler-1.3.tar.gz
.
File metadata
- Download URL: telegram-handler-1.3.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b2cbb3ec897a49134eefd93df3b4b6786ff2d035cb9d844f78e6531fa0f34e1 |
|
MD5 | edf18287ca808576e454eadff84e8f1c |
|
BLAKE2b-256 | 9b1ca7c7d4f31031f85b97a7a96bb80ba516c76ea71f140106574a2986daac7f |
File details
Details for the file telegram_handler-1.3-py3-none-any.whl
.
File metadata
- Download URL: telegram_handler-1.3-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 177913480a59627181662be4048b80b41d6150e49ffdf3d63f3beedcccd2396e |
|
MD5 | 6ffc23b57153c7be0b2ecb32988db547 |
|
BLAKE2b-256 | 636b0873bbb407e1771a37f9c1e77ad850eb9f67068fe34851718c10d0e4a743 |