From ad7e4bbaf7d2dd81bd02818f8c5a56238d1ec0e3 Mon Sep 17 00:00:00 2001 From: Muhammad Aadil Date: Sat, 28 Jan 2023 18:20:58 +0500 Subject: [PATCH 1/3] Added poll_example.py in the examples --- examples/poll_example.py | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 examples/poll_example.py diff --git a/examples/poll_example.py b/examples/poll_example.py new file mode 100644 index 0000000..4fee73f --- /dev/null +++ b/examples/poll_example.py @@ -0,0 +1,45 @@ +#!/usr/bin/python + +# This is an example file to create polls and handle poll answers +import telebot + +API_TOKEN = "" + +bot = telebot.TeleBot(API_TOKEN) + + +DATA = {} + + +@bot.message_handler(commands=["poll"]) +def create_poll(message): + bot.send_message(message.chat.id, "English Article Test") + answer_options = ["a", "an", "the", "-"] + + # is_anonymous = False -- if you want to check the user answer like here + poll = bot.send_poll( + chat_id=message.chat.id, + question="We are going to '' park.", + options=answer_options, + is_anonymous=False, + ) + + # store the poll_id against user_id in the database + poll_id = poll.poll.id + DATA[message.chat.id] = poll_id + + +@bot.poll_answer_handler() +def handle_poll_answers(poll): + + if DATA.get(poll.user.id) == poll.poll_id: + # to check the correct answer + user_answer = poll.option_ids[0] + correct_answer = 2 # "the" is the correct answer + if user_answer == correct_answer: + bot.send_message(poll.user.id, "Good! You're right.") + else: + bot.send_message(poll.user.id, 'The correct answer is "the" .') + + +bot.infinity_polling() From 10a80e1cfa8ebac66693838c9c3b706fd7804f9e Mon Sep 17 00:00:00 2001 From: Muhammad Aadil Date: Sat, 4 Feb 2023 10:27:29 +0500 Subject: [PATCH 2/3] add only quiz type poll example to the poll_example.py --- examples/poll_example.py | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/examples/poll_example.py b/examples/poll_example.py index 4fee73f..fe1ac64 100644 --- a/examples/poll_example.py +++ b/examples/poll_example.py @@ -1,6 +1,6 @@ #!/usr/bin/python -# This is an example file to create polls and handle poll answers +# This is an example file to create quiz polls import telebot API_TOKEN = "" @@ -8,38 +8,20 @@ API_TOKEN = "" bot = telebot.TeleBot(API_TOKEN) -DATA = {} - - @bot.message_handler(commands=["poll"]) def create_poll(message): bot.send_message(message.chat.id, "English Article Test") answer_options = ["a", "an", "the", "-"] - # is_anonymous = False -- if you want to check the user answer like here - poll = bot.send_poll( + bot.send_poll( chat_id=message.chat.id, question="We are going to '' park.", options=answer_options, + type='quiz', + correct_option_id=2, is_anonymous=False, ) - # store the poll_id against user_id in the database - poll_id = poll.poll.id - DATA[message.chat.id] = poll_id - - -@bot.poll_answer_handler() -def handle_poll_answers(poll): - - if DATA.get(poll.user.id) == poll.poll_id: - # to check the correct answer - user_answer = poll.option_ids[0] - correct_answer = 2 # "the" is the correct answer - if user_answer == correct_answer: - bot.send_message(poll.user.id, "Good! You're right.") - else: - bot.send_message(poll.user.id, 'The correct answer is "the" .') bot.infinity_polling() From ed6d6cc03fb556d1509c45f725bf0179300b875f Mon Sep 17 00:00:00 2001 From: Muhammad Aadil Date: Sat, 4 Feb 2023 11:22:49 +0500 Subject: [PATCH 3/3] add poll answer handler to poll_example.py to show the example to send next poll or log user answers --- examples/poll_example.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/examples/poll_example.py b/examples/poll_example.py index fe1ac64..8a3e16b 100644 --- a/examples/poll_example.py +++ b/examples/poll_example.py @@ -17,11 +17,16 @@ def create_poll(message): chat_id=message.chat.id, question="We are going to '' park.", options=answer_options, - type='quiz', + type="quiz", correct_option_id=2, is_anonymous=False, ) +@bot.poll_answer_handler() +def handle_poll(poll): + # This handler can be used to log User answers and to send next poll + pass + bot.infinity_polling()