Telegram Bot Prototype using Serverless Framework and Webtask
Tags: node, bots, serverless, telegrammessenger
Published: 2017-10-02 13:15:37

---
title: "Telegram Bot Prototype using Serverless Framework and Webtask"
published: true
tags: nodejs,bots,serverless,telegram-messenger
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/1024/1*2jp2b8aeXLxBVC0yS0RpYA.jpeg)<figcaption>Scientists from Cognitive Science Research Group at Queen Mary University of London programmed a robot to perform stand-up comedy.â€Š–â€Š<a href="https://www.youtube.com/watch?v=fL5ZtBO4NzM">https://www.youtube.com/watch?v=fL5ZtBO4NzM</a></figcaption>

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&nbsp;Bot.

### What are we going to&nbsp;build?

In this article, we are going to create a very simple telegram bot that retrieves a random dad joke using the [_icanhazdadjoke_ API](https://icanhazdadjoke.com/api)

### What we are going to&nbsp;use?

- Latest version of NodeJSâ€Š–â€ŠDownload it&nbsp;[**here**](https://nodejs.org/en/download/)
- A [Webtask](https://webtask.io/)&nbsp;account
- An editor of your choiceâ€Š–â€ŠIn mi case, i’m using&nbsp;[VSCode](https://code.visualstudio.com/)
- A Telegram account and the Telegram Desktop Client (Recommended).
- The Serverless CLIâ€Š–â€ŠDownload it&nbsp;[here](https://serverless.com/)

### Creating the bot in&nbsp;Telegram

After you created your Telegram account and logged in the desktop client, you need to talk with the [_BotFather_](https://telegram.me/botfather) (Click to go to talk with it in the Telegram Client) to create your bot and generate your **API Token** for your new&nbsp;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&nbsp;bots

![](https://cdn-images-1.medium.com/max/1024/1*Rg_4nvtKJB65kR1VPAIN6w.png)

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&nbsp;5000.”

![](https://cdn-images-1.medium.com/max/1024/1*tt0OoLHTcEqClZFDded-Ig.png)

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&nbsp;ðŸ”œ.

![](https://cdn-images-1.medium.com/max/1024/1*RLucsPL9aOH2NQkKWy8e8g.png)<figcaption>Example of the bot’s username must beÂ unique</figcaption>

### 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&nbsp;Webtask.

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

$ serverless create --template webtasks-nodejs --path \<folder\_name\_of\_your\_bot\_\>

![](https://cdn-images-1.medium.com/max/1024/1*KX3W742tuF_5q-6SKvFHcw.png)

Our generated folder structure will look like&nbsp;this:

![](https://cdn-images-1.medium.com/max/432/1*BBwAryv1SXxAmS_y02Ok_g.png)<figcaption>Generated folder structure by Serverless CLI</figcaption>

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&nbsp;command:

$ npm&nbsp;install

![](https://cdn-images-1.medium.com/max/1024/1*CzGdeDOmDNieab5QtYxegA.png)

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

- axiosâ€Š–â€ŠHTTP Client for&nbsp;NodeJS
- node-telegram-bot-apiâ€Š–â€ŠTelegram Bot API for&nbsp;NodeJS

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

![](https://cdn-images-1.medium.com/max/1024/1*bfIx6mvEAk1GtWbpFulKtw.png)

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_

![](https://cdn-images-1.medium.com/max/896/1*3oF-NTHuuggMNXfts-1lIA.png)

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

$ serverless login

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

$ serverless deploy

![](https://cdn-images-1.medium.com/max/1024/1*Y-eYiJhFz1muGzW6eisa3Q.png)

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&nbsp;appear:

![](https://cdn-images-1.medium.com/max/1024/1*FAELJVG6ZhpvxjdvlVTFZw.png)<figcaption>The Webtask endpoint is workingÂ ðŸ™Œ</figcaption>

### Setting the WebHook of our Telegram&nbsp;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&nbsp;curl:

```
$ curl -X POST https://api.telegram.org/bot<TELEGRAM_BOT_API_TOKEN>/setWebhook -H "Content-type: application-json" -d '{"url": "WEBTASK_ENDPOINT_URL"}'
```

![](https://cdn-images-1.medium.com/max/1024/1*tVZSMsrvmL9JAWbhkUAxVQ.png)<figcaption>Example of setting a Webhook, in this case is another bot that I madeÂ before</figcaption>

### Beep-Boop ðŸ¤–: “Hello&nbsp;World!”

Time to code!&nbsp;ðŸ‘

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

{% gist https://gist.github.com/jefer590/7abf6997b0c6d8f618d3e4e820e89193 %}

After that, we just deploy the&nbsp;Webtask

$ serverless deploy

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

![](https://cdn-images-1.medium.com/max/426/1*ReeiD0XBEGTkOy_JAOx1uQ.png)<figcaption>Any message to the bot will trigger the message “HelloÂ World!”</figcaption>

### Making the bot to tell a random dad joke&nbsp;ðŸ¤¡

Lets code&nbsp;again!

Lets check first what this code&nbsp;do:

{% gist https://gist.github.com/jefer590/d6829568648f9c2947e3c9bc8023acb2 %}

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&nbsp;request.
5. If the message is the command /start, send the welcome&nbsp;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&nbsp;user.
7. If there’s something that the bot does not understand, we send the error message&nbsp;ðŸ˜•.

Note: We can use ES2017 in Webtasks&nbsp;ðŸ˜

### Beep-Boop ðŸ¤–: “Let me tell you a&nbsp;joke”

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

![](https://cdn-images-1.medium.com/max/1004/1*Ojr1JN0eB4jLwT1ZHat9bg.png)<figcaption>It works!Â ðŸŽ‰</figcaption>

### 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&nbsp;ðŸ˜

_The source code of this project is available on Github right_ [_here_](https://github.com/jefer590/telegram-bot-serverless-webtasks-example) _if you want to check it out and run it by yourself._

### ðŸ™Œ Thanks for reading!&nbsp;ðŸ™Œ

### Special Thanks!

Thanks to [Eduardo Romero](https://medium.com/u/b0f1eeeaa8dd) for helping me with the redaction and revision of this&nbsp;article.