tgbot-cpp icon indicating copy to clipboard operation
tgbot-cpp copied to clipboard

User's choise in sendPoll

Open minigo opened this issue 2 years ago • 6 comments

How to know about the user's choice, whether he chose the correct answer or not?

minigo avatar Oct 13 '22 12:10 minigo

Thanks! You can now add a listener with onPoll or with onPollAnswer depending on which Update object you need

llnulldisk avatar Oct 18 '22 17:10 llnulldisk

What conditions does the handler onPollAnswer work? In my project onPoll work everytime, onPollAnswer - never

minigo avatar Nov 14 '22 15:11 minigo

As stated in the docs, onPollAnswer fires, if someone answers the poll or if someone changes the answer in the poll sent by the bot. onPoll refers to the status of the poll itself and gives an update if the poll is closed with closePoll, for example. I tested it locally and can confirm that it works as intended. In case you encounter a different behaviour, could you provide a minimal code snippet to reproduce your problem?

llnulldisk avatar Nov 14 '22 16:11 llnulldisk

Creation:

    _bot = new TgBot::Bot (_key);
    _bot->getEvents ().onPollAnswer (std::bind (&Daemon::cmdOnPollAnswer, this, std::placeholders::_1));
    _bot->getEvents ().onPoll (std::bind (&Daemon::cmdOnPoll, this, std::placeholders::_1));

    std::vector<TgBot::BotCommand::Ptr> commands;

    auto command_question = std::make_shared<TgBot::BotCommand> ();
    command_question->command = "question";
    command_question->description = "Next question";
    commands.push_back (command_question);

    _bot->getApi ().setMyCommands (commands);

    _bot->getEvents ().onCommand ("start", std::bind (&Daemon::cmdStart, this, std::placeholders::_1));
    _bot->getEvents ().onCommand ("question", std::bind (&Daemon::cmdQuestion, this, std::placeholders::_1));

    try
    {
        TgBot::TgLongPoll longPoll (*_bot, 100, 5);

        while (_running)
        {
            longPoll.start ();
        }
    }
    catch (TgBot::TgException& e)
    { }

Sending:

    std::vector<std::string> options = {"1","2","3","4"};
    _bot->getApi ().sendPoll (message->chat->id,
                                                       "Answers",
                                                       options,
                                                       false,
                                                       0,
                                                       std::make_shared<TgBot::GenericReply> (),
                                                       true,
                                                       "quiz",
                                                       false,
                                                       correct_answer,
                                                       explanation);

minigo avatar Nov 15 '22 15:11 minigo

onPollAnswer:

Registers listener which receives an answer if a user changed their answer in a non-anonymous poll.

Does it help if you set isAnonymous = false instead of true?

llnulldisk avatar Nov 16 '22 18:11 llnulldisk

Thank you very much! Work great!

minigo avatar Nov 17 '22 08:11 minigo