Last active
January 4, 2024 11:54
-
-
Save bouchtaoui-dev/38b74f035e432819aac183500ee7f8f1 to your computer and use it in GitHub Desktop.
Example Telegraf
This file contains 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
const { | |
session, | |
Scenes: { Stage, WizardScene }, | |
Markup, | |
} = require("telegraf"); | |
const questionWizard = new WizardScene( | |
"create-question", | |
async (ctx) => { | |
await ctx.reply(`What's the subject?`); | |
ctx.wizard.cursor = 0; | |
return ctx.wizard.next(); | |
}, | |
async (ctx) => { | |
const resp = ctx.message.text; | |
console.log("Received response: ", resp); | |
await ctx.reply(`What's the description?`); | |
return ctx.wizard.next(); | |
}, | |
async (ctx) => { | |
const resp = ctx.message.text; | |
console.log("Received response: ", resp); | |
await ctx.reply( | |
"Choose one", | |
Markup.keyboard([["A", "B", "C"]]) | |
.oneTime() | |
.resize() | |
); | |
return ctx.wizard.next(); | |
}, | |
async (ctx) => { | |
const resp = ctx.message.text; | |
console.log("Received response: ", resp); | |
await ctx.reply("Thank you for your response"); | |
return await ctx.scene.leave(); | |
} | |
); | |
questionWizard.leave(async (ctx) => await ctx.reply("-- BYE --")); | |
const questionize = (bot) => { | |
console.log("reminder function called!"); | |
const stage = new Stage([questionWizard], { default: "create-question" }); | |
bot.use(session()); | |
bot.use(stage.middleware()); | |
bot.command("xxx", async (ctx) => { | |
console.log("Hit /xxx"); | |
await ctx.scene.enter("create-question"); | |
}); | |
}; | |
module.exports = { questionize }; |
This file contains 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
// Setup chatbot | |
const { Telegraf, session, Markup } = require("telegraf"); | |
const { message } = require("telegraf/filters"); | |
const { questionize } = require("./bot/reminder"); | |
const bot = new Telegraf(process.env.BOT_TOKEN); | |
//------------------ | |
// attach questionze to bot | |
questionize(bot); | |
//------------------ | |
// ----------------- | |
bot.command("pyramid", (ctx) => { | |
return ctx.reply( | |
"Keyboard wrap", | |
Markup.keyboard(["one", "two", "three", "four", "five", "six"], { | |
wrap: (btn, index, currentRow) => currentRow.length >= (index + 1) / 2, | |
}) | |
); | |
}); | |
//------------------ | |
bot.start((ctx) => ctx.reply("Welcome")); | |
bot.help((ctx) => ctx.reply(helpMessage)); | |
bot.hears("hi", (ctx) => ctx.reply("Hey there, how can I help you?")); | |
bot.command("quit", async (ctx) => { | |
// Explicit usage | |
await ctx.telegram.leaveChat(ctx.message.chat.id); | |
// Using context shortcut | |
await ctx.leaveChat(); | |
}); | |
//------------------ | |
bot.command("test", (ctx) => { | |
return ctx.reply("<b>Coke</b> or <i>Pepsi?</i>", { | |
parse_mode: "HTML", | |
...Markup.inlineKeyboard([ | |
Markup.button.callback("Coke", "Coke"), | |
Markup.button.callback("Pepsi", "Pepsi"), | |
]), | |
}); | |
}); | |
// ----------------- | |
bot.catch((err, ctx) => { | |
console.log(`Ooops, encountered an error for ${ctx.updateType}`, err); | |
}); | |
bot.launch(); | |
// Enable graceful stop | |
process.once("SIGINT", () => bot.stop("SIGINT")); | |
process.once("SIGTERM", () => bot.stop("SIGTERM")); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment