DEV Community

Fox Larsson
Fox Larsson

Posted on

My first Python Telegram bot — and some tips if you want to follow the same tutorial

Woot-woopety-woot! I just finished writing my first Python Telegram bot!

Thanks a bunch to Dzaky Widya Putra for the tutorial that actually gave me the confidence (and detailed instructions) to give the thing a shot - and to my phone's AI for recommending it to me.

The bot is a simple one: it takes one command, "/boop", and returns a random cute doggo pic. The source of the images is the Random Dog API, and the bot works through the python-telegram-bot library.

I've been studying Python for two and a half weeks now, and all this time I was following the Hyperskill.org curriculum. So far I've completed all their beginner-level projects. So I have a bit of a basic understanding of Python, but no fancy skills.

I was worried that the chatbot project would be out of my scope since I hadn't used external libraries or APIs before. But after the official library tutorial and a bit of anxiety and trepidation, I found out that it's more fun than terrifying (with a pinch of debugging frustration).

You can check out my bot here - however, it currently works only when I launch the program on my computer. Deploying it so it can run continuously is my next plan (maybe Heroku?), I'm really curious about to do that.

If you want to try it yourself:

I think Dzaky's tutorial does a great job of getting you through the process. There were a few small things that inadvertently made me stumble, so I decided to share them here and explain.

Don't forget to install the requests library

Requests is not a built-in module in Python. This means you can't just import it, you have to install in first. Dzaky's tutorial mentions installing the python-telegram-bot library separately, but not requests, so it took me a while to figure out why things were not working, and I did spend some time half blankly staring at my code doing nothing. As you might imagine, reading the error message helped.

Pay attention to the Updater arguments

In the tutorial when you create your instance of the Updater class, it goes like this:

updater = Updater('YOUR_TOKEN')

For me, this threw a Deprecated error. As far as I understand, in the latest version of the library you also need to set use_context to True, like this:

updater = Updater('TOKEN', use_context=True)

If the tutorial's bop function is not working for you check the version from Github

When I was through with the first stage of the tutorial I fired up my bot, sent the command in Telegram and... nothing happened. Not even a hint of a dog pic.

I thought maybe I missed something, copied and pasted the sample code from the tutorial, tried again, and still - nothing.

I copied the code from the project's Github, and immediately booped a cute doggo into existence.

If the same happens to you, here's what I figured out after comparing the two version's line by line. Take a look at the bop function from the tutorial and the same block from the Github version:

def bop(bot, update):
    url = get_image_url()
    chat_id = update.message.chat_id
    bot.send_photo(chat_id=chat_id, photo=url)
def bop(update, context):
    url = get_image_url()
    chat_id = update.message.chat_id
    context.bot.send_photo(chat_id=chat_id, photo=url)

The Github version uses the new context-based syntax, that's why this block works, and the other one doesn't.

I think this is a great project to take your first look at Telegram bots

I hope you find those tips helpful and they save you a bit of frustration and doubt, although those two are really part of the learning process. =) For me, the project was fun, manageable, and an inspiration to dig deeper. And I'm not even going to deny that I went down a rabbit hole of booping and dog adoration (and being pleased with myself) when my code worked. It's a good thing my own dogster was sleeping, or he would have been really jealous.

If there's something you don't understand, all the errors are google-able, and if you want an extra challenge, try doing the same, but with cat gifs, once you've figured out the basics.

A picture of my dog sleeping beside me while I work.

This could have been generated by the bot, but nope, it's my very own sleeping doggo.

Good luck in exploring Telegram bots, they're super fun! And a big thanks to Dzaky for the original tutorial.

Thank you for reading, this post was originally published on ArtFox.Dev on 24.02.2020.

P.S.: this is my first dev.to post, and I'm super anxious =)

Top comments (0)