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
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 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.1.tar.gz
.
File metadata
- Download URL: telegram-handler-1.4.1.tar.gz
- Upload date:
- Size: 3.5 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3d2ab35a9e87347e211e1183df808eadb91a847d1ee58109f6b87f8c0ef4671 |
|
MD5 | 05e1c15d6c526e237748b21894e89268 |
|
BLAKE2b-256 | 36f3e3104ec899a2b62245f59884e333653f3d9c5fddb7e1347862bfb302880b |
File details
Details for the file telegram_handler-1.4.1-py3-none-any.whl
.
File metadata
- Download URL: telegram_handler-1.4.1-py3-none-any.whl
- Upload date:
- Size: 3.9 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4f07fbff707c4c7b1eeab9e72b4a7ff40bca45ddb6def3878ef0d1b52a37bf6 |
|
MD5 | 5fcce4740195e8db76160bded89a5218 |
|
BLAKE2b-256 | 50266f7cf7fbb95019c1269370a8abec5c60fcf325b3a6700272450af1c7995c |