A python library to log messages and exceptions to your Telegram bot. This package has zero dependencies to external libraries and only relies on Python's built-in features.
yatlogger registers itself as a handler for the built-in logging module. Here is an example:
import logging
import yatlogger
logger = yatlogger.register()
logger.setLevel(logging.INFO)
logger.info("Read this on your phone!")
raise ValueError("This unhandled exception will be sent to Telegram, too!")
And the resulting chat messages:
First, create a new bot. It's basically sending some messages to @BotFather.
Next, create a file named .yatlogger.json
and place it in the same directory as your code or in one of the parent directories. The file must look like this:
{
"token": "<your api key>"
}
Replace <your api key>
with the API key you got from the BotFather.
Your bot must know to which chats it should send the logs. So the next step is to register receiving chats.
Run python -m yatlogger
to start the register service. As long as this service is running, you can register new chats.
To register a chat, start a chat with your bot and enter the 6 digit pin you see in the console output.
When you are done, stop the register service with Ctrl + C