How to Write a Telegram Bot to Send Messages with Python
This guide will show you the easiest way to use Python to send Telegram messages. I did this because I train machine learning models that take hours, and I hate coming back to my work computer and checking to make sure everything is okay. It works just as well for any code that takes a long time. Go out, take a walk, get updates and a message when it’s all finished.
Step 1: Speak with “the BotFather”:
Click this link and it will open your telegram client* and start a chat with the BotFather. Send the message “/newbot” (no quotes) and follow the instructions.
*you’ll need to sign up for an account if you don’t already have one
Step 2: Install telegram-send and link it to your bot:
To install, open a terminal and run pip install telegram-send
followed by telegram-send configure
telegram-send will ask for the token you got from the botfather, and then give you a password that you need to message to your new bot on Telegram.
Step 3: Send a test message from your python code
import telegram_send
telegram_send.send(messages=["Wow that was easy!"])
That’s really all there is to it. You now have a function you can use to send messages from inside your python code. For most of you that’s all you’ll need, but in Part 2, I’ll go a bit deeper with a few real-world examples, like a callback in pytorch-lightning, and making it work in Google Colab.