1818Type 'stop' on the command line to stop the bot.
1919"""
2020
21- from telegram .ext import Updater
21+ from telegram .ext import Updater , StringCommandHandler , StringRegexHandler , \
22+ MessageHandler , CommandHandler , RegexHandler , filters
2223from telegram .ext .dispatcher import run_async
2324from time import sleep
2425import logging
3435last_chat_id = 0
3536
3637
37- # Define a few (command) handlers. These usually take the two arguments bot and
38- # update. Error handlers also receive the raised TelegramError object in error.
38+ # Define a few (command) handler callback functions. These usually take the
39+ # two arguments bot and update. Error handlers also receive the raised
40+ # TelegramError object in error.
3941def start (bot , update ):
4042 """ Answer in Telegram """
4143 bot .sendMessage (update .message .chat_id , text = 'Hi!' )
@@ -59,13 +61,8 @@ def any_message(bot, update):
5961 update .message .text ))
6062
6163
62- def unknown_command (bot , update ):
63- """ Answer in Telegram """
64- bot .sendMessage (update .message .chat_id , text = 'Command not recognized!' )
65-
66-
6764@run_async
68- def message (bot , update , ** kwargs ):
65+ def message (bot , update ):
6966 """
7067 Example for an asynchronous handler. It's not guaranteed that replies will
7168 be in order when using @run_async. Also, you have to include **kwargs in
@@ -95,16 +92,12 @@ def cli_noncommand(bot, update, update_queue):
9592 appending it to the argument list. Be careful with this though.
9693 Here, we put the input string back into the queue, but as a command.
9794
98- To learn more about those optional handler parameters, read:
99- http://python-telegram-bot.readthedocs.org/en/latest/telegram.dispatcher.html
95+ To learn more about those optional handler parameters, read the
96+ documentation of the Handler classes.
10097 """
10198 update_queue .put ('/%s' % update )
10299
103100
104- def unknown_cli_command (bot , update ):
105- logger .warn ("Command not found: %s" % update )
106-
107-
108101def error (bot , update , error ):
109102 """ Print error to console """
110103 logger .warn ('Update %s caused error %s' % (update , error ))
@@ -119,42 +112,43 @@ def main():
119112 dp = updater .dispatcher
120113
121114 # This is how we add handlers for Telegram messages
122- dp .addTelegramCommandHandler ("start" , start )
123- dp .addTelegramCommandHandler ("help" , help )
124- dp .addUnknownTelegramCommandHandler (unknown_command )
115+ dp .addHandler (CommandHandler ("start" , start ))
116+ dp .addHandler (CommandHandler ("help" , help ))
125117 # Message handlers only receive updates that don't contain commands
126- dp .addTelegramMessageHandler (message )
127- # Regex handlers will receive all updates on which their regex matches
128- dp .addTelegramRegexHandler ('.*' , any_message )
129-
130- # String handlers work pretty much the same
131- dp .addStringCommandHandler ('reply' , cli_reply )
132- dp .addUnknownStringCommandHandler (unknown_cli_command )
133- dp .addStringRegexHandler ('[^/].*' , cli_noncommand )
118+ dp .addHandler (MessageHandler ([filters .TEXT ], message ))
119+ # Regex handlers will receive all updates on which their regex matches,
120+ # but we have to add it in a separate group, since in one group,
121+ # only one handler will be executed
122+ dp .addHandler (RegexHandler ('.*' , any_message ), group = 'log' )
123+
124+ # String handlers work pretty much the same. Note that we have to tell
125+ # the handler to pass the args or update_queue parameter
126+ dp .addHandler (StringCommandHandler ('reply' , cli_reply , pass_args = True ))
127+ dp .addHandler (StringRegexHandler ('[^/].*' , cli_noncommand ,
128+ pass_update_queue = True ))
134129
135130 # All TelegramErrors are caught for you and delivered to the error
136131 # handler(s). Other types of Errors are not caught.
137132 dp .addErrorHandler (error )
138133
139134 # Start the Bot and store the update Queue, so we can insert updates
140- update_queue = updater .start_polling (poll_interval = 0.1 , timeout = 10 )
135+ update_queue = updater .start_polling (timeout = 10 )
141136
142137 '''
143138 # Alternatively, run with webhook:
144- updater.bot.setWebhook(webhook_url='https://example.com/%s' % token,
145- certificate=open('cert.pem', 'rb'))
146139
147140 update_queue = updater.start_webhook('0.0.0.0',
148141 443,
149142 url_path=token,
150143 cert='cert.pem',
151- key='key.key')
144+ key='key.key',
145+ webhook_url='https://example.com/%s'
146+ % token)
152147
153148 # Or, if SSL is handled by a reverse proxy, the webhook URL is already set
154149 # and the reverse proxy is configured to deliver directly to port 6000:
155150
156- update_queue = updater.start_webhook('0.0.0.0',
157- 6000)
151+ update_queue = updater.start_webhook('0.0.0.0', 6000)
158152 '''
159153
160154 # Start CLI-Loop
0 commit comments