test for documentation example, it's work
This commit is contained in:
parent
3d95512bf2
commit
29a29733f7
5
packages/telegram/README.md
Normal file
5
packages/telegram/README.md
Normal 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.
|
@ -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'))
|
||||
|
Loading…
Reference in New Issue
Block a user