-
Notifications
You must be signed in to change notification settings - Fork 5.9k
4.6 API update #1723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
4.6 API update #1723
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
9c8b803
First take on 4.6 support
Poolitzer 9e7274a
improved docs
Poolitzer 8d0fa03
Minor doc formattings
Bibo-Joshi d265ecc
added poll and poll_answer to filters
Poolitzer 0849835
added tests, fixed mentioned issues
Poolitzer 7d25d19
added poll_answer + poll filter tests
Poolitzer a929274
Update docs according to official API docs
Bibo-Joshi 6239d9a
introducing pollhandler and pollanswerhandler
Poolitzer 5557e2a
Merge remote-tracking branch 'upstream/4.6-update' into 4.6-update
Poolitzer bd1eafe
First take on 4.6 support
Poolitzer 4836de4
improved docs
Poolitzer 7989fa8
Minor doc formattings
Bibo-Joshi 25c9dba
added poll and poll_answer to filters
Poolitzer 51df566
added tests, fixed mentioned issues
Poolitzer c7e0f9c
added poll_answer + poll filter tests
Poolitzer c935f10
introducing pollhandler and pollanswerhandler
Poolitzer 07adcec
Update docs according to official API docs
Bibo-Joshi d492038
Merge remote-tracking branch 'upstream/4.6-update' into 4.6-update
Poolitzer 8f1f251
correct_option_id validated with None
sharunkumar a5e770f
Merge pull request #1764 from sharunkumar/patch-1
Poolitzer a36ffd2
improving example
Poolitzer 517cc3d
improving code
Poolitzer 92556df
adding poll filter example to the pollbot.py
Poolitzer f9883e3
Update Readme
Bibo-Joshi a275257
simplify pollbot.py and add some comments
Bibo-Joshi a2fa913
add tests for Poll(Answer)Handler
Bibo-Joshi 4556f09
Merge branch 'master' into 4.6-update
Bibo-Joshi ded8788
We just want Filters.poll, not Filters.update.poll
Bibo-Joshi 41d4da8
Merge master
Bibo-Joshi 413b518
Merge branch 'master' into 4.6-update
Bibo-Joshi 0dd5931
Make test_official fail again
Bibo-Joshi 472d981
Handle ME.language in M._parse_*
Bibo-Joshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| telegram.KeyboardButtonPollType | ||
| =============================== | ||
|
|
||
| .. autoclass:: telegram.KeyboardButtonPollType | ||
| :members: | ||
| :show-inheritance: |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| telegram.PollAnswer | ||
| =================== | ||
|
|
||
| .. autoclass:: telegram.PollAnswer | ||
| :members: | ||
| :show-inheritance: |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| #!/usr/bin/env python | ||
| # -*- coding: utf-8 -*- | ||
| # This program is dedicated to the public domain under the CC0 license. | ||
|
|
||
| """ | ||
| Basic example for a bot that works with polls. Only 3 people are allowed to interact with each | ||
| poll/quiz the bot generates. The preview command generates a closed poll/quiz, excatly like the | ||
| one the user sends the bot | ||
| """ | ||
| import logging | ||
|
|
||
| from telegram import (Poll, ParseMode, KeyboardButton, KeyboardButtonPollType, | ||
| ReplyKeyboardMarkup, ReplyKeyboardRemove) | ||
| from telegram.ext import (Updater, CommandHandler, PollAnswerHandler, PollHandler, MessageHandler, | ||
| Filters) | ||
| from telegram.utils.helpers import mention_html | ||
|
|
||
| logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | ||
| level=logging.INFO) | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def start(update, context): | ||
| """Inform user about what this bot can do""" | ||
| update.message.reply_text('Please select /poll to get a Poll, /quiz to get a Quiz or /preview' | ||
| ' to generate a preview for your poll') | ||
|
|
||
|
|
||
| def poll(update, context): | ||
| """Sends a predefined poll""" | ||
| questions = ["Good", "Really good", "Fantastic", "Great"] | ||
| message = context.bot.send_poll(update.effective_user.id, "How are you?", questions, | ||
| is_anonymous=False, allows_multiple_answers=True) | ||
| # Save some info about the poll the bot_data for later use in receive_poll_answer | ||
| payload = {message.poll.id: {"questions": questions, "message_id": message.message_id, | ||
| "chat_id": update.effective_chat.id, "answers": 0}} | ||
| context.bot_data.update(payload) | ||
|
|
||
|
|
||
| def receive_poll_answer(update, context): | ||
| """Summarize a users poll vote""" | ||
| answer = update.poll_answer | ||
| poll_id = answer.poll_id | ||
| try: | ||
| questions = context.bot_data[poll_id]["questions"] | ||
| # this means this poll answer update is from an old poll, we can't do our answering then | ||
| except KeyError: | ||
| return | ||
| selected_options = answer.option_ids | ||
| answer_string = "" | ||
| for question_id in selected_options: | ||
| if question_id != selected_options[-1]: | ||
| answer_string += questions[question_id] + " and " | ||
| else: | ||
| answer_string += questions[question_id] | ||
| user_mention = mention_html(update.effective_user.id, update.effective_user.full_name) | ||
| context.bot.send_message(context.bot_data[poll_id]["chat_id"], | ||
| "{} feels {}!".format(user_mention, answer_string), | ||
| parse_mode=ParseMode.HTML) | ||
| context.bot_data[poll_id]["answers"] += 1 | ||
| # Close poll after three participants voted | ||
| if context.bot_data[poll_id]["answers"] == 3: | ||
| context.bot.stop_poll(context.bot_data[poll_id]["chat_id"], | ||
| context.bot_data[poll_id]["message_id"]) | ||
|
|
||
|
|
||
| def quiz(update, context): | ||
| """Send a predefined poll""" | ||
| questions = ["1", "2", "4", "20"] | ||
| message = update.effective_message.reply_poll("How many eggs do you need for a cake?", | ||
| questions, type=Poll.QUIZ, correct_option_id=2) | ||
| # Save some info about the poll the bot_data for later use in receive_quiz_answer | ||
| payload = {message.poll.id: {"chat_id": update.effective_chat.id, | ||
| "message_id": message.message_id}} | ||
| context.bot_data.update(payload) | ||
|
|
||
|
|
||
| def receive_quiz_answer(update, context): | ||
| """Close quiz after three participants took it""" | ||
| # the bot can receive closed poll updates we don't care about | ||
| if update.poll.is_closed: | ||
| return | ||
| if update.poll.total_voter_count == 3: | ||
| try: | ||
| quiz_data = context.bot_data[update.poll.id] | ||
| # this means this poll answer update is from an old poll, we can't stop it then | ||
| except KeyError: | ||
| return | ||
| context.bot.stop_poll(quiz_data["chat_id"], quiz_data["message_id"]) | ||
|
|
||
|
|
||
| def preview(update, context): | ||
| """Ask user to create a poll and display a preview of it""" | ||
| # using this without a type lets the user chooses what he wants (quiz or poll) | ||
| button = [[KeyboardButton("Press me!", request_poll=KeyboardButtonPollType())]] | ||
| message = "Press the button to let the bot generate a preview for your poll" | ||
| # using one_time_keyboard to hide the keyboard | ||
| update.effective_message.reply_text(message, | ||
| reply_markup=ReplyKeyboardMarkup(button, | ||
| one_time_keyboard=True)) | ||
|
|
||
|
|
||
| def receive_poll(update, context): | ||
| """On receiving polls, reply to it by a closed poll copying the received poll""" | ||
| actual_poll = update.effective_message.poll | ||
| # Only need to set the question and options, since all other parameters don't matter for | ||
| # a closed poll | ||
| update.effective_message.reply_poll( | ||
| question=actual_poll.question, | ||
| options=[o.text for o in actual_poll.options], | ||
| # with is_closed true, the poll/quiz is immediately closed | ||
| is_closed=True, | ||
| reply_markup=ReplyKeyboardRemove() | ||
| ) | ||
|
|
||
|
|
||
| def help_handler(update, context): | ||
| """Display a help message""" | ||
| update.message.reply_text("Use /quiz, /poll or /preview to test this " | ||
| "bot.") | ||
|
|
||
|
|
||
| def main(): | ||
| # Create the Updater and pass it your bot's token. | ||
| # Make sure to set use_context=True to use the new context based callbacks | ||
| # Post version 12 this will no longer be necessary | ||
| updater = Updater("TOKEN", use_context=True) | ||
| dp = updater.dispatcher | ||
| dp.add_handler(CommandHandler('start', start)) | ||
| dp.add_handler(CommandHandler('poll', poll)) | ||
| dp.add_handler(PollAnswerHandler(receive_poll_answer)) | ||
| dp.add_handler(CommandHandler('quiz', quiz)) | ||
| dp.add_handler(PollHandler(receive_quiz_answer)) | ||
| dp.add_handler(CommandHandler('preview', preview)) | ||
| dp.add_handler(MessageHandler(Filters.poll, receive_poll)) | ||
| dp.add_handler(CommandHandler('help', help_handler)) | ||
|
|
||
| # Start the Bot | ||
| updater.start_polling() | ||
|
|
||
| # Run the bot until the user presses Ctrl-C or the process receives SIGINT, | ||
| # SIGTERM or SIGABRT | ||
| updater.idle() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.