Last active
October 15, 2019 12:12
-
-
Save zetavg/5dab25e3f2a8da230945d3607c657531 to your computer and use it in GitHub Desktop.
telegraf-keyboard-bot on Google Cloud Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"telegraf": "^3.32.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment