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
    • AI Studio
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Yandex Cloud Functions
  • Comparison with other Yandex Cloud services
    • All tutorials
    • Creating a skill for Alice
    • Deploying a web application
    • Creating a skill for Alice and a website with authorization
    • Writing data from a device into a database
    • Writing load balancer logs to PostgreSQL
    • Developing a Slack bot
    • Developing a Telegram bot
    • Connecting to a YDB database from a Python function
    • Connecting to a YDB database from a function in Node.js
    • Converting a video to a GIF in Python
    • Creating a Node.js function using TypeScript
    • Developing a custom integration
    • Creating a budget trigger that invokes a function to stop a VM
    • Creating an interactive serverless application using WebSocket
    • Automatically copying objects from one Object Storage bucket to another
    • Running computations on a schedule in DataSphere
    • Interactive debugging of functions
    • Regular asynchronous recognition of audio files from Object Storage
    • Canary release of a Cloud Functions function
    • Deploying a fault-tolerant architecture with preemptible VMs
    • Creating triggers that invoke a function to stop a VM and send a Telegram notification
    • Loading data from Yandex Direct to a data mart enabled by Yandex Managed Service for ClickHouse® using Yandex Cloud Functions, Yandex Object Storage, and Yandex Data Transfer
    • Status monitoring of geographically distributed devices
    • Sensor reading monitoring and event notifications
    • Emulating multiple IoT devices
    • Streaming Yandex Cloud Postbox events to Yandex Data Streams and analyzing them using Yandex DataLens
    • URL shortener
    • Yandex Tracker: data export and visualization
    • Running computations in DataSphere using the API
    • Developing a Telegram bot for text and audio recognition
    • Configuring responses in Yandex Cloud Logging and Cloud Functions
    • Developing functions in Functions Framework and deploying them to Yandex Serverless Containers
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Release notes
  • FAQ

In this article:

  • Getting started
  • Required paid resources
  • Set up resources
  • Create a Telegram bot
  • Post a bot image
  • Create an Object Storage bucket
  • 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
  • Test your Telegram bot
  • 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 June 3, 2025
  • Getting started
    • Required paid resources
  • Set up resources
  • Create a Telegram bot
  • Post a bot image
    • Create an Object Storage bucket
    • 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
  • Test your Telegram bot
  • How to delete the resources you created

With serverless technology, you can create a Telegram bot that will respond to chat messages.

To create a bot:

  1. Set up your environment.
  2. Set up 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. Test your Telegram bot.

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 function invocation count, computing resources allocated to run the function, and outbound traffic (see Cloud Functions pricing).
  • Fee for the amount of stored data, number of data operations, 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).

Set up resourcesSet up resources

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

Create a Telegram botCreate a Telegram bot

Create a bot in Telegram and get a token.

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

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

  3. In the username field, specify a username for the bot, e.g., ServerlessHelloTelegramBot. This will allow users to find your bot on Telegram. The username must end with ...Bot or ..._bot.

    Once done, you will get a token. Save it, as you will need it later.

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

    /setuserpic
    

Post a bot imagePost a bot image

If you want your bot to respond to user messages with an image, create an Object Storage bucket and upload sayhello.png from the saved archive to it.

Create an Object Storage bucketCreate an Object Storage bucket

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, as you will need it later.

    2. Specify these 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 created bucket.
  2. Select Object Storage.
  3. Select the bucket.
  4. Click Upload.
  5. In the window that opens, select sayhello.png 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 created bucket.
  2. Select Object Storage.
  3. Select the bucket.
  4. Select sayhello.png.
  5. Click Get link.
  6. Make sure the image is accessible through the link 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 Specification field and paste 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 you created when setting up the resources.
  6. Click Create.

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

Create a functionCreate a function

If you want your Telegram bot to respond to the /start and /help commands and send an image in response to any other text, create a designated 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 Node.js as the runtime environment and click Continue.

  7. Under Function code, replace the contents of the index.js file with the code below. Replace <API_gateway_domain> with 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 and enable Public function under General information.

  13. Save your function ID, as 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 your 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 service domain.

    Result:

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

Test your Telegram botTest your Telegram bot

Chat with the bot:

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

  2. Send /start to the chat.

    The bot should respond with:

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

    The bot should respond with:

    Hello, <username>.
    I can say Hello and nothing more
    
  4. Send a 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
Connecting to a YDB database from a Python function
Yandex project
© 2025 Yandex.Cloud LLC