telegraf
Version:
📡 Modern Telegram bot framework
93 lines (78 loc) • 2.46 kB
JavaScript
const Telegraf = require('telegraf')
const Extra = require('telegraf/extra')
const Markup = require('telegraf/markup')
const bot = new Telegraf(process.env.BOT_TOKEN)
bot.use(Telegraf.log())
bot.command('onetime', ({ reply }) =>
reply('One time keyboard', Markup
.keyboard(['/simple', '/inline', '/pyramid'])
.oneTime()
.resize()
.extra()
)
)
bot.command('custom', ({ reply }) => {
return reply('Custom buttons keyboard', Markup
.keyboard([
['🔍 Search', '😎 Popular'], // Row1 with 2 buttons
['☸ Setting', '📞 Feedback'], // Row2 with 2 buttons
['📢 Ads', '⭐️ Rate us', '👥 Share'] // Row3 with 3 buttons
])
.oneTime()
.resize()
.extra()
)
})
bot.hears('🔍 Search', ctx => ctx.reply('Yay!'))
bot.hears('📢 Ads', ctx => ctx.reply('Free hugs. Call now!'))
bot.command('special', (ctx) => {
return ctx.reply('Special buttons keyboard', Extra.markup((markup) => {
return markup.resize()
.keyboard([
markup.contactRequestButton('Send contact'),
markup.locationRequestButton('Send location')
])
}))
})
bot.command('pyramid', (ctx) => {
return ctx.reply('Keyboard wrap', Extra.markup(
Markup.keyboard(['one', 'two', 'three', 'four', 'five', 'six'], {
wrap: (btn, index, currentRow) => currentRow.length >= (index + 1) / 2
})
))
})
bot.command('simple', (ctx) => {
return ctx.replyWithHTML('<b>Coke</b> or <i>Pepsi?</i>', Extra.markup(
Markup.keyboard(['Coke', 'Pepsi'])
))
})
bot.command('inline', (ctx) => {
return ctx.reply('<b>Coke</b> or <i>Pepsi?</i>', Extra.HTML().markup((m) =>
m.inlineKeyboard([
m.callbackButton('Coke', 'Coke'),
m.callbackButton('Pepsi', 'Pepsi')
])))
})
bot.command('random', (ctx) => {
return ctx.reply('random example',
Markup.inlineKeyboard([
Markup.callbackButton('Coke', 'Coke'),
Markup.callbackButton('Dr Pepper', 'Dr Pepper', Math.random() > 0.5),
Markup.callbackButton('Pepsi', 'Pepsi')
]).extra()
)
})
bot.hears(/\/wrap (\d+)/, (ctx) => {
return ctx.reply('Keyboard wrap', Extra.markup(
Markup.keyboard(['one', 'two', 'three', 'four', 'five', 'six'], {
columns: parseInt(ctx.match[1])
})
))
})
bot.action('Dr Pepper', (ctx, next) => {
return ctx.reply('👍').then(() => next())
})
bot.action(/.+/, (ctx) => {
return ctx.answerCbQuery(`Oh, ${ctx.match[0]}! Great choice`)
})
bot.startPolling()