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
22 changes: 10 additions & 12 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 @@ -43,15 +39,15 @@
git clone 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 @@ -284,7 +282,7 @@
--request POST `
--url 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