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:

  1. Program the logic of a Telegram bot.
  2. Register a new bot in Telegram.
  3. 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:

  • /start with a hello message;
  • /sticker with a sticker response;
  • /getwebhook to get information about your bot’s settings;
  • /setwebhook to configure your bot (more on that later).

Follow these steps with our sample bot to get started:

  1. Dowload the source code as an archive («Code» → «Download ZIP») and unpack it.
  2. 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.
  3. 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).
  4. Go to Cloud platformFunctionsCreate function, learn more.
  5. Specify the name, for example «Bot».
  6. Upload the ZIP file as the Executable code.
  7. Specify the Path to the file as «/bot/tele_bot.py».
  8. Specify the Function to execute as «main».
  9. Add the environment variable “TOKEN”, but don’t add the value yet, we will cover it the next step.
  10. Click Save and deploy.
  11. 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:

  1. Find the @BotFather bot in Telegram. This is an official bot that helps you manage your existing bots.
  2. Send the following command:

    /newbot
  3. @BotFather will ask you to choose a name for your bot. Next send the name for your bot ending in «_bot».

  4. 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:

  1. Find the environment variable “TOKEN” in the function settings. We left it empty in the first step.
  2. Specify the token of your bot that you received from @BotFather in this variable.
  3. 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

  1. Find the @SelectelServerless_bot in Telegram.
  2. Run the following:

    /setwebhook <The token of your bot> <Cloud function's URL>
  3. The bot will respond with that it receives API from Telegram. If everything is working as intended, the response will be «true».

  4. To make sure everything is working correctly, send the command:

    /getwebhook <The token of your bot>
  5. 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.

Results

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.