- Notifications
You must be signed in to change notification settings - Fork5.7k
Automatic pass_* for Handlers using reflection: Autowiring#859
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
Show all changes
18 commits Select commitHold shift + click to select a range
d19af21
Initial version of autowire
JosXabf15f54
Changed order of keyword arguments in the hope that no user has treat…
JosXa1243680
Stable autowiring functionality
JosXacafd9f8
Added autowiring example
JosXa10e4aee
Extended autowiring example
JosXa44b922e
Added tests and more elaborate documentation for autowire
JosXa9c6efb8
Fixed StringCommandHandler pass-flags
JosXa6f02143
Comment changed
JosXa284e606
Fixing tests that only fail on Travis
JosXacd4aded
Reduced method complexity
JosXa05c16a1
Fixed small test problems
JosXab388d1a
Implemented test for base class `Handler` and fixed order of instruct…
JosXa5da1bb1
Switched to setattr() to reduce cyclomatic complexity
JosXa1216fa3
Making flake8 happy
JosXa6f308e8
Making flake8 happy
JosXa9403113
Removed unneeded tuple return values
JosXa2339606
Added erroneous parameter definition callback
JosXa287007f
Added erroneous parameter definition callback
JosXaFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletionsexamples/autowiring.py
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,113 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Bot that displays the autowiring functionality | ||
# This program is dedicated to the public domain under the CC0 license. | ||
""" | ||
This bot shows how to use `autowire=True` in Handler definitions to save a lot of effort typing | ||
the explicit pass_* flags. | ||
Usage: | ||
Autowiring example: Try sending /start, /data, "My name is Leandro", or some random text. | ||
Press Ctrl-C on the command line or send a signal to the process to stop the | ||
bot. | ||
""" | ||
import logging | ||
from telegram.ext import Updater, CommandHandler, RegexHandler | ||
# Enable logging | ||
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | ||
level=logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
def error(bot, update, error): | ||
logger.warning('Update "%s" caused error "%s"' % (update, error)) | ||
def start(bot, update, args): | ||
query = ' '.join(args) # `args` is magically defined | ||
if query: | ||
update.message.reply_text(query) | ||
else: | ||
update.message.reply_text("Example: /start here I am") | ||
def simple_update_only(update): | ||
""" | ||
A simple handler that only needs an `update` object. | ||
Useful e.g. for basic commands like /help that need to do nothing but reply with some text. | ||
""" | ||
update.message.reply_text("This should have produced an error " | ||
"for the MessageHandler in group=1.") | ||
def callback_with_data(bot, update, chat_data, user_data): | ||
msg = 'Adding something to chat_data...\n' | ||
chat_data['value'] = "I'm a chat_data value" | ||
msg += chat_data['value'] | ||
msg += '\n\n' | ||
msg += 'Adding something to user_data...\n' | ||
user_data['value'] = "I'm a user_data value" | ||
msg += user_data['value'] | ||
update.message.reply_text(msg, quote=True) | ||
def regex_with_groups(bot, update, groups, groupdict): | ||
update.message.reply_text("Nice, your {} is {}.".format(groups[0], groups[1])) | ||
update.message.reply_text('Groupdict: {}'.format(groupdict)) | ||
def callback_undefined_arguments(bot, update, chat_data, groups): | ||
pass | ||
def main(): | ||
# Create the Updater and pass it your bot's token. | ||
updater = Updater("TOKEN") | ||
# Get the dispatcher to register handlers | ||
dp = updater.dispatcher | ||
# Inject the `args` parameter automagically | ||
dp.add_handler(CommandHandler("start", start, autowire=True)) | ||
# A RegexHandler example where `groups` and `groupdict` are passed automagically | ||
# Examples: Send "My name is Leandro" or "My cat is blue". | ||
dp.add_handler(RegexHandler(r'[Mm]y (?P<object>.*) is (?P<value>.*)', | ||
regex_with_groups, | ||
autowire=True)) | ||
# This will raise an error because the bot argument is missing... | ||
dp.add_handler(CommandHandler('help', simple_update_only), group=1) | ||
# ... but with the autowiring capability, you can have callbacks with only an `update` argument. | ||
dp.add_handler(CommandHandler('help', simple_update_only, autowire=True), group=2) | ||
# Passing `chat_data` and `user_data` explicitly... | ||
dp.add_handler(CommandHandler("data", callback_with_data, | ||
pass_chat_data=True, | ||
pass_user_data=True)) | ||
# ... is equivalent to passing them automagically. | ||
dp.add_handler(CommandHandler("data", callback_with_data, autowire=True)) | ||
# An example of using the `groups` parameter which is not defined for a CommandHandler. | ||
# Uncomment the line below and you will see a warning. | ||
# dp.add_handler(CommandHandler("erroneous", callback_undefined_arguments, autowire=True)) | ||
dp.add_error_handler(error) | ||
updater.start_polling() | ||
# Run the bot until you press Ctrl-C or the process receives SIGINT, | ||
# SIGTERM or SIGABRT. This should be used most of the time, since | ||
# start_polling() is non-blocking and will stop the bot gracefully. | ||
updater.idle() | ||
if __name__ == '__main__': | ||
main() |
17 changes: 15 additions & 2 deletionstelegram/ext/callbackqueryhandler.py
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
13 changes: 12 additions & 1 deletiontelegram/ext/choseninlineresulthandler.py
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
23 changes: 18 additions & 5 deletionstelegram/ext/commandhandler.py
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.
Uh oh!
There was an error while loading.Please reload this page.
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.