|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Bot that displays the autowiring functionality |
| 5 | +# This program is dedicated to the public domain under the CC0 license. |
| 6 | +""" |
| 7 | +This bot shows how to use `autowire=True` in Handler definitions to save a lot of effort typing |
| 8 | +the explicit pass_* flags. |
| 9 | +
|
| 10 | +Usage: |
| 11 | +Autowiring example: Try sending /start, "test", /data or something random. |
| 12 | +Press Ctrl-C on the command line or send a signal to the process to stop the |
| 13 | +bot. |
| 14 | +""" |
| 15 | + |
| 16 | +import logging |
| 17 | + |
| 18 | +from telegram.ext import Updater, CommandHandler, MessageHandler, Filters |
| 19 | + |
| 20 | +# Enable logging |
| 21 | +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
| 22 | + level=logging.INFO) |
| 23 | + |
| 24 | +logger = logging.getLogger(__name__) |
| 25 | + |
| 26 | + |
| 27 | +def error(bot, update, error): |
| 28 | + logger.warning('Update "%s" caused error "%s"' % (update, error)) |
| 29 | + |
| 30 | + |
| 31 | +def start(bot, update, args): |
| 32 | + query = ' '.join(args) # `args` is magically defined |
| 33 | + if query: |
| 34 | + update.message.reply_text(query) |
| 35 | + else: |
| 36 | + update.message.reply_text("Example: /start here I am") |
| 37 | + |
| 38 | + |
| 39 | +def simple_update_only(update): |
| 40 | + """ |
| 41 | + A simple handler that only needs an `update` object. |
| 42 | + Useful e.g. for /help commands that need to do nothing but reply with some text. |
| 43 | + """ |
| 44 | + update.message.reply_text("This should have produced an error " |
| 45 | + "for the MessageHandler in group=1.") |
| 46 | + |
| 47 | + |
| 48 | +def callback_with_data(bot, update, chat_data, user_data): |
| 49 | + msg = 'Adding something to chat_data...\n' |
| 50 | + chat_data['value'] = "I'm a chat_data value" |
| 51 | + msg += chat_data['value'] |
| 52 | + |
| 53 | + msg += '\n\n' |
| 54 | + |
| 55 | + msg += 'Adding something to user_data...\n' |
| 56 | + user_data['value'] = "I'm a user_data value" |
| 57 | + msg += user_data['value'] |
| 58 | + |
| 59 | + update.message.reply_text(msg, quote=True) |
| 60 | + |
| 61 | + |
| 62 | +def main(): |
| 63 | + # Create the EventHandler and pass it your bot's token. |
| 64 | + updater = Updater("TOKEN") |
| 65 | + |
| 66 | + # Get the dispatcher to register handlers |
| 67 | + dp = updater.dispatcher |
| 68 | + |
| 69 | + # Inject the `args` parameter automagically |
| 70 | + dp.add_handler(CommandHandler("start", start, autowire=True)) |
| 71 | + |
| 72 | + # This will raise an error because the bot argument is missing... |
| 73 | + dp.add_handler(MessageHandler(Filters.text, simple_update_only), group=1) |
| 74 | + # ... but with the autowiring capability, you can have callbacks with only an `update` argument. |
| 75 | + dp.add_handler(MessageHandler(Filters.text, simple_update_only, autowire=True), group=2) |
| 76 | + |
| 77 | + # Passing `chat_data` and `user_data` explicitly... |
| 78 | + dp.add_handler(CommandHandler("data", callback_with_data, |
| 79 | + pass_chat_data=True, |
| 80 | + pass_user_data=True)) |
| 81 | + # ... is equivalent to passing them automagically. |
| 82 | + dp.add_handler(CommandHandler("data", callback_with_data, autowire=True)) |
| 83 | + |
| 84 | + dp.add_error_handler(error) |
| 85 | + updater.start_polling() |
| 86 | + |
| 87 | + # Run the bot until you press Ctrl-C or the process receives SIGINT, |
| 88 | + # SIGTERM or SIGABRT. This should be used most of the time, since |
| 89 | + # start_polling() is non-blocking and will stop the bot gracefully. |
| 90 | + updater.idle() |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + main() |
0 commit comments