Skip to content

Commit 8aed727

Browse files
committed
Ability to send messages w/o notifying the user
Implemented in: - send - send_photo - send_audio - send_voice - send_video - send_file - send_location - send_sticker - forward_to Closes: #30
1 parent ae37d8a commit 8aed727

File tree

3 files changed

+195
-57
lines changed

3 files changed

+195
-57
lines changed

botogram/objects/mixins.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __(self, *args, **kwargs):
2323
class ChatMixin:
2424
"""Add some methods for chats"""
2525

26-
def _get_call_args(self, reply_to, extra):
26+
def _get_call_args(self, reply_to, extra, notify=True):
2727
"""Get default API call arguments"""
2828
# Convert instance of Message to ids in reply_to
2929
if hasattr(reply_to, "message_id"):
@@ -41,14 +41,16 @@ def _get_call_args(self, reply_to, extra):
4141
args["reply_to_message_id"] = reply_to
4242
if extra is not None:
4343
args["reply_markup"] = extra.serialize()
44+
if not notify:
45+
args["disable_notification"] = True
4446

4547
return args
4648

4749
@_require_api
4850
def send(self, message, preview=True, reply_to=None, syntax=None,
49-
extra=None):
51+
extra=None, notify=True):
5052
"""Send a message"""
51-
args = self._get_call_args(reply_to, extra)
53+
args = self._get_call_args(reply_to, extra, notify)
5254
args["text"] = message
5355
args["disable_web_page_preview"] = not preview
5456

@@ -59,9 +61,10 @@ def send(self, message, preview=True, reply_to=None, syntax=None,
5961
self._api.call("sendMessage", args)
6062

6163
@_require_api
62-
def send_photo(self, path, caption=None, reply_to=None, extra=None):
64+
def send_photo(self, path, caption=None, reply_to=None, extra=None,
65+
notify=True):
6366
"""Send a photo"""
64-
args = self._get_call_args(reply_to, extra)
67+
args = self._get_call_args(reply_to, extra, notify)
6568
if caption is not None:
6669
args["caption"] = caption
6770

@@ -70,9 +73,9 @@ def send_photo(self, path, caption=None, reply_to=None, extra=None):
7073

7174
@_require_api
7275
def send_audio(self, path, duration=None, performer=None, title=None,
73-
reply_to=None, extra=None):
76+
reply_to=None, extra=None, notify=True):
7477
"""Send an audio track"""
75-
args = self._get_call_args(reply_to, extra)
78+
args = self._get_call_args(reply_to, extra, notify)
7679
if duration is not None:
7780
args["duration"] = duration
7881
if performer is not None:
@@ -85,9 +88,9 @@ def send_audio(self, path, duration=None, performer=None, title=None,
8588

8689
@_require_api
8790
def send_voice(self, path, duration=None, title=None, reply_to=None,
88-
extra=None):
91+
extra=None, notify=True):
8992
"""Send a voice message"""
90-
args = self._get_call_args(reply_to, extra)
93+
args = self._get_call_args(reply_to, extra, notify)
9194
if duration is not None:
9295
args["duration"] = duration
9396

@@ -96,9 +99,9 @@ def send_voice(self, path, duration=None, title=None, reply_to=None,
9699

97100
@_require_api
98101
def send_video(self, path, duration=None, caption=None, reply_to=None,
99-
extra=None):
102+
extra=None, notify=True):
100103
"""Send a video"""
101-
args = self._get_call_args(reply_to, extra)
104+
args = self._get_call_args(reply_to, extra, notify)
102105
if duration is not None:
103106
args["duration"] = duration
104107
if caption is not None:
@@ -108,26 +111,27 @@ def send_video(self, path, duration=None, caption=None, reply_to=None,
108111
self._api.call("sendVideo", args, files)
109112

110113
@_require_api
111-
def send_file(self, path, reply_to=None, extra=None):
114+
def send_file(self, path, reply_to=None, extra=None, notify=True):
112115
"""Send a generic file"""
113-
args = self._get_call_args(reply_to, extra)
116+
args = self._get_call_args(reply_to, extra, notify)
114117

115118
files = {"document": open(path, "rb")}
116119
self._api.call("sendDocument", args, files)
117120

118121
@_require_api
119-
def send_location(self, latitude, longitude, reply_to=None, extra=None):
122+
def send_location(self, latitude, longitude, reply_to=None, extra=None,
123+
notify=True):
120124
"""Send a geographic location"""
121-
args = self._get_call_args(reply_to, extra)
125+
args = self._get_call_args(reply_to, extra, notify)
122126
args["latitude"] = latitude
123127
args["longitude"] = longitude
124128

125129
self._api.call("sendLocation", args)
126130

127131
@_require_api
128-
def send_sticker(self, sticker, reply_to=None, extra=None):
132+
def send_sticker(self, sticker, reply_to=None, extra=None, notify=True):
129133
"""Send a sticker"""
130-
args = self._get_call_args(reply_to, extra)
134+
args = self._get_call_args(reply_to, extra, notify)
131135

132136
files = {"sticker": open(sticker, "rb")}
133137
self._api.call("sendSticker", args, files)
@@ -137,16 +141,19 @@ class MessageMixin:
137141
"""Add some methods for messages"""
138142

139143
@_require_api
140-
def forward_to(self, to):
144+
def forward_to(self, to, notify=True):
141145
"""Forward the message to another user"""
142146
if hasattr(to, "id"):
143147
to = to.id
144148

145-
self._api.call("forwardMessage", {
146-
"chat_id": to,
147-
"from_chat_id": self.chat.id,
148-
"message_id": self.message_id,
149-
})
149+
args = dict()
150+
args["chat_id"] = to
151+
args["from_chat_id"] = self.chat.id
152+
args["message_id"] = self.message_id
153+
if not notify:
154+
args["disable_notification"] = True
155+
156+
self._api.call("forwardMessage", args)
150157

151158
@_require_api
152159
def reply(self, *args, **kwargs):

docs/api/bot.rst

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ components.
286286

287287
:return: A frozen instance of the current bot.
288288

289-
.. py:method:: send(chat, message[, preview=True, reply_to=None, syntax=None, extra=None])
289+
.. py:method:: send(chat, message[, preview=True, reply_to=None, syntax=None, extra=None, notify=True])
290290
291291
This method sends a message to a specific chat. The chat must be
292292
identified by its ID, and Telegram applies some restrictions on the chats
@@ -306,14 +306,18 @@ components.
306306
processed by Telegram (:ref:`learn more about rich formatting
307307
<tricks-messages-syntax>`).
308308

309+
The *notify* parameter is for defining if your message should trigger
310+
a notification on the client side (yes by default).
311+
309312
:param int chat: The ID of the chat which should receive the message.
310313
:param str messgae: The message you want to send.
311314
:param bool preview: If you want to show the link preview.
312315
:param int reply_to: The ID of the message this one is replying to.
313316
:param string syntax: The name of the syntax you used for the message.
314317
:param object extra: An extra object you want to attach (see above).
318+
:param bool notify: If you want to trigger the client notification.
315319

316-
.. py:method:: send_photo(chat, path[, caption="", reply_to=None, extra=None])
320+
.. py:method:: send_photo(chat, path[, caption="", reply_to=None, extra=None, notify=True])
317321
318322
This method sends a photo to a specific chat. The chat must be identified
319323
by its ID, and Telegram applies some restrictions on the chats allowed to
@@ -328,13 +332,17 @@ components.
328332
* :py:class:`botogram.ReplyKeyboardHide`
329333
* :py:class:`botogram.ForceReply`
330334

335+
The *notify* parameter is for defining if your message should trigger
336+
a notification on the client side (yes by default).
337+
331338
:param int chat: The ID of the chat which should receive the photo.
332339
:param str path: The path to the photo you want to send.
333340
:param str caption: An optional caption for the photo.
334341
:param int reply_to: The ID of the message this one is replying to.
335342
:param object extra: An extra object you want to attach (see above).
343+
:param bool notify: If you want to trigger the client notification.
336344

337-
.. py:method:: send_audio(chat, path, [duration=None, performer=None, title=None, reply_to=None, extra=None])
345+
.. py:method:: send_audio(chat, path, [duration=None, performer=None, title=None, reply_to=None, extra=None, notify=True])
338346
339347
This method sends an audio track to a specific chat. The chat must be
340348
identified by its ID, and Telegram applies some restrictions on the chats
@@ -352,15 +360,19 @@ components.
352360
* :py:class:`botogram.ReplyKeyboardHide`
353361
* :py:class:`botogram.ForceReply`
354362

363+
The *notify* parameter is for defining if your message should trigger
364+
a notification on the client side (yes by default).
365+
355366
:param int chat: The ID of the chat which should receive the photo.
356367
:param str path: The path to the audio track
357368
:param int duration: The track duration, in seconds
358369
:param str performer: The name of the performer
359370
:param str title: The title of the track
360371
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
361372
:param object extra: An extra reply interface object to attach
373+
:param bool notify: If you want to trigger the client notification.
362374

363-
.. py:method:: send_voice(chat, path, [duration=None, reply_to=None, extra=None])
375+
.. py:method:: send_voice(chat, path, [duration=None, reply_to=None, extra=None, notify=True])
364376
365377
This method sends a voice message to a specific chat. The chat must be
366378
identified by its ID, and Telegram applies some restrictions on the chats
@@ -378,13 +390,17 @@ components.
378390
* :py:class:`botogram.ReplyKeyboardHide`
379391
* :py:class:`botogram.ForceReply`
380392

393+
The *notify* parameter is for defining if your message should trigger
394+
a notification on the client side (yes by default).
395+
381396
:param int chat: The ID of the chat which should receive the photo.
382397
:param str path: The path to the voice message
383398
:param int duration: The message duration, in seconds
384399
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
385400
:param object extra: An extra reply interface object to attach
401+
:param bool notify: If you want to trigger the client notification.
386402

387-
.. py:method:: send_video(chat, path, [duration=None, caption=None, reply_to=None, extra=None])
403+
.. py:method:: send_video(chat, path, [duration=None, caption=None, reply_to=None, extra=None, notify=True])
388404
389405
This method sends a video to a specific chat. The chat must be identified
390406
by its ID, and Telegram applies some restrictions on the chats allowed to
@@ -402,14 +418,18 @@ components.
402418
* :py:class:`botogram.ReplyKeyboardHide`
403419
* :py:class:`botogram.ForceReply`
404420

421+
The *notify* parameter is for defining if your message should trigger
422+
a notification on the client side (yes by default).
423+
405424
:param int chat: The ID of the chat which should receive the video
406425
:param str path: The path to the video
407426
:param int duration: The video duration, in seconds
408427
:param str caption The caption of the video
409428
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
410429
:param object extra: An extra reply interface object to attach
430+
:param bool notify: If you want to trigger the client notification.
411431

412-
.. py:method:: send_file(chat, path, [reply_to=None, extra=None])
432+
.. py:method:: send_file(chat, path, [reply_to=None, extra=None, notify=True])
413433
414434
This method sends a generic file to a specific chat. The chat must be
415435
identified by its ID, and Telegram applies some restrictions on the chats
@@ -426,12 +446,16 @@ components.
426446
* :py:class:`botogram.ReplyKeyboardHide`
427447
* :py:class:`botogram.ForceReply`
428448

449+
The *notify* parameter is for defining if your message should trigger
450+
a notification on the client side (yes by default).
451+
429452
:param int chat: The ID of the chat which should receive the file
430453
:param str path: The path to the file
431454
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
432455
:param object extra: An extra reply interface object to attach
456+
:param bool notify: If you want to trigger the client notification.
433457

434-
.. py:method:: send_location(chat, latitude, longitude, [reply_to=None, extra=None])
458+
.. py:method:: send_location(chat, latitude, longitude, [reply_to=None, extra=None, notify=True])
435459
436460
This method sends a geographic location to a specific chat. The chat must
437461
be identified by its ID, and Telegram applies some restrictions on the
@@ -448,13 +472,17 @@ components.
448472
* :py:class:`botogram.ReplyKeyboardHide`
449473
* :py:class:`botogram.ForceReply`
450474

475+
The *notify* parameter is for defining if your message should trigger
476+
a notification on the client side (yes by default).
477+
451478
:param int chat: The ID of the chat which should receive the location
452479
:param float latitude: The latitude of the location
453480
:param float longitude: The longitude of the location
454481
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
455482
:param object extra: An extra reply interface object to attach
483+
:param bool notify: If you want to trigger the client notification.
456484

457-
.. py:method:: send_sticker(sticker, [reply_to=None, extra=None])
485+
.. py:method:: send_sticker(sticker, [reply_to=None, extra=None, notify=True])
458486
459487
This method sends a sticker to a specific chat chat (in webp format). The
460488
chat must be identified by its ID, and Telegram applies some restrictions
@@ -471,7 +499,11 @@ components.
471499
* :py:class:`botogram.ReplyKeyboardHide`
472500
* :py:class:`botogram.ForceReply`
473501

502+
The *notify* parameter is for defining if your message should trigger
503+
a notification on the client side (yes by default).
504+
474505
:param int chat: The ID of the chat which should receive the location
475506
:param str sticker: The path to the webp-formatted sticker
476507
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
477508
:param object extra: An extra reply interface object to attach
509+
:param bool notify: If you want to trigger the client notification.

0 commit comments

Comments
 (0)