Telegram Bot Prototype using Serverless Framework and Webtask

Fernando Alvarez
Oct 2, 2017 · 6 min read
Image for post
Image for post
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?

What we are going to use?

  • 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 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

Image for post
Image for post

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.”

Image for post
Image for post

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 🔜.

Image for post
Image for post
Example of the bot’s username must be unique

Creating the Webtask function using 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_>

Image for post
Image for post

Our generated folder structure will look like this:

Image for post
Image for post
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

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

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:

Image for post
Image for post
The Webtask endpoint is working 🙌

Setting the WebHook of our Telegram Bot

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

Beep-Boop 🤖: “Hello World!”

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

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

Making the bot to tell a random dad joke 🤡

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”

Image for post
Image for post
It works! 🎉

Conclusion


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!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store