Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions ru/_tutorials/serverless/telegram-bot-serverless.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# Как создать бота в Telegram


С помощью serverless-технологий можно создать [бота](../../glossary/chat-bot.md) для Telegram, который будет отвечать на сообщения в чате.


<iframe width="640" height="360" src="https://runtime.strm.yandex.ru/player/video/vplvjsucpfkw2e4zdzy2?autoplay=0&mute=0" allow="autoplay; fullscreen; picture-in-picture; encrypted-media" frameborder="0" scrolling="no"></iframe>

[Смотреть видео на YouTube](https://www.youtube.com/watch?v=C2Ahit2EBo0).



Чтобы создать бота:

1. [Подготовьте окружение](#before-begin).
Expand Down Expand Up @@ -40,18 +36,18 @@
1. Клонируйте [репозиторий](https://sourcecraft.dev/yandex-cloud-examples/yc-telegram-bot-serverless) с исходным кодом, необходимым для создания бота, для этого в терминале выполните команду [git](https://git-scm.com/):

```bash
git clone https://git@git.sourcecraft.dev/yandex-cloud-examples/yc-telegram-bot-serverless.git
git clone [https://git@git.sourcecraft.dev/yandex-cloud-examples/yc-telegram-bot-serverless.git](https://git@git.sourcecraft.dev/yandex-cloud-examples/yc-telegram-bot-serverless.git)
```

1. [Создайте](../../iam/operations/sa/create.md) сервисный аккаунт и [назначьте](../../iam/operations/sa/assign-role-for-sa.md) ему роли `{{ roles-editor }}` и `{{ roles-functions-invoker }}` на ваш каталог.
1. [Создайте](../../iam/operations/sa/create.md) сервисный аккаунт и [назначьте](../../iam/operations/sa/assign-role-for-sa.md) ему роли `{{ roles-editor }}` и `{{ roles-functions-invoker }}` на ваш каталог.

## Зарегистрируйте Telegram-бота {#create-bot}

Зарегистрируйте вашего бота в Telegram и получите токен.

1. Для регистрации нового бота запустите бота [BotFather](https://t.me/BotFather) и отправьте команду:

```
```text
/newbot
```

Expand All @@ -62,7 +58,7 @@

1. Установите иконку для бота — файл `sayhello.png` из сохраненного архива. Отправьте боту BotFather команду:

```
```text
/setuserpic
```

Expand Down Expand Up @@ -182,12 +178,14 @@
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.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-шлюза>/sayhello.png'});
ctx.reply(`Hello, ${ctx.message.from.username}`);

});

module.exports.handler = async function (event, context) {
Expand Down Expand Up @@ -262,7 +260,7 @@
```bash
curl \
--request POST \
--url https://api.telegram.org/bot<токен_бота>/setWebhook \
--url [https://api.telegram.org/bot](https://api.telegram.org/bot)<токен_бота>/setWebhook \
--header 'content-type: application/json' \
--data '{"url": "<домен_API-шлюза>/fshtb-function"}'
```
Expand All @@ -272,7 +270,7 @@
```bash
curl ^
--request POST ^
--url https://api.telegram.org/bot<токен_бота>/setWebhook ^
--url [https://api.telegram.org/bot](https://api.telegram.org/bot)<токен_бота>/setWebhook ^
--header "content-type: application/json" ^
--data "{\"url\": \"<домен_API-шлюза>/fshtb-function\"}"
```
Expand All @@ -282,9 +280,9 @@
```powershell
curl.exe `
--request POST `
--url https://api.telegram.org/bot<токен_бота>/setWebhook `
--url [https://api.telegram.org/bot](https://api.telegram.org/bot)<токен_бота>/setWebhook `
--header '"content-type: application/json"' `
--data '"{ \"url\": \"<домен_API-шлюза>/fshtb-function\" }"'
--data '{ "url": "<домен_API-шлюза>/fshtb-function" }'
```

Где:
Expand All @@ -294,7 +292,7 @@

Результат:

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

Expand Down