send telegram notification using a bot
Project description
telegram-coffee-break
It's time to take a coffee break! your python code is running, and you'll be notified through telegram when you will have your results.
Goal
Create a telegram bot that will send you notifications on your phone to follow your code progress.
Installation
pip install telegram-coffee-break
Preparation
First: You will need to create a new telegram bot as follow:
Go to the BotFather (if you open it in desktop, make sure you have the Telegram app), then create new bot by sending the /newbot command. Follow the steps until you get the username and token for your bot. You can go to your bot by accessing this URL: https://telegram.me/YOUR_BOT_USERNAME and your token should looks like this.
7044NNNNN:AAEtcZ*************
Keep the token safe in a file and set your bot.
Next: You need to find your telegram ID
https://support.bigonechina.com/hc/en-us/articles/360008014894-How-to-get-the-Telegram-user-ID-
Go to the userinfobot and send /start. He will give you your personal ID.
Id: 871NNNNN
First: lol
Last: lolilol
Lang: en
Usage example
Simple example where you simply receive a notification at a certain point in the progress of the code.
from telegrambotalarm import TelegramBot
TOKEN = 'NNNNNNNNNN:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
MYID = 'NNNNNNNNN'
bot = TelegramBot(TOKEN, MYID)
bot.send_message('Your code is running')
... do modeling etc ...
message = {
train accuracy : train_acc,
test accuracy : test_acc
}
bot.send_message(message)
bot.send_image('decision_tree.png', caption=message)
Best example where you are informed if an error occurs during the execution of a piece of code.
def train_model():
## do something ##
results = {
'train accuracy' : train_acc,
'test accuracy' : test_acc
}
return results
from telegrambotalarm import TelegramBot
import traceback
TOKEN = 'nnnnnnnnnn:xxxxxxxxxxxxxxxxxxxxx'
MYID = 'nnnnnnnn'
bot = TelegramBot(TOKEN, MYID)
# Run this
try:
results = train_model()
# If error occurs, send the error with its trace
except Exception as e:
print(traceback.format_exc())
bot.send_error_message(traceback.format_exc())
bot.send_message(results)
bot.send_image('decision_tree.png', caption=message)
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
Hashes for telegram-coffee-break-0.1.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5823f2a2bd1a4c7033751beca8619e2e983d857420b47b8f9c63f8d1e3093457 |
|
MD5 | a32fe6a72c6c2b2876368ee21e41727f |
|
BLAKE2b-256 | aee6a542924973adcd43f05459be1c198fd85046d991dda66f74e53a96559d7a |
Hashes for telegram_coffee_break-0.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9cda79adc6a7cbd8adb567806eb0c1a4803903e4f735d1a23378c7395b529599 |
|
MD5 | 2975727d75d99ca900ba860d3d931549 |
|
BLAKE2b-256 | 771b8b9f1b0b9a99274271ce9170c86d48c5d0883a69f5cd64f9cbc84381236a |