How to build a simple Echo Bot on Telegram using hook.io and Python

In this tutorial, i want to explain how to create a simple “Echo BOT” on Telegram using the WebHook feature and a microservice hosted on Hook.io.

Telegram currently support two ways of processing bot updates, getUpdates and setWebhook. getUpdates is a pull mechanism, setWebhook is push.

The main advantages of using a Webhook over getUpdates is pretty clear: as soon as an update arrives, it will delivered to your microservice for processing.

Microservices are small, independent processes that communicate with each other to form complex applications which utilize language-agnostic APIs.

These services are small building blocks, highly decoupled and focused on doing a small task, facilitating a modular approach to system-building

In this tutorial I suggest to use Hook.io.
This service provides an infrastructure that let you deploy HTTP microservices effortlessly: basically you can script HTTP requests without deploying an entire web stack.
On hook.io, microservices are represented by a single function matched to a single unique URL.

Let’s try to create this bot! (in 3 simple steps) 🙂


1. Create a Telegram Bot

In order to register a bot with Telegram, you first need to create a personal Telegram account. Visit web.telegram.org and enter your phone number. Telegram will send you a text message and you can then create an account by following the instructions on the screen.

Once you have a Telegram account, you can register a new Telegram Bot by using BotFather.
Visit t.me/botfather and start a conversation with the “Telegram’s bot that creates other bots“.

To create a new bot, send the following command to Bot Father:

/newbot

You should get a reply instantly that asks you to choose a name for your Bot, so you can send the choosen name as chat message to BotFather.

BotFather will now ask you to pick a username for your Bot. This username has to end in bot, and be globally unique.

Now BotFather will send you a “Congratulations” message, which will include a token.

Take note of the token! 🙂

 


2. Create the Microservice

First, register an account on Hook.io (or use your GitHub account) and login.

Then create a new hook simply visiting the Create Hook section and choosing a name for your service:

On the Hook Source panel you can choose your programming language and you can provide here the code that will be executed when webhook will be triggered:

Now choose “Python” and write your code! 🙂

In other tutorial, i’ve find a simple pyhon code for an Echo Bot, like this:

import requests
message=Hook['params']['message']['text']
chatID=Hook['params']['message']['chat']['id']
URL='https://api.telegram.org/bot<TOKEN>/sendMessage'
req=requests.get(URL,verify=False,data={'chat_id':chatID,'text':message})

(where <TOKEN> is the API token send by BotFather)

The code simply retrieve the data from the webhook sended by telegram platform (the “Hook” object) and extract message and chatId of the sender.

Then create a request to Telegram API in order to send a message to the sender with the same text of original message.

However, in this code there is a tricky bug: in the webhook mode, Telegram servers send updates continuosly until receives an “200 Ok” response from the webhook page.
So, with this code, the microservice don’t send any response to the telegram platform, that continue to invoke the webhook page.

The result is a ‘bot loop’: your bot continuosly reply with the same message.

To solve this problem we need to write a code a little more complex, but with the ability to send a “200 Ok” response to Telegram servers, like this:

Now, the last step…


3. Connect the Bot with the Microservice

We have to tell the bot to use the newly created microservice.

To make this step, you can call, using cUrl or your browser, this link:

https://api.telegram.org/bot<TOKEN>/setWebhook?url=https://hook.io/<hook-user>/<hook-name>

where:

  • <TOKEN> is the token of your bot;
  • <hook-user> is the username created on hook.io;
  • <hook-name> is the name of the micro-service you created.

Once the URL is launched, if it’s okay, you’ll read something like this:

{"ok": true, "result": true, "description": "Webhook was set"}

And now the bot works correctly, without any loop:

 

Now have fun with your ‘Parrot BOT’!


References

Comments