I am using Deno and GrammY to create a simple bot that will send a poll to the user on the /q command, and then when the user attempts the poll, would reply to the user on the basis of the choice that he took.
The code of the the bot.ts as of now looks like:
import { Bot } from "https://deno.land/x/[email protected]/mod.ts";
const bot = new Bot(MY_REDACTED_BOT_TOKEN);
bot.command("q", async (ctx) => {
ctx.api.sendPoll(
ctx.msg.chat.id,
`What is 1+1?`,
["0", "1", "2", "3"],
{
is_anonymous: false,
type: "quiz",
correct_option_id: 2,
}
);
});
bot.start();
How can I add the functionality to wait for the user to attempt the quiz and then proceed on the basis of it (something equivalent to PollAnswerHandler in python-telegram-bot)?
You can listen for
poll_answerlike shown below. You will find all relevant information in the context objectctx, specifically underctx.pollAnswer, e.g theuserand the chosenoption_ids(i.e selected answer(s)) as demonstrated in this code example:In the code to send the poll I also added
const message =to save the return value, which containsmessage.poll.id. This can be used to keep track of which poll was sent to which user so that you can easily find out to which poll a poll response refers. Note: the poll Id changes every time you send the poll.The console would show for example: