Skip to content

Commit 30ed6e3

Browse files
committed
Add channel_post, edited_channel_post support.
1 parent 27a79c4 commit 30ed6e3

2 files changed

Lines changed: 58 additions & 3 deletions

File tree

‎telebot/__init__.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ def __init__(self, token, threaded=True, skip_pending=False):
6767

6868
self.message_handlers = []
6969
self.edited_message_handlers = []
70+
self.channel_post_handlers = []
71+
self.edited_channel_post_handlers = []
7072
self.inline_handlers = []
7173
self.chosen_inline_handlers = []
7274
self.callback_query_handlers = []
@@ -129,6 +131,8 @@ def __retrieve_updates(self, timeout=20):
129131
def process_new_updates(self, updates):
130132
new_messages = []
131133
edited_new_messages = []
134+
new_channel_posts = []
135+
new_edited_channel_posts = []
132136
new_inline_querys = []
133137
new_chosen_inline_results = []
134138
new_callback_querys = []
@@ -139,6 +143,10 @@ def process_new_updates(self, updates):
139143
new_messages.append(update.message)
140144
if update.edited_message:
141145
edited_new_messages.append(update.edited_message)
146+
if update.channel_post:
147+
new_channel_posts.append(update.channel_post)
148+
if update.edited_channel_post:
149+
new_edited_channel_posts.append(update.edited_channel_post)
142150
if update.inline_query:
143151
new_inline_querys.append(update.inline_query)
144152
if update.chosen_inline_result:
@@ -150,6 +158,10 @@ def process_new_updates(self, updates):
150158
self.process_new_messages(new_messages)
151159
if len(edited_new_messages) > 0:
152160
self.process_new_edited_messages(edited_new_messages)
161+
if len(new_channel_posts) > 0:
162+
self.process_new_channel_posts(new_channel_posts)
163+
if len(new_edited_channel_posts) > 0:
164+
self.process_new_edited_channel_posts(new_edited_channel_posts)
153165
if len(new_inline_querys) > 0:
154166
self.process_new_inline_query(new_inline_querys)
155167
if len(new_chosen_inline_results) > 0:
@@ -167,6 +179,12 @@ def process_new_messages(self, new_messages):
167179
def process_new_edited_messages(self, edited_message):
168180
self._notify_command_handlers(self.edited_message_handlers, edited_message)
169181

182+
def process_new_channel_posts(self, channel_post):
183+
self._notify_command_handlers(self.channel_post_handlers, channel_post)
184+
185+
def process_new_edited_channel_posts(self, edited_channel_post):
186+
self._notify_command_handlers(self.edited_channel_post_handlers, edited_channel_post)
187+
170188
def process_new_inline_query(self, new_inline_querys):
171189
self._notify_command_handlers(self.inline_handlers, new_inline_querys)
172190

@@ -748,6 +766,38 @@ def decorator(handler):
748766
def add_edited_message_handler(self, handler_dict):
749767
self.edited_message_handlers.append(handler_dict)
750768

769+
def channel_post_handler(self, commands=None, regexp=None, func=None, content_types=['text'], **kwargs):
770+
def decorator(handler):
771+
handler_dict = self._build_handler_dict(handler,
772+
commands=commands,
773+
regexp=regexp,
774+
func=func,
775+
content_types=content_types,
776+
**kwargs)
777+
self.add_channel_post_handler(handler_dict)
778+
return handler
779+
780+
return decorator
781+
782+
def add_channel_post_handler(self, handler_dict):
783+
self.channel_post_handlers.append(handler_dict)
784+
785+
def edited_channel_post_handler(self, commands=None, regexp=None, func=None, content_types=['text'], **kwargs):
786+
def decorator(handler):
787+
handler_dict = self._build_handler_dict(handler,
788+
commands=commands,
789+
regexp=regexp,
790+
func=func,
791+
content_types=content_types,
792+
**kwargs)
793+
self.add_edited_message_handler(handler_dict)
794+
return handler
795+
796+
return decorator
797+
798+
def add_edited_channel_post_handler(self, handler_dict):
799+
self.edited_channel_post_handlers.append(handler_dict)
800+
751801
def inline_handler(self, func, **kwargs):
752802
def decorator(handler):
753803
handler_dict = self._build_handler_dict(handler, func=func, **kwargs)

‎telebot/types.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,17 @@ def de_json(cls, json_type):
113113
chosen_inline_result = ChosenInlineResult.de_json(obj['chosen_inline_result'])
114114
if 'callback_query' in obj:
115115
callback_query = CallbackQuery.de_json(obj['callback_query'])
116-
return cls(update_id, message, edited_message, inline_query, chosen_inline_result, callback_query)
116+
return cls(update_id, message, edited_message, channel_post, edited_channel_post, inline_query,
117+
chosen_inline_result, callback_query)
117118

118-
def __init__(self, update_id, message, edited_message, inline_query, chosen_inline_result, callback_query):
119+
def __init__(self, update_id, message, edited_message, channel_post, edited_channel_post, inline_query,
120+
chosen_inline_result, callback_query):
119121
self.update_id = update_id
120122
self.edited_message = edited_message
121123
self.message = message
122124
self.edited_message = edited_message
125+
self.channel_post = channel_post
126+
self.edited_channel_post = edited_channel_post
123127
self.inline_query = inline_query
124128
self.chosen_inline_result = chosen_inline_result
125129
self.callback_query = callback_query
@@ -191,7 +195,8 @@ def de_json(cls, json_string):
191195
all_members_are_administrators = obj.get('all_members_are_administrators')
192196
return cls(id, type, title, username, first_name, last_name, all_members_are_administrators)
193197

194-
def __init__(self, id, type, title=None, username=None, first_name=None, last_name=None, all_members_are_administrators=None):
198+
def __init__(self, id, type, title=None, username=None, first_name=None, last_name=None,
199+
all_members_are_administrators=None):
195200
self.type = type
196201
self.last_name = last_name
197202
self.first_name = first_name

0 commit comments

Comments
 (0)