Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Tutorials
    • All tutorials
    • URL shortener
    • Entering data into storage systems
    • Storing application runtime logs
    • Deploying a web application using the Java Servlet API
    • Developing a Slack bot
    • Developing a Telegram bot
    • Developing a custom integration in API Gateway
    • Developing CRUD APIs for movie services
    • Building a CI/CD pipeline in GitLab
    • Working with an API gateway via WebSocket
    • Creating an interactive serverless application using WebSocket
    • Automatically copying objects from one Object Storage bucket to another
    • Visualizing logs in Grafana using the Cloud Logging plugin
    • Canary release of a Cloud Functions function
    • Interactive debugging of Cloud Functions functions
    • Creating a Node.js function using TypeScript
    • Running a containerized app in Serverless Containers
    • Streaming Yandex Cloud Postbox events to Data Streams and analyzing them using DataLens
    • Using API Gateway to set up speech synthesis in SpeechKit
    • Connecting to YDB from a Cloud Functions function in Python
    • Connecting to a YDB database from a Cloud Functions function in Node.js
    • API Gateway protection with Smart Web Security
    • Deploying a web app with JWT authorization in API Gateway and authentication in Firebase
    • Automatic data upload to Yandex SpeechSense using Yandex Workflows
    • Configuring responses in Cloud Logging and Yandex Cloud Functions
    • Setting up Workflows integration with Tracker, YandexGPT, and Yandex Cloud Postbox
    • Developing functions in Functions Framework and deploying them to Yandex Serverless Containers

In this article:

  • Getting started
  • Required paid resources
  • Create resources
  • Register your Telegram bot
  • Post a bot image
  • Create a bucket in Object Storage
  • Upload the image to the bucket
  • Get a link to the uploaded image
  • Create an API gateway
  • Create a function
  • Configure a link between the function and the Telegram bot
  • Make sure the Telegram bot works
  • How to delete the resources you created
  1. Serverless technologies
  2. Developing a Telegram bot

How to create a Telegram bot

Written by
Yandex Cloud
Updated at May 7, 2025
  • Getting started
    • Required paid resources
  • Create resources
  • Register your Telegram bot
  • Post a bot image
    • Create a bucket in Object Storage
    • Upload the image to the bucket
    • Get a link to the uploaded image
  • Create an API gateway
  • Create a function
  • Configure a link between the function and the Telegram bot
  • Make sure the Telegram bot works
  • How to delete the resources you created

You can use serverless technologies to create a Telegram bot that will respond to chat messages.

To create a bot:

  1. Set up your environment.
  2. Create resources.
  3. Register your Telegram bot.
  4. Post a bot image.
  5. Create an API gateway.
  6. Create a function.
  7. Configure a link between the function and the Telegram bot.
  8. Make sure the Telegram bot works.

If you no longer need the resources you created, delete them.

Getting startedGetting started

Sign up in Yandex Cloud and create a billing account:

  1. Navigate to the management console and log in to Yandex Cloud or register a new account.
  2. On the Yandex Cloud Billing page, make sure you have a billing account linked and it has the ACTIVE or TRIAL_ACTIVE status. If you do not have a billing account, create one and link a cloud to it.

If you have an active billing account, you can navigate to the cloud page to create or select a folder for your infrastructure to operate in.

Learn more about clouds and folders.

Required paid resourcesRequired paid resources

The cost of Telegram bot support includes:

  • Fee for the number of function calls, computing resources allocated to executing the function, and outgoing traffic (see Cloud Functions pricing).
  • Fee for the amount of stored data, the number of data transactions, and outbound traffic (see Object Storage pricing).
  • Fee for the number of requests to the API gateway and outbound traffic (see Yandex API Gateway pricing).

Create resourcesCreate resources

  1. Download the file archive required to create a bot.
  2. Create a service account and assign the editor and functions.functionInvoker roles to it for your folder.

Register your Telegram botRegister your Telegram bot

Register your bot in Telegram and get a token.

  1. To register the new bot, launch the BotFather bot and run this command:

    /newbot
    
  2. In the name field, enter a name for the new bot, e.g., Serverless Hello Telegram Bot. This is the name the bot users will see.

  3. In the username field, specify a username for the new bot, e.g., ServerlessHelloTelegramBot. You can use it to locate the bot in Telegram. The username must end with ...Bot or ..._bot.

    As a result, you will get a token. Save it, as you will need it later.

  4. Set an icon for the bot using the sayhello.png file from the saved archive. Send this command to the BotFather bot:

    /setuserpic
    

Post a bot imagePost a bot image

For the bot to respond to user messages with an image, create a bucket in Object Storage and upload the sayhello.png file from the saved archive to the bucket.

Create a bucket in Object StorageCreate a bucket in Object Storage

Management console
  1. In the management console, select the folder where you want to create a bucket.

  2. Select Object Storage.

  3. Click Create bucket.

  4. On the bucket creation page:

    1. Enter a name for the bucket. Save the bucket name. You will need it later.

    2. Specify the bucket settings:

      • Max size: 1 GB
      • Object read access: Public
      • Storage class: Standard
    3. Click Create bucket.

Upload the image to the bucketUpload the image to the bucket

Management console
  1. In the management console, select the folder containing the previously created bucket.
  2. Select Object Storage.
  3. Select the previously created bucket.
  4. Click Upload.
  5. In the window that opens, select the sayhello.png file from the saved archive. The management console will display the file selected for upload.
  6. Click Upload.

Get a link to the uploaded imageGet a link to the uploaded image

Management console
  1. In the management console, select the folder containing the previously created bucket.
  2. Select Object Storage.
  3. Select the previously created bucket.
  4. Select sayhello.png.
  5. Click Get link.
  6. Make sure the image is available in your browser.

Create an API gatewayCreate an API gateway

Create and configure an API Gateway.

Management console
  1. In the management console, select the folder where you want to create an API gateway.

  2. Select API Gateway.

  3. Click Create API gateway.

  4. Name the gateway: for-serverless-hello-telegram-bot.

  5. Clear the contents of the Specification field and replace them with this code:

    openapi: 3.0.0
    info:
      title: for-serverless-hello-telegram-bot
      version: 1.0.0
    paths:
      /sayhello.png:
        get:
          x-yc-apigateway-integration:
            type: object_storage
            bucket: <bucket_name>
            object: sayhello.png
            presigned_redirect: false
            service_account_id: <service_account_ID>
          operationId: static
    

    Where:

    • bucket: Bucket name.
    • service_account_id: ID of the service account created when setting up the resources.
  6. Click Create.

  7. Select the created API gateway. Save the Default domain field value. You will need it later.

Create a functionCreate a function

To make the Telegram bot respond to the /start and /help commands and send an image in response to any other text, create a function.

Management console
  1. In the Management console, select the folder where you want to create a function.

  2. Select Cloud Functions.

  3. Click Create function.

  4. Enter the function name: fshtb-function.

  5. Click Create.

  6. Under Editor, select the Node.js runtime environment and click Continue.

  7. Under Function code, replace the contents of the index.js file with the code below. Instead of <API_gateway_domain>, specify the API gateway service domain.

    const { Telegraf } = require('telegraf');
    
    const bot = new Telegraf(process.env.BOT_TOKEN);
    bot.start((ctx) => ctx.reply(`Hello. \nMy name Serverless Hello Telegram Bot \nI'm working on Cloud Function in the Yandex Cloud.`))
    bot.help((ctx) => ctx.reply(`Hello, ${ctx.message.from.username}.\nI can say Hello and nothing more`))
    bot.on('text', (ctx) => {
        ctx.replyWithPhoto({url: '<API_gateway_domain>/sayhello.png'});
        ctx.reply(`Hello, ${ctx.message.from.username}`);
    
    });
    
    module.exports.handler = async function (event, context) {
        const message = JSON.parse(event.body);
        await bot.handleUpdate(message);
        return {
            statusCode: 200,
            body: '',
        };
    };
    
  8. Under Function code, create a file named package.json and paste the following code into it:

    {
      "name": "ycf-telegram-example",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "MIT",
      "dependencies": {
        "telegraf": "^4.12.0"
      }
    }
    
  9. Specify the following settings:

    • Runtime environment: nodejs16
    • Entry point: index.handler
    • Timeout: 5
  10. Add the BOT_TOKEN environment variable. In the Value field, specify the Telegram bot token.

  11. Click Save changes.

  12. Make sure the function is public. To do this, navigate to the Overview page; under General information, switch the Public function option to on.

  13. Save your function ID. You will need it later.

Configure a link between the function and the Telegram botConfigure a link between the function and the Telegram bot

Management console
  1. In the management console, select the folder.

  2. Select API Gateway.

  3. Select the for-serverless-hello-telegram-bot API gateway.

  4. Update the API gateway specification by adding the fshtb-function section at the end of the code:

      /fshtb-function:
        post:
          x-yc-apigateway-integration:
            type: cloud_functions
            function_id: <function_ID>
          operationId: fshtb-function
    

    Where function_id is the fshtb-function ID.

  5. Click Save.

  6. Run the following request in the terminal:

    • Linux, macOS:

      curl \
        --request POST \
        --url https://api.telegram.org/bot<bot_token>/setWebhook \
        --header 'content-type: application/json' \
        --data '{"url": "<API_gateway_domain>/fshtb-function"}'
      
    • Windows (cmd):

      curl ^
        --request POST ^
        --url https://api.telegram.org/bot<bot_token>/setWebhook ^
        --header "content-type: application/json" ^
        --data "{\"url\": \"<API_gateway_domain>/fshtb-function\"}"
      
    • Windows (PowerShell):

      curl.exe `
        --request POST `
        --url https://api.telegram.org/bot<bot_token>/setWebhook `
        --header '"content-type: application/json"' `
        --data '"{ \"url\": \"<API_gateway_domain>/fshtb-function\" }"'
      

    Where:

    • <bot_token>: Telegram bot token.
    • <API_gateway_domain>: API gateway's service domain.

    Result:

    {"ok":true,"result":true,"description":"Webhook was set"}
    

Make sure the Telegram bot worksMake sure the Telegram bot works

Talk to the bot:

  1. Open Telegram and search for the bot using the previously created username.

  2. Send the /start message to the chat.

    The bot must respond with:

    Hello.
    My name Serverless Hello Telegram Bot
    I'm working on Cloud Function in the Yandex Cloud.
    
  3. Send the /help message to the chat.

    The bot must respond with:

    Hello, <username>.
    I can say Hello and nothing more
    
  4. Send any text message to the chat. The bot should respond with an image and this message: Hello, <username>.

How to delete the resources you createdHow to delete the resources you created

To stop paying for the resources you created:

  • Delete the function.
  • Delete the API gateway.
  • Delete the bucket.

Was the article helpful?

Previous
Developing a Slack bot
Next
Developing a custom integration in API Gateway
© 2025 Direct Cursus Technology L.L.C.