1111
1212logger = logging .getLogger ('TeleBot' )
1313formatter = logging .Formatter (
14- '%(asctime)s (%(filename)s:%(lineno)d %(threadName)s) %(levelname)s - %(name)s: "%(message)s"'
14+ '%(asctime)s (%(filename)s:%(lineno)d %(threadName)s) %(levelname)s - %(name)s: "%(message)s"'
1515)
1616
1717console_output_handler = logging .StreamHandler (sys .stderr )
@@ -67,6 +67,7 @@ def __init__(self, token, threaded=True, skip_pending=False):
6767
6868 self .message_handlers = []
6969 self .inline_handlers = []
70+ self .chosen_inline_handlers = []
7071
7172 self .threaded = threaded
7273 if self .threaded :
@@ -104,7 +105,7 @@ def __skip_updates(self):
104105 for update in updates :
105106 if update .update_id > self .last_update_id :
106107 self .last_update_id = update .update_id
107- updates = self .get_updates (offset = self .last_update_id + 1 , timeout = 1 )
108+ updates = self .get_updates (offset = self .last_update_id + 1 , timeout = 1 )
108109 return total
109110
110111 def __retrieve_updates (self , timeout = 20 ):
@@ -119,29 +120,36 @@ def __retrieve_updates(self, timeout=20):
119120 updates = self .get_updates (offset = (self .last_update_id + 1 ), timeout = timeout )
120121 new_messages = []
121122 new_inline_querys = []
123+ new_chosen_inline_results = []
122124 for update in updates :
123125 if update .update_id > self .last_update_id :
124126 self .last_update_id = update .update_id
125127 if update .message :
126128 new_messages .append (update .message )
127129 if update .inline_query :
128130 new_inline_querys .append (update .inline_query )
129- # TODO Chosen
131+ if update .chosen_inline_result :
132+ new_chosen_inline_results .append (update .chosen_inline_result )
130133 logger .debug ('Received {0} new updates' .format (len (updates )))
131134 if len (new_messages ) > 0 :
132135 self .process_new_messages (new_messages )
133136 if len (new_inline_querys ) > 0 :
134137 self .process_new_inline_query (new_inline_querys )
138+ if len (new_chosen_inline_results ) > 0 :
139+ self .process_new_chosen_inline_query (new_chosen_inline_results )
135140
136141 def process_new_messages (self , new_messages ):
137142 self ._append_pre_next_step_handler ()
138143 self .__notify_update (new_messages )
139- self ._notify_command_handlers (new_messages )
144+ self ._notify_command_handlers (self . message_handlers , new_messages )
140145 self ._notify_message_subscribers (new_messages )
141146 self ._notify_message_next_handler (new_messages )
142147
143148 def process_new_inline_query (self , new_inline_querys ):
144- self ._notify_inline_handlers (new_inline_querys )
149+ self ._notify_command_handlers (self .inline_handlers , new_inline_querys )
150+
151+ def process_new_chosen_inline_query (self , new_chosen_inline_querys ):
152+ self ._notify_command_handlers (self .chosen_inline_handlers , new_chosen_inline_querys )
145153
146154 def __notify_update (self , new_messages ):
147155 for listener in self .update_listener :
@@ -171,9 +179,9 @@ def __threaded_polling(self, none_stop=False, interval=0, timeout=3):
171179
172180 polling_thread = util .WorkerThread (name = "PollingThread" )
173181 or_event = util .OrEvent (
174- polling_thread .done_event ,
175- polling_thread .exception_event ,
176- self .worker_pool .exception_event
182+ polling_thread .done_event ,
183+ polling_thread .exception_event ,
184+ self .worker_pool .exception_event
177185 )
178186
179187 while not self .__stop_polling .wait (interval ):
@@ -282,8 +290,8 @@ def send_message(self, chat_id, text, disable_web_page_preview=None, reply_to_me
282290 :return: API reply.
283291 """
284292 return types .Message .de_json (
285- apihelper .send_message (self .token , chat_id , text , disable_web_page_preview , reply_to_message_id ,
286- reply_markup , parse_mode ))
293+ apihelper .send_message (self .token , chat_id , text , disable_web_page_preview , reply_to_message_id ,
294+ reply_markup , parse_mode ))
287295
288296 def forward_message (self , chat_id , from_chat_id , message_id ):
289297 """
@@ -306,7 +314,7 @@ def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, rep
306314 :return: API reply.
307315 """
308316 return types .Message .de_json (
309- apihelper .send_photo (self .token , chat_id , photo , caption , reply_to_message_id , reply_markup ))
317+ apihelper .send_photo (self .token , chat_id , photo , caption , reply_to_message_id , reply_markup ))
310318
311319 def send_audio (self , chat_id , audio , duration = None , performer = None , title = None , reply_to_message_id = None ,
312320 reply_markup = None ):
@@ -322,8 +330,8 @@ def send_audio(self, chat_id, audio, duration=None, performer=None, title=None,
322330 :return: Message
323331 """
324332 return types .Message .de_json (
325- apihelper .send_audio (self .token , chat_id , audio , duration , performer , title , reply_to_message_id ,
326- reply_markup ))
333+ apihelper .send_audio (self .token , chat_id , audio , duration , performer , title , reply_to_message_id ,
334+ reply_markup ))
327335
328336 def send_voice (self , chat_id , voice , duration = None , reply_to_message_id = None , reply_markup = None ):
329337 """
@@ -336,7 +344,7 @@ def send_voice(self, chat_id, voice, duration=None, reply_to_message_id=None, re
336344 :return: Message
337345 """
338346 return types .Message .de_json (
339- apihelper .send_voice (self .token , chat_id , voice , duration , reply_to_message_id , reply_markup ))
347+ apihelper .send_voice (self .token , chat_id , voice , duration , reply_to_message_id , reply_markup ))
340348
341349 def send_document (self , chat_id , data , reply_to_message_id = None , reply_markup = None ):
342350 """
@@ -348,7 +356,7 @@ def send_document(self, chat_id, data, reply_to_message_id=None, reply_markup=No
348356 :return: API reply.
349357 """
350358 return types .Message .de_json (
351- apihelper .send_data (self .token , chat_id , data , 'document' , reply_to_message_id , reply_markup ))
359+ apihelper .send_data (self .token , chat_id , data , 'document' , reply_to_message_id , reply_markup ))
352360
353361 def send_sticker (self , chat_id , data , reply_to_message_id = None , reply_markup = None ):
354362 """
@@ -360,7 +368,7 @@ def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=Non
360368 :return: API reply.
361369 """
362370 return types .Message .de_json (
363- apihelper .send_data (self .token , chat_id , data , 'sticker' , reply_to_message_id , reply_markup ))
371+ apihelper .send_data (self .token , chat_id , data , 'sticker' , reply_to_message_id , reply_markup ))
364372
365373 def send_video (self , chat_id , data , duration = None , caption = None , reply_to_message_id = None , reply_markup = None ):
366374 """
@@ -374,7 +382,7 @@ def send_video(self, chat_id, data, duration=None, caption=None, reply_to_messag
374382 :return:
375383 """
376384 return types .Message .de_json (
377- apihelper .send_video (self .token , chat_id , data , duration , caption , reply_to_message_id , reply_markup ))
385+ apihelper .send_video (self .token , chat_id , data , duration , caption , reply_to_message_id , reply_markup ))
378386
379387 def send_location (self , chat_id , latitude , longitude , reply_to_message_id = None , reply_markup = None ):
380388 """
@@ -387,7 +395,7 @@ def send_location(self, chat_id, latitude, longitude, reply_to_message_id=None,
387395 :return: API reply.
388396 """
389397 return types .Message .de_json (
390- apihelper .send_location (self .token , chat_id , latitude , longitude , reply_to_message_id , reply_markup ))
398+ apihelper .send_location (self .token , chat_id , latitude , longitude , reply_to_message_id , reply_markup ))
391399
392400 def send_chat_action (self , chat_id , action ):
393401 """
@@ -515,21 +523,26 @@ def decorator(fn):
515523
516524 return decorator
517525
518- def inline_handler (self , regexp = None , func = None ):
519-
526+ def inline_handler (self , func ):
520527 def decorator (fn ):
521528 handler_dict = {'function' : fn }
522- filters = {}
523- if regexp :
524- filters ['regexp' ] = regexp
525- if func :
526- filters ['lambda' ] = func
529+ filters = {'lambda' : func }
527530 handler_dict ['filters' ] = filters
528531 self .inline_handlers .append (handler_dict )
529532 return fn
530533
531534 return decorator
532535
536+ def chosen_inline_handler (self , func ):
537+ def decorator (fn ):
538+ handler_dict = {'function' : fn }
539+ filters = {'lambda' : func }
540+ handler_dict ['filters' ] = filters
541+ self .chosen_inline_handlers .append (handler_dict )
542+ return fn
543+
544+ return decorator
545+
533546 @staticmethod
534547 def _test_message_handler (message_handler , message ):
535548 for filter , filter_value in six .iteritems (message_handler ['filters' ]):
@@ -549,20 +562,13 @@ def _test_filter(filter, filter_value, message):
549562 return filter_value (message )
550563 return False
551564
552- def _notify_command_handlers (self , new_messages ):
565+ def _notify_command_handlers (self , handlers , new_messages ):
553566 for message in new_messages :
554- for message_handler in self . message_handlers :
567+ for message_handler in handlers :
555568 if self ._test_message_handler (message_handler , message ):
556569 self .__exec_task (message_handler ['function' ], message )
557570 break
558571
559- def _notify_inline_handlers (self , inlien_querys ):
560- for inline_query in inlien_querys :
561- for inline_handler in self .inline_handlers :
562- if self ._test_message_handler (inline_handler , inline_query ):
563- self .__exec_task (inline_handler ['function' ], inline_query )
564- break
565-
566572
567573class AsyncTeleBot (TeleBot ):
568574 def __init__ (self , * args , ** kwargs ):
0 commit comments