A custom Telegram Bot to share your precious photos (and allow users to filter by theme!)

An example of how you can use the “Reply Keyboard” function in Python-Telegram-Bot to create a simple thematic “photo album”

Zachary Lim
Towards Data Science

--

Ever wanted to create a Telegram bot that would allow you to share your photos with the rest of the world? Not really? Well, after you’re done with this article, you’ll probably think differently. And if not… well, you’re just wrong.

In this article, we will look at using the Reply Keyboard in Telegram (pictured below) to create our very own bot that sends photos to users based on categories. This article builds off an earlier article on a more basic use of a Telegram bot so look at that first if you've never had any experience building a Telegram bot.

Here’s how the final product will look like:

The Concept

The concept is simple. We will store our photos on imgur and then sort these photos into categories. We will then link the photos and categories in a pandas dataframe and then filter the photos based on user input, sending only those filtered photos back to the users.

Step 1: Uploading your photos

This part is relatively easy. Just create an account on imgur and upload dem photos. For my case, I was creating a bot to send photos of my dog.

Isn’t she sweeet?

Step 2: Categorizing your photos

Once you’re done with uploading your photos, you need to get the direct links of the photos. The following screenshots show you how that’s easily done in imgur.

Once you’ve grabbed your direct links, throw them into excel. Of course, you could probably do it in Python but I decided to go with Excel just because it was simpler (at least in my head).

With the links in excel, you want to use the SUBSTITUTE function to get rid of the front part of the url.

Drag this down the entire list and you have your unique identifiers for each image. The next step then was to categorize each photo with a certain tag. As you can see, I was very creative with my tags.

Step 3: Writing the bot

3.1 Creating the Reply Keyboard and Tagging

After all the basic stuff of importing your libraries and enabling logging (which can be found at this article), create your Reply Keyboard. Each list represents one row in the keyboard, so create as many rows as you need. Don’t try to cram too many items into a single row because it will cause the text to be compressed (and it looks ugly!)

I then imported the csv we created earlier as a dataframe.

3.2 Writing the functions

For this simple bot, I had 3 functions — the standard “start” function, a “photo” function to get the user input, and a “reply” function to send the photos.

As you can see, it’s not a lot of code. The main chunk of code is at the end (and even then, ‘chunk’ is a stretch). We use a for loop to cycle through the filtered list of photos and then tag them on to the imgur header before sending those photos out.

3.3 Creating the message handlers

After I created the individual functions, I had to add them to the main function through CommandHandler and MessageHandler.

The “start” and “photo” commands are your regular type of command. The MessageHandler for replying with photos was the interesting one. I used a regex (regular expression) filter to filter all the messages being sent by the user for the categories that I had decided upon for my photos. When these phrases were detected, the "reply" function would be called, starting the whole process of sending photos.

Conclusion

So there you have it, a really simple way to create a little Telegram Bot-photo album that you can share with family and friends! Thanks for reading!

Originally published at http://zachlim98.github.io/me.

--

--

Hi! I’m learning to explore data and think about personal finance (not always in that order)