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

Add callback query handler.

This commit is contained in:
eternnoir 2016-04-16 14:18:19 +08:00
parent 96e06c248b
commit 1e320a5b0d
2 changed files with 32 additions and 2 deletions

View File

@ -68,6 +68,7 @@ class TeleBot:
self.message_handlers = []
self.inline_handlers = []
self.chosen_inline_handlers = []
self.callback_query_handlers = []
self.threaded = threaded
if self.threaded:
@ -124,6 +125,7 @@ class TeleBot:
new_messages = []
new_inline_querys = []
new_chosen_inline_results = []
new_callback_querys = []
for update in updates:
if update.update_id > self.last_update_id:
self.last_update_id = update.update_id
@ -133,6 +135,8 @@ class TeleBot:
new_inline_querys.append(update.inline_query)
if update.chosen_inline_result:
new_chosen_inline_results.append(update.chosen_inline_result)
if update.callback_query:
new_callback_querys.append(update.callback_query)
logger.debug('Received {0} new updates'.format(len(updates)))
if len(new_messages) > 0:
self.process_new_messages(new_messages)
@ -140,6 +144,8 @@ class TeleBot:
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)
if len(new_callback_querys) > 0:
self.process_new_callback_query(new_callback_querys)
def process_new_messages(self, new_messages):
self._append_pre_next_step_handler()
@ -154,6 +160,9 @@ class TeleBot:
def process_new_chosen_inline_query(self, new_chosen_inline_querys):
self._notify_command_handlers(self.chosen_inline_handlers, new_chosen_inline_querys)
def process_new_callback_query(self, new_callback_querys):
self._notify_command_handlers(self.callback_query_handlers, new_callback_querys)
def __notify_update(self, new_messages):
for listener in self.update_listener:
self.__exec_task(listener, new_messages)
@ -652,6 +661,23 @@ class TeleBot:
self.chosen_inline_handlers.append(handler_dict)
def callback_query_handler(self, func):
def decorator(handler):
self.add_callback_query_handler(handler, func)
return decorator
def add_callback_query_handler(self, handler, func):
filters = {'lambda': func}
handler_dict = {
'function': handler,
'filters': filters
}
self.callback_query_handlers.append(handler_dict)
@staticmethod
def _test_message_handler(message_handler, message):
for filter, filter_value in six.iteritems(message_handler['filters']):

View File

@ -86,19 +86,23 @@ class Update(JsonDeserializable):
message = None
inline_query = None
chosen_inline_result = None
callback_query = None
if 'message' in obj:
message = Message.de_json(obj['message'])
if 'inline_query' in obj:
inline_query = InlineQuery.de_json(obj['inline_query'])
if 'chosen_inline_result' in obj:
chosen_inline_result = ChosenInlineResult.de_json(obj['chosen_inline_result'])
return cls(update_id, message, inline_query, chosen_inline_result)
if 'callback_query' in obj:
callback_query = CallbackQuery.de_json(obj['callback_query'])
return cls(update_id, message, inline_query, chosen_inline_result, callback_query)
def __init__(self, update_id, message, inline_query, chosen_inline_result):
def __init__(self, update_id, message, inline_query, chosen_inline_result, callback_query):
self.update_id = update_id
self.message = message
self.inline_query = inline_query
self.chosen_inline_result = chosen_inline_result
self.callback_query = callback_query
class User(JsonDeserializable):