Demo Telegram Bot
A Telegram bot on a Yandex Cloud serverless stack that returns the same message sent by the user. It is implemented with Node.js and the Telegraf.js
You can modify the Telegram bot's skills as you see fit.
Required paid resources
The cost of resources for the application includes:
- Fee for the number of function calls, computing resources allocated to a function, and outgoing traffic (see Yandex Cloud Functions pricing).
- Fee for the number of requests to the API gateway and outgoing traffic (see Yandex API Gateway pricing).
- Fee for standard queue requests (see Yandex Message Queue pricing).
- Secret storage and request fees (see Yandex Lockbox pricing).
Getting started
Register your bot in Telegram and get a token:
- Start the
BotFather
bot and send the/newbot
command to it. - Name your bot, e.g.,
Serverless Echo Telegram Bot
. This is the name users will see when talking to the bot. - Specify a username for your bot, e.g.,
ServerlessHelloTelegramBot
. You can use it to locate the bot in Telegram. The username must end with...Bot
or..._bot
.
Your Telegram bot's token will be displayed.
Create a Yandex Lockbox secret. In the Key field, enter TG_TOKEN
, and in the Value field, enter the returned Telegram bot token.
Deploy the application
-
In the management console
, select the folder where you want to deploy the application. -
Select Cloud Apps.
-
On the left-hand panel, select Marketplace.
-
Select Demo Telegram Bot and click Use.
-
Specify the following:
- Application name.
- Application description (not required).
- Service account with the
admin
role for the folder, or select Auto to have the service account created when installing the application. This service account will be used to create the application resources. - ID of the previously created Yandex Lockbox secret.
-
Click Install and wait for the installation to complete.
-
On the Overview page, find the API gateway under Application resources, go to the gateway page, and copy the service domain link.
-
To bind the function to the Telegram bot, run a request. Replace
<bot_token>
with your Telegram bot token, and<API_gateway_domain>
with a link to your API gateway's service domain.Linux/macOSWindows (cmd)Windows (PowerShell)curl \ --request POST \ --url "https://api.telegram.org/bot<bot_token>/setWebhook?url=<API_gateway_domain>/echo"
curl ^ --request POST ^ --url "https://api.telegram.org/bot<bot_token>/setWebhook?url=<API_gateway_domain>/echo"
curl.exe ` --request POST ` --url "https://api.telegram.org/bot<bot_token>/setWebhook?url=<API_gateway_domain>/echo"
Result:
{"ok":true,"result":true,"description":"Webhook was set"}
Test your website
Write to the bot on Telegram.