Telegram API with python

Create a simple Bot with Telegram that notifies you about the progress of your code.

How to start with the Telegram API and Python to receive messages or images.

Romain Gratier
Towards Data Science
5 min readDec 9, 2020

--

The motivation behind this post

For a while, I had a small project in mind! I wanted to find an easy way to get notified of my python coding pipelines' progress while they were running. I was sure that a quick and robust possibility was available. But back in the days, I did not know about API, and I certainly did not expect that some of them were that easy to use. Therefore, in this post, I want to open your eyes to APIs possibilities and motivate you by introducing this small meaningful project.

First, we are going to talk about APIs. Specially, we will discuss why they are used and how they are built. Then we will dive into the project and its different steps.

APIs

During my journey as a data scientist, I have learned a lot of things. One of the most relevant outcomes is probably the importance of APIs. API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to communicate with each other. In other words, those pieces of software give can give you access to many applications as Telegram/Facebook/Twitter but also to large Database as well.

APIs work with a precise structure to be easily scalable and understandable. They typically utilize HTTP protocols and methods (i.e., GET, POST, DELETE), and if you want to learn more about them, I encourage you to start reading this.

The best way to realize it is to start playing with the API developed by Telegram? Telegram's API is an easy start to receive cool notifications without the need for third parties and complicated pipelines. Here I will use a simple POST method from the python package requests.

Quick project:

As Telegram’s API is friendly to use, we can quickly create exciting projects. This post will explain a small project that can notify you of your code's (good or bad) progress! You can pip install it here, and there is no need to deploy it on a server. You only need to set up a telegram bot and find your UserID.

Get Started

Setup of Telegram Bot

The goal here is to create a newbot with its features. I am describing the most important ones, but you can easily improve the bot using the documentation.

Search for BotFather in the search engine, and click on Start.

Image by author

Click on /newbot.

Image by author

You can go fancy with the name of your bot. But beware of the username! It should end up with a bot, and it should not already exist.

Image by author

At this stage, Telegram creates a bot for you and gives you access to your token. It will allow you to give it some orders through the API

Look for your userID

As the previous step, look for userinfobot.

Image by author

And press /start.

Image by author

You will now have access to your userId that will allow the bot to send messages directly to you.

A bit of Python

In this example, I am using the requests module from Python. It is reliable and robust, with a strong community behind it. As mentioned previously, I am using the POST method to send data to the API, contrary to the GET method, which allows you to ask for data access. If you want further information concerning APIs, feel free to go to this link.

The code snippet below shows how to send a message from the bot to our telegram account.

Now, if you want to send images from your local machine, the following code does the job perfectly.

The project

After playing around with the API, I remembered a simple idea that arose a year before. I was looking at results from a Kaggle competition, and a friend seated next to me had thought that I was receiving results from my code that just finished running. I remember thinking, “how cool it would be to set up something that creates a message with a caption and images about your code's progress?”.

The main part of the code is quite simple, and it allows us to send information not only to me but also to others if they share their userID as well. It allows you to do something else while a long code runs. Indeed, you don’t have to worry about checking every minute if the code is still running.

The code

The package's main module is easy to get and will give you a better idea of its structure.

How to use it:

Let me show you how you will be able to use it.

First pip install it! To leave no one behind, the command line below is coming from PyPI, which is THE package manager for python. If you are interested in knowing more about how it is structured and how to develop a python package, I encourage you to see this video.

pip install telegram-coffee-break

And you can use it as follows to also get notified if any problems happen during the pipeline process.

If you want to get a closer look at the code or add something, please check its GitHub page here.

Live example:

Image by author

Conclusion

I hope you liked this small article and that you learned something about APIs and their amazing possibilities. Don’t forget that we only speak here about the POST method, but I think you will be able to extrapolate your current knowledge to use other methods. I tried to introduce you to their structure without using any complicated wrappers. But if you think you are ready for the next steps, you can take a look at the most well-known wrappers for the Telegram API.

Next

The next steps will be :

  • find a better way to handle the token and the destination ID to avoid passing those to the function every time.
  • Develop a function to send notifications to multiple userID to spread the information within a group of programmers.

--

--

Software developer with a machine learning background. Currently working in the geospatial field.