From 30ed6e37d3cd66e38d326f3e89eece14455f3379 Mon Sep 17 00:00:00 2001 From: eternnoir Date: Sat, 3 Dec 2016 13:28:22 +0800 Subject: [PATCH] Add channel_post, edited_channel_post support. --- telebot/__init__.py | 50 +++++++++++++++++++++++++++++++++++++++++++++ telebot/types.py | 11 +++++++--- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index afbb89b..e219b77 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -67,6 +67,8 @@ class TeleBot: self.message_handlers = [] self.edited_message_handlers = [] + self.channel_post_handlers = [] + self.edited_channel_post_handlers = [] self.inline_handlers = [] self.chosen_inline_handlers = [] self.callback_query_handlers = [] @@ -129,6 +131,8 @@ class TeleBot: def process_new_updates(self, updates): new_messages = [] edited_new_messages = [] + new_channel_posts = [] + new_edited_channel_posts = [] new_inline_querys = [] new_chosen_inline_results = [] new_callback_querys = [] @@ -139,6 +143,10 @@ class TeleBot: new_messages.append(update.message) if update.edited_message: edited_new_messages.append(update.edited_message) + if update.channel_post: + new_channel_posts.append(update.channel_post) + if update.edited_channel_post: + new_edited_channel_posts.append(update.edited_channel_post) if update.inline_query: new_inline_querys.append(update.inline_query) if update.chosen_inline_result: @@ -150,6 +158,10 @@ class TeleBot: self.process_new_messages(new_messages) if len(edited_new_messages) > 0: self.process_new_edited_messages(edited_new_messages) + if len(new_channel_posts) > 0: + self.process_new_channel_posts(new_channel_posts) + if len(new_edited_channel_posts) > 0: + self.process_new_edited_channel_posts(new_edited_channel_posts) if len(new_inline_querys) > 0: self.process_new_inline_query(new_inline_querys) if len(new_chosen_inline_results) > 0: @@ -167,6 +179,12 @@ class TeleBot: def process_new_edited_messages(self, edited_message): self._notify_command_handlers(self.edited_message_handlers, edited_message) + def process_new_channel_posts(self, channel_post): + self._notify_command_handlers(self.channel_post_handlers, channel_post) + + def process_new_edited_channel_posts(self, edited_channel_post): + self._notify_command_handlers(self.edited_channel_post_handlers, edited_channel_post) + def process_new_inline_query(self, new_inline_querys): self._notify_command_handlers(self.inline_handlers, new_inline_querys) @@ -748,6 +766,38 @@ class TeleBot: def add_edited_message_handler(self, handler_dict): self.edited_message_handlers.append(handler_dict) + def channel_post_handler(self, commands=None, regexp=None, func=None, content_types=['text'], **kwargs): + def decorator(handler): + handler_dict = self._build_handler_dict(handler, + commands=commands, + regexp=regexp, + func=func, + content_types=content_types, + **kwargs) + self.add_channel_post_handler(handler_dict) + return handler + + return decorator + + def add_channel_post_handler(self, handler_dict): + self.channel_post_handlers.append(handler_dict) + + def edited_channel_post_handler(self, commands=None, regexp=None, func=None, content_types=['text'], **kwargs): + def decorator(handler): + handler_dict = self._build_handler_dict(handler, + commands=commands, + regexp=regexp, + func=func, + content_types=content_types, + **kwargs) + self.add_edited_message_handler(handler_dict) + return handler + + return decorator + + def add_edited_channel_post_handler(self, handler_dict): + self.edited_channel_post_handlers.append(handler_dict) + def inline_handler(self, func, **kwargs): def decorator(handler): handler_dict = self._build_handler_dict(handler, func=func, **kwargs) diff --git a/telebot/types.py b/telebot/types.py index 7449ad2..42d37ec 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -113,13 +113,17 @@ class Update(JsonDeserializable): chosen_inline_result = ChosenInlineResult.de_json(obj['chosen_inline_result']) if 'callback_query' in obj: callback_query = CallbackQuery.de_json(obj['callback_query']) - return cls(update_id, message, edited_message, inline_query, chosen_inline_result, callback_query) + return cls(update_id, message, edited_message, channel_post, edited_channel_post, inline_query, + chosen_inline_result, callback_query) - def __init__(self, update_id, message, edited_message, inline_query, chosen_inline_result, callback_query): + def __init__(self, update_id, message, edited_message, channel_post, edited_channel_post, inline_query, + chosen_inline_result, callback_query): self.update_id = update_id self.edited_message = edited_message self.message = message self.edited_message = edited_message + self.channel_post = channel_post + self.edited_channel_post = edited_channel_post self.inline_query = inline_query self.chosen_inline_result = chosen_inline_result self.callback_query = callback_query @@ -191,7 +195,8 @@ class Chat(JsonDeserializable): all_members_are_administrators = obj.get('all_members_are_administrators') return cls(id, type, title, username, first_name, last_name, all_members_are_administrators) - def __init__(self, id, type, title=None, username=None, first_name=None, last_name=None, all_members_are_administrators=None): + def __init__(self, id, type, title=None, username=None, first_name=None, last_name=None, + all_members_are_administrators=None): self.type = type self.last_name = last_name self.first_name = first_name