How I follow my experiments progress with Telegram
Published:
Introduction
During my master’s degree, I was constantly multitasking between experiments and other everyday activities. I was often worried about whether the experiments were still running, checking if the results were correct, or even if they had finished. However, keeping my eyes on the screen and staying constantly alert was dull, time-consuming, and sometimes stressful.
I decided to change during my Ph.D. I thought: What if I created a script that sends me a message every time an experiment ends? Well, I did that. I used a Telegram bot to send messages about the progress of an specific experiment. It sends me an image of a plot made with matplotlib
summarizing the results with other statistics in text format. The figure below is an example of a message sent by the bot.
To do this I used the following technologies:
python 3.12
requests==2.32.4
Telegram bot
To create the telegram bot was simple. I just started a new conversation with @BotFather
as the figure below.
As you can see it returned a token used to interact with the bot and Telegram, we’ll need to save it for later. I omitted for obvious reasons. To see the available methods for the bots, please refer to https://core.telegram.org/bots/api#available-methods
. As I wanted to send, primarly, text messages, in this post I’ll only cover the sendMessage
method.
To create a middleware between me and the bot, I created a private channel called ‘blog
’ as the figure below.
Finally, I restricted saving content from the channel.
Now we need to subscribe our bot to the chat and get the chat ID. To subscribe our bot to our channel is simple: you only need to go to the Channel info > Subscribers > Add Subscribers
and type the bot’s username (in my case, hrchlhck_bot
). Finally, add the bot and make it admin with least privileges.
Now lets get the chat ID. It is possible to get through the telegram web version https://web.telegram.org
. You’ll just need to authenticate into your account, then open the blog
channel. You can see at the URL bar something like https://web.telegram.org/k/#-285.....
. Copy the number after #
without the sign and save it. Let’s integrate with the code.
Integrating with experiments
To integrate Telegram with the experiments, first I downloaded the requests
package using pip install requests
. Then, I created the following snippet:
import requests
TOKEN = "" # Telegram bot token
CHAT_ID = "-100<REPLACE WITH THE CHAT ID HERE>"
BASE_URL = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={CHAT_ID}"
def notify(msg: str) -> dict:
url = BASE_URL + f"&text={msg}"
return requests.get(url).json()
if __name__ == '__main__':
resp = notify('Hello, blog!')
print(resp)
The following snippet produced the following output:
{'ok': True, 'result': {'message_id': 4, 'sender_chat': {'id': OMITTED, 'title': 'blog', 'type': 'channel'}, 'chat': {'id': OMITTED, 'title': 'blog', 'type': 'channel'}, 'date': 1751588235, 'text': 'Hello, blog!', 'has_protected_content': True}}
In the telegram channel, as the following figure, the message arrived.
Finally, to integrate with the experiments, I just imported the notify
function and made some tweaks to send images (sendPhoto
).
Done! If you have any questions, please do not hesitate to contact me through pedro@prhrck.com.