From 28417d18afacf267571f83b997ddbccd46cfba4a Mon Sep 17 00:00:00 2001 From: Artem Kolesnikov Date: Sat, 20 Feb 2016 15:58:16 +0500 Subject: [PATCH] Added ability to set message handler, inline handler, chosen inline handler with method calling --- telebot/__init__.py | 72 ++++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 3eaaf85..1d6d812 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -524,41 +524,65 @@ class TeleBot: :param content_types: This commands' supported content types. Must be a list. Defaults to ['text']. """ - def decorator(fn): - handler_dict = {'function': fn} - filters = {'content_types': content_types} - if regexp: - filters['regexp'] = regexp - if func: - filters['lambda'] = func - if commands: - filters['commands'] = commands - handler_dict['filters'] = filters - self.message_handlers.append(handler_dict) - return fn + def decorator(handler): + self.add_message_handler(handler, commands, regexp, func, content_types) + return handler return decorator + def add_message_handler(self, handler, commands=None, regexp=None, func=None, content_types=None): + if content_types is None: + content_types = ['text'] + + filters = {'content_types': content_types} + if regexp: + filters['regexp'] = regexp + if func: + filters['lambda'] = func + if commands: + filters['commands'] = commands + + handler_dict = { + 'function': handler, + 'filters': filters + } + + self.message_handlers.append(handler_dict) + def inline_handler(self, func): - def decorator(fn): - handler_dict = {'function': fn} - filters = {'lambda': func} - handler_dict['filters'] = filters - self.inline_handlers.append(handler_dict) - return fn + def decorator(handler): + self.add_inline_handler(handler, func) + return handler return decorator + def add_inline_handler(self, handler, func): + filters = {'lambda': func} + + handler_dict = { + 'function': handler, + 'filters': filters + } + + self.inline_handlers.append(handler_dict) + 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 + def decorator(handler): + self.add_chosen_inline_handler(handler, func) + return handler return decorator + def add_chosen_inline_handler(self, handler, func): + filters = {'lambda': func} + + handler_dict = { + 'function': handler, + 'filters': filters + } + + self.chosen_inline_handlers.append(handler_dict) + @staticmethod def _test_message_handler(message_handler, message): for filter, filter_value in six.iteritems(message_handler['filters']):