Creating a Telegram Bot on a Cloud Function
Bots can help you simplify communication with users by creating a smart assistant for them. Bots understand text commands and can access the API of your website and service, or be a standalone product with unique services. Usually, a server is required to create a bot, but in this article we will show you how to do without a server. This bot is essential for sudden variation in the number of requests and perfectly withstands peak loads. You can set up a Telegram bot on a Cloud function using the official API and our example by following these 3 steps:
- Program the logic of a Telegram bot.
- Register a new bot in Telegram.
- Add logic to the bot.
Programming the Logic of a Telegram Bot
To simplify a problem, we have created an example of such a bot. Its source code is available at github.com/selectel/cloud-telegram-bot. It understands the following commands:
/startwith a hello message;
/stickerwith a sticker response;
/getwebhookto get information about your bot’s settings;
/setwebhookto configure your bot (more on that later).
Follow these steps with our sample bot to get started:
- Dowload the source code as an archive («Code» → «Download ZIP») and unpack it.
- The archive should contain:
- «setup.py» with the minimal code so that the Python environment takes the contents of the folder as an installation module;
- «requirements.txt» with a description of dependencies;
- «bot» folder with the source code of the bot.
- Select all files and folders and create a new ZIP archive. Then the source code inside the archive will not have any extra folders (as when downloading the archive from github).
- Go to Cloud platform → Functions → Create function, learn more.
- Specify the name, for example «Bot».
- Upload the ZIP file as the Executable code.
- Specify the Path to the file as «/bot/tele_bot.py».
- Specify the Function to execute as «main».
- Add the environment variable “TOKEN”, but don’t add the value yet, we will cover it the next step.
- Click Save and deploy.
- Check the box that you need a Public link.
We now have the API that implements the response logic of your bot. The next step is to register it in Telegram.
Registering a New Bot in Telegram
You need to register your bot in order for Telegram to know about it. There is only one way to do this:
- Find the @BotFather bot in Telegram. This is an official bot that helps you manage your existing bots.
Send the following command:
@BotFather will ask you to choose a name for your bot. Next send the name for your bot ending in «_bot».
Write down the received token from @BotFather. This is the key for working with the Telegram API.
Now you have an API, a bot registered in Telegram, and its token. You only need to link them together.
Adding Logic to the Bot
At this stage, you need to make sure that the bot knows the user’s token, and Telegram knows where its API is:
- Find the environment variable “TOKEN” in the function settings. We left it empty in the first step.
- Specify the token of your bot that you received from @BotFather in this variable.
- Click Save and deploy.
Next, you need to forward messages that users send to Telegram to our bot (to the API that we have created for it).
Method 1. Using @SelectelServerless_bot
- Find the @SelectelServerless_bot in Telegram.
Run the following:
/setwebhook <The token of your bot> <Cloud function's URL>
The bot will respond with that it receives API from Telegram. If everything is working as intended, the response will be «true».
To make sure everything is working correctly, send the command:
/getwebhook <The token of your bot>
In response, you should receive information about webhook, including the URL that you specified.
Method 2. Using the Prepared Form
Open the form (or copy it to your Cloud Storage). Paste the token of your bot into the first field and the Cloud function’s URL into the second filed, then click «Set Webhook». api.telegram.org will be accessed and you will see the result.
Please note that VPN may be required.
A bot that is available in Telegram, responds to the commands, and is capable of automatically scaling under load and without a server is ready to work.