X Tutup
Skip to content

Commit 884bb41

Browse files
committed
update README with dispatcher changes
1 parent 1e19084 commit 884bb41

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

README.rst

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -314,53 +314,60 @@ Now, we need to define a function that should process a specific type of update:
314314
>>> def start(bot, update):
315315
... bot.sendMessage(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")
316316

317-
We want this function to be called on a Telegram message that contains the ``/start`` command, so we need to register it in the dispatcher::
317+
We want this function to be called on a Telegram message that contains the ``/start`` command. To do that, we have to use a ``CommandHandler`` object and register it in the dispatcher::
318318

319-
>>> dispatcher.addTelegramCommandHandler('start', start)
319+
>>> from telegram.ext import CommandHandler
320+
>>> start_handler = CommandHandler('start', start)
321+
>>> dispatcher.addHandler(start_handler)
320322

321323
The last step is to tell the ``Updater`` to start working::
322324

323325
>>> updater.start_polling()
324326

325-
Our bot is now up and running (go ahead and try it)! It's not doing anything yet, besides answering to the ``/start`` command. Let's add another handler function and register it::
327+
Our bot is now up and running (go ahead and try it)! It's not doing anything yet, besides answering to the ``/start`` command. Let's add another handler that listens for regular messages. We're using the `MessageHandler` here to echo to all text messages::
326328

327329
>>> def echo(bot, update):
328330
... bot.sendMessage(chat_id=update.message.chat_id, text=update.message.text)
329331
...
330-
>>> dispatcher.addTelegramMessageHandler(echo)
332+
>>> from telegram.ext import MessageHandler
333+
>>> from telegram.ext import filters
334+
>>> echo_handler = MessageHandler([filters.TEXT], echo)
335+
>>> dispatcher.addHandler(echo_handler)
331336

332-
Our bot should now reply to all messages that are not a command with a message that has the same content.
337+
Our bot should now reply to all text messages that are not a command with a message that has the same content.
333338

334-
People might try to send commands to the bot that it doesn't understand, so we should get that covered as well::
335-
336-
>>> def unknown(bot, update):
337-
... bot.sendMessage(chat_id=update.message.chat_id, text="Sorry, I didn't understand that command.")
338-
...
339-
>>> dispatcher.addUnknownTelegramCommandHandler(unknown)
340-
341-
Let's add some functionality to our bot. We want to add the ``/caps`` command, that will take some text as parameter and return it in all caps. We can get the arguments that were passed to the command in the handler function simply by adding it to the parameter list::
339+
Let's add some functionality to our bot. We want to add the ``/caps`` command, that will take some text as parameter and return it in all caps. We can get the arguments that were passed to a command in the handler function::
342340

343341
>>> def caps(bot, update, args):
344342
... text_caps = ' '.join(args).upper()
345343
... bot.sendMessage(chat_id=update.message.chat_id, text=text_caps)
346344
...
347-
>>> dispatcher.addTelegramCommandHandler('caps', caps)
345+
>>> caps_handler = CommandHandler('caps', caps, pass_args=True)
346+
>>> dispatcher.addHandler(caps_handler)
348347

349348
To enable our bot to respond to inline queries, we can add the following (you will also have to talk to BotFather)::
350349

351350
>>> from telegram import InlineQueryResultArticle
352351
>>> def inline_caps(bot, update):
353-
... # If you activated inline feedback, updates might either contain
354-
... # inline_query or chosen_inline_result, the other one will be None
355-
... if update.inline_query:
356-
... query = bot.update.inline_query.query
357-
... results = list()
358-
... results.append(InlineQueryResultArticle(query.upper(), 'Caps', text_caps))
359-
... bot.answerInlineQuery(update.inline_query.id, results)
352+
... query = bot.update.inline_query.query
353+
... results = list()
354+
... results.append(InlineQueryResultArticle(query.upper(), 'Caps', query.upper()))
355+
... bot.answerInlineQuery(update.inline_query.id, results)
356+
...
357+
>>> from telegram.ext import InlineQueryHandler
358+
>>> inline_caps_handler = InlineQueryHandler(inline_caps)
359+
>>> dispatcher.addHandler(inline_caps_handler)
360+
361+
People might try to send commands to the bot that it doesn't understand, so we can use a ``RegexHandler`` to recognize all commands that were not recognized by the previous handlers. **Note:** This handler has to be added last, else it will be triggered before the ``CommandHandler``s had a chance to look at the update::
362+
363+
>>> def unknown(bot, update):
364+
... bot.sendMessage(chat_id=update.message.chat_id, text="Sorry, I didn't understand that command.")
360365
...
361-
>>> dispatcher.addTelegramInlineHandler(inline_caps)
366+
>>> from telegram.ext import RegexHandler
367+
>>> unknown_handler = RegexHandler(r'/.*', unknown)
368+
>>> dispatcher.addHandler(unknown_handler)
362369

363-
Now it's time to stop the bot::
370+
If you're done playing around, stop the bot with this::
364371

365372
>>> updater.stop()
366373

0 commit comments

Comments
 (0)
X Tutup