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.
Yandex Object Storage
    • All tutorials
    • Getting statistics on object queries with S3 Select
    • Getting website traffic statistics with S3 Select
    • Getting statistics on object queries using Yandex Query
    • Generating a resource-by-resource cost breakdown report using S3 Select
    • Server-side encryption
    • Integrating an L7 load balancer with CDN and Object Storage
    • Blue-green and canary deployment of service versions
    • Analyzing logs in DataLens
    • Mounting buckets to the file system of Yandex Data Processing hosts
    • Using Object Storage in Yandex Data Processing
    • Importing data from Object Storage, processing and exporting to Managed Service for ClickHouse®
    • Mounting a bucket as a disk in Windows
    • Migrating data from Yandex Data Streams using Yandex Data Transfer
    • Using hybrid storage in Yandex Managed Service for ClickHouse®
    • Loading data from Yandex Managed Service for OpenSearch to Yandex Object Storage using Yandex Data Transfer
    • Automatically copying objects from one bucket to another
    • Recognizing audio files in a bucket on a regular basis
    • Training a model in Yandex DataSphere on data from Object Storage
    • Connecting to Object Storage from VPC
    • Migrating data to Yandex Managed Service for PostgreSQL using Yandex Data Transfer
    • Uploading data to Yandex Managed Service for Greenplum® using Yandex Data Transfer
    • Uploading data to Yandex Managed Service for ClickHouse® using Yandex Data Transfer
    • Uploading data to Yandex Managed Service for YDB using Yandex Data Transfer
    • Exchanging data between Yandex Managed Service for ClickHouse® and Yandex Data Processing
    • Uploading data from Yandex Managed Service for YDB using Yandex Data Transfer
    • Hosting a static Gatsby website in Object Storage
    • Migrating a database from Managed Service for PostgreSQL to Object Storage
    • Exchanging data between Yandex Managed Service for ClickHouse® and Yandex Data Processing
    • Importing data from Yandex Managed Service for PostgreSQL to Yandex Data Processing using Sqoop
    • Importing data from Yandex Managed Service for MySQL® to Yandex Data Processing using Sqoop
    • Migrating data from Yandex Object Storage to Yandex Managed Service for MySQL® using Yandex Data Transfer
    • Migrating a database from Yandex Managed Service for MySQL® to Yandex Object Storage
    • Exporting Greenplum® data to a cold storage in Yandex Object Storage
    • Loading data from Yandex Direct to a Yandex Managed Service for ClickHouse® data mart using Yandex Cloud Functions, Yandex Object Storage, and Yandex Data Transfer
    • Migrating data from Elasticsearch to Yandex Managed Service for OpenSearch
    • Uploading Terraform states to Object Storage
    • Locking Terraform states using Managed Service for YDB
    • Visualizing Yandex Query data
    • Publishing game updates
    • VM backups using Hystax Acura
    • Backing up to Object Storage with CloudBerry Desktop Backup
    • Backing up to Object Storage with Duplicati
    • Backing up to Object Storage with Bacula
    • Backing up to Yandex Object Storage with Veeam Backup
    • Backing up to Object Storage with Veritas Backup Exec
    • Managed Service for Kubernetes cluster backups in Object Storage
    • Developing a custom integration in API Gateway
    • URL shortener
    • Storing application runtime logs
    • Developing a skill for Alice and a website with authorization
    • Creating an interactive serverless application using WebSocket
    • Deploying a web application using the Java Servlet API
    • Developing a Telegram bot
    • Replicating logs to Object Storage using Fluent Bit
    • Replicating logs to Object Storage using Data Streams
    • Uploading audit logs to ArcSight SIEM
    • Exporting audit logs to SIEM Splunk systems
    • Creating an MLFlow server for logging experiments and artifacts
    • Operations with data using Yandex Query
    • Federated data queries using Query
    • Recognizing text in image archives using Vision OCR
    • Converting a video to a GIF in Python
    • Automating tasks using Managed Service for Apache Airflow™
    • Processing files with usage details in Yandex Cloud Billing
    • Deploying a web app with JWT authorization in API Gateway and authentication in Firebase
    • Searching for Yandex Cloud events in Yandex Query
    • Searching for Yandex Cloud events in Object Storage
    • Creating an external table from a bucket table using a configuration file
    • Migrating databases from Google BigQuery to Managed Service for ClickHouse®
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Bucket logs
  • Release notes
  • FAQ

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. Tutorials
  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
Deploying a web application using the Java Servlet API
Next
Replicating logs to Object Storage using Fluent Bit
© 2025 Direct Cursus Technology L.L.C.