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 it the/newbot
command. - Enter a name for your bot, such as
Serverless Echo Telegram Bot
. This is the name users will see when talking to the bot. - Specify a username for your bot, such as
ServerlessHelloTelegramBot
. You can use the user name to search for the bot in Telegram. The user name 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 the
<bot_token>
with the Telegram bot token and the<API_gateway_domain>
with a link to the 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 the controller
Write to the bot on Telegram.