From 29a29733f7a077fedd4ba480cc534ca754ed505b Mon Sep 17 00:00:00 2001 From: dmitry Date: Mon, 4 Sep 2023 00:38:17 +0400 Subject: [PATCH] test for documentation example, it's work --- packages/telegram/README.md | 5 ++++ packages/telegram/src/index.ts | 54 ++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 packages/telegram/README.md diff --git a/packages/telegram/README.md b/packages/telegram/README.md new file mode 100644 index 0000000..696778b --- /dev/null +++ b/packages/telegram/README.md @@ -0,0 +1,5 @@ +For starting work you need creating telegram bot, and token for it + +Telegram token +To use the Telegram Bot API, you first have to get a bot account by chatting with BotFather (https://core.telegram.org/bots#6-botfather). +BotFather will give you a token, something like 123456789:AbCdefGhIJKlmNoPQRsTUVwxyZ. diff --git a/packages/telegram/src/index.ts b/packages/telegram/src/index.ts index 399f1b3..bc16727 100644 --- a/packages/telegram/src/index.ts +++ b/packages/telegram/src/index.ts @@ -1,6 +1,54 @@ import { loadBotConfig } from "config-loader"; -import { Telegraf } from 'telegraf'; +import { Telegraf, Context } from 'telegraf'; +import { message } from 'telegraf/filters' const config = loadBotConfig(); -const bot = new Telegraf(config.token); -console.log(JSON.stringify(config)); + +interface MyContext extends Context { + myProp?: string + myOtherProp?: number + +} +const bot = new Telegraf(config.token); + +bot.command('quit', async (ctx) => { + // Explicit usage + await ctx.telegram.leaveChat(ctx.message.chat.id) + + // Using context shortcut + await ctx.leaveChat() +}) + +bot.on(message('text'), async (ctx) => { + // Explicit usage + await ctx.telegram.sendMessage(ctx.message?.chat?.id, `Hello ${ctx.state.role}`) + + // Using context shortcut + await ctx.reply(`Hello ${ctx.state.role}`) +}) + +bot.on('callback_query', async (ctx) => { + // Explicit usage + await ctx.telegram.answerCbQuery(ctx.callbackQuery.id) + + // Using context shortcut + await ctx.answerCbQuery() +}) + +bot.on('inline_query', async (ctx) => { + //@ts-ignore + const result = [] + // Explicit usage + //@ts-ignore + await ctx.telegram.answerInlineQuery(ctx.inlineQuery.id, result) + + // Using context shortcut + //@ts-ignore + await ctx.answerInlineQuery(result) +}) + +bot.launch() + +// Enable graceful stop +process.once('SIGINT', () => bot.stop('SIGINT')) +process.once('SIGTERM', () => bot.stop('SIGTERM'))