Lonely is Thy God

His grandmother used to say, "Every child is a curious soul." So, while every adult around him seemed to run away from his questions, only she would patiently listen to his thoughts and explain the…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Writing a telegram bot in Python

My messenger of choice is Telegram. It is fast, works on every device I have, has encryption and is not owned by Facebook. To emphasize: All other messengers that are spread enough among my friends do suck on Linux (at least in my experience) or rely on nasty, battery eating web apps.

I really enjoy writing Python code more then any other language I know. Normally you find someone who already solved the problem you are approaching and provided his solution. Writing a Telegram bot is no exception here.

While many sites provide some kind of notification service for new content, like an RSS feed or something, the Anesia site does not. While the new content is posted to Facebook, I did not want to use that. I preferred only accessing the site where the comics are published. So I looked around on the site and found, that the images of the comics to not have a simple naming scheme, so guessing their URLs is not viable. But every new comic gets a new post, that has a new number. So I only have to check if the URL of the next comic returns something useful.

The program gets executed once every hour by a cron job and checks if the next URL contains a comic. If there is a new comic, it extracts the image URL from the site and posts it to a Telegram channel I created for this purpose.

To create a bot for Telegram, you have to talk to the Botfather, a bot run by the Telegram team that lets you manage your bot. Just start a Telegram conversation with@botfather and he will walk you through everything you need to know.

With the command /newbot you can create a new bot. The Botfather then asks you for a name for it and an username. That username is the name your bot will be reached under. This is all you need to do to create your bot. The Botfather then tells you the API token you need to use the bot. This token is very important since it is you authentication to enable only you to manage the bot. Write that code down since you need it later.

The program remembers the ID number of the last comic it found. The number is incremented by 1 and the new URL is constructed via

The URL is then read using the urllib module:

If the site returns a HTTP error - in most cases 404 - then there is no new comic and the program exits.

To see if the returned page contains a new comic, I use a simple regular expression. I designed the expression by looking at the page source, figuring out what parts of the page are relevant to me and then trying for hours to write the correct expression. Here is what I came up with:

The first pattern checks if the page contains the string “Página não encontrada” which is Portuguese for “Page not found”. This is also the first regular expression that gets evaluated and ends the skips that post ID for further inspection. I guess this check is not necessary since normally a 404 HTTP error is returned when the post does not exist, but I left it in to be sure.

The second pattern searches for an image in the page, that has a class associated and an alternative text. These two criteria turned out to be enough to uniquely identify the image in the page. This required a lot of trial and error but the result is quite simple. The return value of that expression contains the URL of the image that we want to send to the channel.

I created the send image function that takes the URL of the next comic and its number:

Apart from wanting to always receive the newest comic, a big part of the motivation for this project was to learn about the Bot API and regular expressions with Python. I did not dig deep with the expressions but I now have an idea of how they work and I lost the fear I had before.

The bot has been running about a month now and so far everything went smooth. Maybe eveerything will break if they change something on their end, but this is ok, since I only put like on day into the program and spend no second with robustness and testing, which you absolutely should to if you get serious.

Add a comment

Related posts:

Best between React and Angular?

The web development world is constantly experiencing new entries in the context of technologies, frameworks, and tools. When it comes time to choose a particular framework or library, programmers get…

Cases for the Galaxy S23 reveal a new camera design over launch date rumour

The year is almost over, which suggests that the introduction of Samsung’s upcoming Galaxy S23 series is likely to close. We’ve seen a few leaked device renderings, but these cases appear to validate…