Skip to content

Commit a5d0b96

Browse files
Marcopietroalbini
authored andcommitted
Add support for receiving channel posts
1 parent 1656fa7 commit a5d0b96

File tree

8 files changed

+196
-0
lines changed

8 files changed

+196
-0
lines changed

botogram/bot.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def __init__(self, api_connection):
7171
self.register_update_processor("message", messages.process_message)
7272
self.register_update_processor("edited_message",
7373
messages.process_edited_message)
74+
self.register_update_processor("channel_post",
75+
messages.process_channel_post)
76+
self.register_update_processor("edited_channel_post",
77+
messages.process_channel_post_edited)
7478

7579
self._bot_id = str(uuid.uuid4())
7680

@@ -144,6 +148,16 @@ def message_edited(self, func):
144148
self._main_component.add_message_edited_hook(func)
145149
return func
146150

151+
def channel_post(self, func):
152+
"""Add a channel post hook"""
153+
self._main_component.add_channel_post_hook(func)
154+
return func
155+
156+
def channel_post_edited(self, func):
157+
"""Add a edited channel post hook"""
158+
self._main_component.add_channel_post_edited_hook(func)
159+
return func
160+
147161
def command(self, name, hidden=False, order=0):
148162
"""Register a new command"""
149163
def __(func):

botogram/components.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def __new__(cls, *args, **kwargs):
3232
self.__timers = []
3333
self.__chat_unavailable_hooks = []
3434
self.__messages_edited_hooks = []
35+
self.__channel_post_hooks = []
36+
self.__channel_post_edited_hooks = []
3537

3638
self._component_id = str(uuid.uuid4())
3739

@@ -158,6 +160,22 @@ def add_message_edited_hook(self, func):
158160
hook = hooks.MessageEditedHook(func, self)
159161
self.__messages_edited_hooks.append(hook)
160162

163+
def add_channel_post_hook(self, func):
164+
"""Add a channel post hook"""
165+
if not callable(func):
166+
raise ValueError("A channel post hook must be callable")
167+
168+
hook = hooks.ChannelPostHook(func, self)
169+
self.__channel_post_hooks.append(hook)
170+
171+
def add_channel_post_edited_hook(self, func):
172+
"""Add an edited channel post hook"""
173+
if not callable(func):
174+
raise ValueError("A edited channel post hook must be callable")
175+
176+
hook = hooks.EditedChannelPostHook(func, self)
177+
self.__channel_post_edited_hooks.append(hook)
178+
161179
def _add_no_commands_hook(self, func):
162180
"""Register an hook which will be executed when no commands matches"""
163181
if not callable(func):
@@ -181,6 +199,8 @@ def _get_chains(self):
181199
"tasks": [self.__timers],
182200
"chat_unavalable_hooks": [self.__chat_unavailable_hooks],
183201
"messages_edited": [self.__messages_edited_hooks],
202+
"channel_post": [self.__channel_post_hooks],
203+
"channel_post_edited": [self.__channel_post_edited_hooks]
184204
}
185205

186206
def _get_commands(self):

botogram/hooks.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,24 @@ def _call(self, bot, update):
212212
message=message)
213213

214214

215+
class ChannelPostHook(Hook):
216+
"""Underlying hook for @bot.channel_post"""
217+
218+
def _call(self, bot, update):
219+
message = update.channel_post
220+
return bot._call(self.func, self.component_id, chat=message.chat,
221+
message=message)
222+
223+
224+
class EditedChannelPostHook(Hook):
225+
"""Underlying hook for @bot.channel_post_edited"""
226+
227+
def _call(self, bot, update):
228+
message = update.edited_channel_post
229+
return bot._call(self.func, self.component_id, chat=message.chat,
230+
message=message)
231+
232+
215233
class TimerHook(Hook):
216234
"""Underlying hook for a timer"""
217235

botogram/messages.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,35 @@ def process_edited_message(bot, chains, update):
3737

3838
bot.logger.debug("No hook actually processed the #%s update." %
3939
update.update_id)
40+
41+
42+
def process_channel_post(bot, chains, update):
43+
"""Process a channel post"""
44+
for hook in chains["channel_post"]:
45+
bot.logger.debug("Processing channel post in update #%s with the "
46+
"hook %s..." % (update.update_id, hook.name))
47+
48+
result = hook.call(bot, update)
49+
if result is True:
50+
bot.logger.debug("Update %s was just processed by the %s hook." %
51+
(update.update_id, hook.name))
52+
return
53+
54+
bot.logger.debug("No hook actually processed the #%s update." %
55+
update.update_id)
56+
57+
58+
def process_channel_post_edited(bot, chains, update):
59+
"""Process an edited channel post"""
60+
for hook in chains["channel_post_edited"]:
61+
bot.logger.debug("Processing edited channel post in update #%s with"
62+
"the hook %s..." % (update.update_id, hook.name))
63+
64+
result = hook.call(bot, update)
65+
if result is True:
66+
bot.logger.debug("Update %s was just processed by the %s hook." %
67+
(update.update_id, hook.name))
68+
return
69+
70+
bot.logger.debug("No hook actually processed the #%s update." %
71+
update.update_id)

botogram/objects/updates.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Update(BaseObject):
2323
optional = {
2424
"message": Message,
2525
"edited_message": Message,
26+
"channel_post": Message,
27+
"edited_channel_post": Message
2628
}
2729
_check_equality_ = "update_id"
2830

docs/api/bot.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,51 @@ components.
194194
195195
.. versionadded:: 0.3
196196

197+
.. py:decoratormethod:: channel_post
198+
199+
Functions decorated with this decorator will receive all the messages
200+
posted to channels the bot is a member of. This allows you to act when
201+
certain messages are received, as an example.
202+
203+
You can :ref:`request the following arguments <bot-structure-hooks-args>`
204+
in the decorated functions:
205+
206+
* **chat**: the chat in which the channel post was originally sent
207+
(instance of :py:class:`~botogram.Chat`)
208+
209+
* **message**: the message (instance of :py:class:`~botogram.Message`)
210+
211+
.. code-block:: python
212+
213+
@bot.channel_post
214+
def channel_post(chat, message):
215+
message.reply("I read this post!")
216+
217+
.. versionadded:: 0.4
218+
219+
.. py:decoratormethod:: channel_post_edited
220+
221+
Functions decorated with this decorator will receive all the messages
222+
edited in channels the bot is a member of. This allows you to act when
223+
certain messages are changed, as an example.
224+
225+
You can :ref:`request the following arguments <bot-structure-hooks-args>`
226+
in the decorated functions:
227+
228+
* **chat**: the chat in which the channel post was originally sent
229+
(instance of :py:class:`~botogram.Chat`)
230+
231+
* **message**: the (new) edited message (instance of
232+
:py:class:`~botogram.Message`)
233+
234+
.. code-block:: python
235+
236+
@bot.channel_post_edited
237+
def channel_post_edited(chat, message):
238+
message.reply("This post is changed!")
239+
240+
.. versionadded:: 0.4
241+
197242
.. py:decoratormethod:: command(name, [hidden=False, order=0])
198243
199244
This decorator register a new command, and calls the decorated function

docs/api/components.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,64 @@ about how to create them in the ":ref:`custom-components`" chapter.
158158

159159
.. versionadded:: 0.3
160160

161+
.. py:method:: add_channel_post_hook(func)
162+
163+
All the functions provided to this method will receive all the messages
164+
posted to channels the bot is a member of. This allows you to act when
165+
certain messages are received, as an example.
166+
167+
You can :ref:`request the following arguments <bot-structure-hooks-args>`
168+
in the provided functions:
169+
170+
* **chat**: the chat in which the channel post was originally sent
171+
(instance of :py:class:`~botogram.Chat`)
172+
173+
* **message**: the message (instance of :py:class:`~botogram.Message`)
174+
175+
.. code-block:: python
176+
177+
class ChannelAckComponent(botogram.Component):
178+
component_name = "channel-ack"
179+
180+
def __init__(self):
181+
self.add_channel_post_hook(self.reply)
182+
183+
def reply(self, chat, message):
184+
message.reply("I read this post!")
185+
186+
:param callable func: The function you want to use.
187+
188+
.. versionadded:: 0.4
189+
190+
.. py:method:: add_channel_post_edited_hook(func)
191+
192+
All the functions provided to this method will receive all the messages
193+
edited in channels the bot is a member of. This allows you to act when
194+
certain messages are changed, as an example.
195+
196+
You can :ref:`request the following arguments <bot-structure-hooks-args>`
197+
in the provided functions:
198+
199+
* **chat**: the chat in which the channel post was originally sent
200+
(instance of :py:class:`~botogram.Chat`)
201+
202+
* **message**: the (new) edited message (instance of :py:class:`~botogram.Message`)
203+
204+
.. code-block:: python
205+
206+
class ChannelAlertComponent(botogram.Component):
207+
component_name = "channel-alert"
208+
209+
def __init__(self):
210+
self.add_channel_post_edited_hook(self.reply)
211+
212+
def reply(self, chat, message):
213+
message.reply("This post is changed!")
214+
215+
:param callable func: The function you want to use.
216+
217+
.. versionadded:: 0.4
218+
161219
.. py:method:: add_command(name, func, [hidden=False, order=0])
162220
163221
This function registers a new command, and calls the provided function

docs/changelog/0.4.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ Release description not yet written.
1919
New features
2020
------------
2121

22+
* Added support for receiving messages sent to channels
23+
24+
* New decorator :py:meth:`botogram.Bot.channel_post`
25+
* New decorator :py:meth:`botogram.Bot.channel_post_edited`
26+
* New method :py:meth:`botogram.Component.add_channel_post_hook`
27+
* New method :py:meth:`botogram.Component.add_channel_post_edited_hook`
28+
2229
* Added ability to disable the link preview in ``/help``.
2330

2431
* New parameter :py:attr:`botogram.Bot.link_preview_in_help`

0 commit comments

Comments
 (0)