Skip to content

Instantly share code, notes, and snippets.

@zachlim98
Created November 23, 2020 10:53
Show Gist options
  • Save zachlim98/0f3f0668b3e2fd954e33e6a717d7cfa5 to your computer and use it in GitHub Desktop.
Save zachlim98/0f3f0668b3e2fd954e33e6a717d7cfa5 to your computer and use it in GitHub Desktop.

Introduction

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 probably 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:

doggy

The Concept

So, 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.

Imgur

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.

Screenshot_1

Screenshot_2

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).

Screenshot_3

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

https://gist.github.com/b10cb503a4c60db0db48b3e2cec8ca07

Drag this down the entire list and you have your unique identifiers for each image.

Screenshot_4

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 ugly!)

https://gist.github.com/fca86b3b0873dd20b897a0ca7966e126

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

https://gist.github.com/487051ad7d00cb92063ecc912a8a4afb

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.

https://gist.github.com/7e28242fa0716af160ff60f6b984a45c

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.

https://gist.github.com/42348d6219bf3786b85196b6f88d43fc

The "start" and "photo" commands were 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment