1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Handler done.

This commit is contained in:
eternnoir 2016-01-05 13:18:32 +08:00
parent c214f8000e
commit c706a7aba3

View File

@ -67,6 +67,7 @@ class TeleBot:
self.message_handlers = []
self.inline_handlers = []
self.chosen_inline_handlers = []
self.threaded = threaded
if self.threaded:
@ -104,7 +105,7 @@ class TeleBot:
for update in updates:
if update.update_id > self.last_update_id:
self.last_update_id = update.update_id
updates = self.get_updates(offset=self.last_update_id+1, timeout=1)
updates = self.get_updates(offset=self.last_update_id + 1, timeout=1)
return total
def __retrieve_updates(self, timeout=20):
@ -119,6 +120,7 @@ class TeleBot:
updates = self.get_updates(offset=(self.last_update_id + 1), timeout=timeout)
new_messages = []
new_inline_querys = []
new_chosen_inline_results = []
for update in updates:
if update.update_id > self.last_update_id:
self.last_update_id = update.update_id
@ -126,22 +128,28 @@ class TeleBot:
new_messages.append(update.message)
if update.inline_query:
new_inline_querys.append(update.inline_query)
# TODO Chosen
if update.chosen_inline_result:
new_chosen_inline_results.append(update.chosen_inline_result)
logger.debug('Received {0} new updates'.format(len(updates)))
if len(new_messages) > 0:
self.process_new_messages(new_messages)
if len(new_inline_querys) > 0:
self.process_new_inline_query(new_inline_querys)
if len(new_chosen_inline_results) > 0:
self.process_new_chosen_inline_query(new_chosen_inline_results)
def process_new_messages(self, new_messages):
self._append_pre_next_step_handler()
self.__notify_update(new_messages)
self._notify_command_handlers(new_messages)
self._notify_command_handlers(self.message_handlers, new_messages)
self._notify_message_subscribers(new_messages)
self._notify_message_next_handler(new_messages)
def process_new_inline_query(self, new_inline_querys):
self._notify_inline_handlers(new_inline_querys)
self._notify_command_handlers(self.inline_handlers, new_inline_querys)
def process_new_chosen_inline_query(self, new_chosen_inline_querys):
self._notify_command_handlers(self.chosen_inline_handlers, new_chosen_inline_querys)
def __notify_update(self, new_messages):
for listener in self.update_listener:
@ -515,21 +523,26 @@ class TeleBot:
return decorator
def inline_handler(self, regexp=None, func=None):
def inline_handler(self, func):
def decorator(fn):
handler_dict = {'function': fn}
filters = {}
if regexp:
filters['regexp'] = regexp
if func:
filters['lambda'] = func
filters = {'lambda': func}
handler_dict['filters'] = filters
self.inline_handlers.append(handler_dict)
return fn
return decorator
def chosen_inline_handler(self, func):
def decorator(fn):
handler_dict = {'function': fn}
filters = {'lambda': func}
handler_dict['filters'] = filters
self.chosen_inline_handlers.append(handler_dict)
return fn
return decorator
@staticmethod
def _test_message_handler(message_handler, message):
for filter, filter_value in six.iteritems(message_handler['filters']):
@ -549,20 +562,13 @@ class TeleBot:
return filter_value(message)
return False
def _notify_command_handlers(self, new_messages):
def _notify_command_handlers(self, handlers, new_messages):
for message in new_messages:
for message_handler in self.message_handlers:
for message_handler in handlers:
if self._test_message_handler(message_handler, message):
self.__exec_task(message_handler['function'], message)
break
def _notify_inline_handlers(self, inlien_querys):
for inline_query in inlien_querys:
for inline_handler in self.inline_handlers:
if self._test_message_handler(inline_handler, inline_query):
self.__exec_task(inline_handler['function'], inline_query)
break
class AsyncTeleBot(TeleBot):
def __init__(self, *args, **kwargs):