Telegram Bot Prototype using Serverless Framework and Webtask

Scientists from Cognitive Science Research Group at Queen Mary University of London programmed a robot to perform stand-up comedy. — https://www.youtube.com/watch?v=fL5ZtBO4NzM

FaaS (Functions as a Service) is gaining momentum more and more popular this days because They are easy to scale, not idle times costs and incredibly powerful when creating microservices. I was told that Auth0 Inc. created their own FaaS called Webtask and one of the greatest features is that you can easily create your function using NodeJS, so I decided to give it a try and create a very simple prototype with a Telegram bot, Why a bot? Because bots are commonly known to use servers to provide their services, and I want to try the serverless approach of this. Why a telegram bot? Because I am curious about how to do a Telegram Bot.

What are we going to build?

In this article, we are going to create a very simple telegram bot that retrieves a random dad joke using the icanhazdadjoke API

What we are going to use?

  • Latest version of NodeJS — Download it here
  • A Webtask account
  • An editor of your choice — In mi case, i’m using VSCode
  • A Telegram account and the Telegram Desktop Client (Recommended).
  • The Serverless CLI — Download it here

Creating the bot in Telegram

After you created your Telegram account and logged in the desktop client, you need to talk with the BotFather (Click to go to talk with it in the Telegram Client) to create your bot and generate your API Token for your new bot.

After you open the client with the BotFather, the /start will be triggered automatically, and the bot will answer with a big list of commands that will help you to create and manage your Telegram bots

Use the/newbot command to start. The bot will need a human-readable name. I used “Dad Jokes Bot” but you can use any name like “My very first super duper bot joke 5000.”

The BotFather will ask you for an username for the bot. The username must be unique and end with bot. In my case, I used “DadJokesTestBot” but the username can be any. After you insert the valid bot username will tell you that the bot was successfully created and it will give you the API Token. Write it down. You will need it 🔜.

Example of the bot’s username must be unique

Creating the Webtask function using Serverless CLI

After creating our Telegram bot using the bot father, we need to create the function that will provide the bot’s functionality. In this case, we will use the Serverless CLI.

Serverless Framework provides a very easy-to-use CLI that generates skeletons of functions from different providers like AWS Lambdas, Google Cloud Functions, Microsoft Azure Functions and, of course, Auth0 Webtask.

First, we need to create our webtask function using the CLI. Using the next command

$ serverless create --template webtasks-nodejs --path <folder_name_of_your_bot_>

Our generated folder structure will look like this:

Generated folder structure by Serverless CLI

We need to do a couple of things after upload our function to Webtask. First, we need to install the webtask handler for this project that is included in the package.json file. We Only need to make this command:

$ npm install

After that, we need to install a couple of packages to make the bot work:

  • axios — HTTP Client for NodeJS
  • node-telegram-bot-api — Telegram Bot API for NodeJS

$ npm install --save axios node-telegram-bot-api

After installing all the project dependencies, we need to configure the function name for Webstask. Open the serverless.yml file and then modify the name of the function in service>name. The typical generated example name is webtasks-nodejs

After that, we need to login to Webstask using the command:

$ serverless login

After the login, the only thing left is deploy our function. Use the next command:

$ serverless deploy

After this, you will receive your endpoint URL, test it in your browser to be sure that is working. If everything is alright a JSON like this needs to appear:

The Webtask endpoint is working 🙌

Setting the WebHook of our Telegram Bot

After configuring our Webtask endpoint, we need to tell to our bot where it needs to send all the requests that we could make in the chat. Telegram Bots use WebHooks. They are very easy to set up. We are going to set the WebHook to our task’s endpoint using curl:

$ curl -X POST https://api.telegram.org/bot<TELEGRAM_BOT_API_TOKEN>/setWebhook -H "Content-type: application-json" -d '{"url": "WEBTASK_ENDPOINT_URL"}'
Example of setting a Webhook, in this case is another bot that I made before

Beep-Boop 🤖: “Hello World!”

Time to code! 👏

In the handler.js file, we are going to make the bot respond to messages:

After that, we just deploy the Webtask

$ serverless deploy

And then we just chat a little bit to check if the bot responds

Any message to the bot will trigger the message “Hello World!”

Making the bot to tell a random dad joke 🤡

Lets code again!

Lets check first what this code do:

Complete code example
  1. We create a TelegramBot instance.
  2. Then we use axiospackage to call the icanhazdadjoke API.
  3. Then we set the Token and we create an instance of the bot using the TelegramBot object.
  4. We get the chatId and message from the current request.
  5. If the message is the command /start, send the welcome message.
  6. If the message is the command /tellmeajoke, we retrieve a random dad joke in text/plain from the icanhazdadjoke API and tell the joke to the user.
  7. If there’s something that the bot does not understand, we send the error message 😕.

Note: We can use ES2017 in Webtasks 😍

Beep-Boop 🤖: “Let me tell you a joke”

Moment of truth, lets try to chat with our bot!

It works! 🎉

Conclusion

This was a simple example, but it shows how fast you can start with serverless bots. Worth to mention that Telegram Bot API is incredibly easy to use and well documented, it shows you great examples of how to create your bots and how to consume their APIs. What I liked the most of this quick project is how easy it is to get create, deploy and test functions with the help of Serverless and Webtasks. I can see a bright future for Webtasks 😁


The source code of this project is available on Github right here if you want to check it out and run it by yourself.


🙌 Thanks for reading! 🙌


Special Thanks!

Thanks to Eduardo Romero for helping me with the redaction and revision of this article.