test for documentation example, it's work

This commit is contained in:
dmitry 2023-09-04 00:38:17 +04:00
parent 3d95512bf2
commit 29a29733f7
2 changed files with 56 additions and 3 deletions

View File

@ -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.

View File

@ -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<MyContext>(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'))