How to Write a Telegram Bot to Send Messages with Python

Made-Up Masters
2 min readJul 28, 2020

--

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

I was making this for use with Pytorch-Lightning, but it isn’t required

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.

Send the token you got from the botfather and telegram-send will reply with a 5-digit password you need to send to your new bot.
Forwarding the password to my new lightningbot

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.

Links and Resources

  1. Telegram-send documentation (command-line)
  2. Telegram-send documentation (Python, very limited)

--

--

Made-Up Masters

All the skills of going back to school, with none of the debt.