Skip to content

Instantly share code, notes, and snippets.

@zetavg
Last active October 15, 2019 12:12
Show Gist options
  • Save zetavg/5dab25e3f2a8da230945d3607c657531 to your computer and use it in GitHub Desktop.
Save zetavg/5dab25e3f2a8da230945d3607c657531 to your computer and use it in GitHub Desktop.
telegraf-keyboard-bot on Google Cloud Functions
/**
* telegraf-keyboard-bot
*
* Function to execute: handleRequest
*
* Required environment variables:
*
* - TELEGRAM_BOT_AUTHENTICATION_TOKEN
*
* Run
* curl 'https://api.telegram.org/bot<TELEGRAM_BOT_AUTHENTICATION_TOKEN>/setWebhook?url=<CLOUD_FUNCTION_URL>'
* to register the bot.
*
*/
const Telegraf = require('telegraf')
const Extra = require('telegraf/extra')
const Markup = require('telegraf/markup')
const bot = new Telegraf(process.env.TELEGRAM_BOT_AUTHENTICATION_TOKEN)
////////////
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.command('caption', (ctx) => {
return ctx.replyWithPhoto({ url: 'https://picsum.photos/200/300/?random' },
Extra.load({ caption: 'Caption' })
.markdown()
.markup((m) =>
m.inlineKeyboard([
m.callbackButton('Plain', 'plain'),
m.callbackButton('Italic', 'italic')
])
)
)
})
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('plain', async (ctx) => {
await ctx.answerCbQuery()
await ctx.editMessageCaption('Caption', Markup.inlineKeyboard([
Markup.callbackButton('Plain', 'plain'),
Markup.callbackButton('Italic', 'italic')
]))
})
bot.action('italic', async (ctx) => {
await ctx.answerCbQuery()
await ctx.editMessageCaption('_Caption_', Extra.markdown().markup(Markup.inlineKeyboard([
Markup.callbackButton('Plain', 'plain'),
Markup.callbackButton('* Italic *', 'italic')
])))
})
bot.action(/.+/, (ctx) => {
return ctx.answerCbQuery(`Oh, ${ctx.match[0]}! Great choice`)
})
////////////
bot.catch((e) => {
console.error(e)
})
exports.handleRequest = async (req, res) => {
try {
await bot.handleUpdate(req.body, res)
} catch (e) {
console.error(e)
}
res.status(200).send()
}
{
"dependencies": {
"telegraf": "^3.32.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment