Skip to content

Commit 2192bd7

Browse files
clsungbe-hase
authored andcommitted
Add notificationDisabled to send message APIs (#164)
- refer to #162 - also add test to broadcast
1 parent 590c04b commit 2192bd7

10 files changed

+93
-10
lines changed

linebot/api.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(self, channel_access_token, endpoint=DEFAULT_API_ENDPOINT,
5858
else:
5959
self.http_client = RequestsHttpClient(timeout=timeout)
6060

61-
def reply_message(self, reply_token, messages, timeout=None):
61+
def reply_message(self, reply_token, messages, notification_disabled=False, timeout=None):
6262
"""Call reply message API.
6363
6464
https://devdocs.line.me/en/#reply-message
@@ -78,6 +78,8 @@ def reply_message(self, reply_token, messages, timeout=None):
7878
Max: 5
7979
:type messages: T <= :py:class:`linebot.models.send_messages.SendMessage` |
8080
list[T <= :py:class:`linebot.models.send_messages.SendMessage`]
81+
:param bool notification_disabled: (optional) True to disable push notification
82+
when the message is sent. The default value is False.
8183
:param timeout: (optional) How long to wait for the server
8284
to send data before giving up, as a float,
8385
or a (connect timeout, read timeout) float tuple.
@@ -89,14 +91,15 @@ def reply_message(self, reply_token, messages, timeout=None):
8991

9092
data = {
9193
'replyToken': reply_token,
92-
'messages': [message.as_json_dict() for message in messages]
94+
'messages': [message.as_json_dict() for message in messages],
95+
'notificationDisabled': notification_disabled,
9396
}
9497

9598
self._post(
9699
'/v2/bot/message/reply', data=json.dumps(data), timeout=timeout
97100
)
98101

99-
def push_message(self, to, messages, timeout=None):
102+
def push_message(self, to, messages, notification_disabled=False, timeout=None):
100103
"""Call push message API.
101104
102105
https://devdocs.line.me/en/#push-message
@@ -108,6 +111,8 @@ def push_message(self, to, messages, timeout=None):
108111
Max: 5
109112
:type messages: T <= :py:class:`linebot.models.send_messages.SendMessage` |
110113
list[T <= :py:class:`linebot.models.send_messages.SendMessage`]
114+
:param bool notification_disabled: (optional) True to disable push notification
115+
when the message is sent. The default value is False.
111116
:param timeout: (optional) How long to wait for the server
112117
to send data before giving up, as a float,
113118
or a (connect timeout, read timeout) float tuple.
@@ -119,14 +124,15 @@ def push_message(self, to, messages, timeout=None):
119124

120125
data = {
121126
'to': to,
122-
'messages': [message.as_json_dict() for message in messages]
127+
'messages': [message.as_json_dict() for message in messages],
128+
'notificationDisabled': notification_disabled,
123129
}
124130

125131
self._post(
126132
'/v2/bot/message/push', data=json.dumps(data), timeout=timeout
127133
)
128134

129-
def multicast(self, to, messages, timeout=None):
135+
def multicast(self, to, messages, notification_disabled=False, timeout=None):
130136
"""Call multicast API.
131137
132138
https://devdocs.line.me/en/#multicast
@@ -140,6 +146,8 @@ def multicast(self, to, messages, timeout=None):
140146
Max: 5
141147
:type messages: T <= :py:class:`linebot.models.send_messages.SendMessage` |
142148
list[T <= :py:class:`linebot.models.send_messages.SendMessage`]
149+
:param bool notification_disabled: (optional) True to disable push notification
150+
when the message is sent. The default value is False.
143151
:param timeout: (optional) How long to wait for the server
144152
to send data before giving up, as a float,
145153
or a (connect timeout, read timeout) float tuple.
@@ -151,14 +159,15 @@ def multicast(self, to, messages, timeout=None):
151159

152160
data = {
153161
'to': to,
154-
'messages': [message.as_json_dict() for message in messages]
162+
'messages': [message.as_json_dict() for message in messages],
163+
'notificationDisabled': notification_disabled,
155164
}
156165

157166
self._post(
158167
'/v2/bot/message/multicast', data=json.dumps(data), timeout=timeout
159168
)
160169

161-
def broadcast(self, messages, timeout=None):
170+
def broadcast(self, messages, notification_disabled=False, timeout=None):
162171
"""Call broadcast API.
163172
164173
https://developers.line.biz/en/reference/messaging-api/#send-broadcast-message
@@ -169,6 +178,8 @@ def broadcast(self, messages, timeout=None):
169178
Max: 5
170179
:type messages: T <= :py:class:`linebot.models.send_messages.SendMessage` |
171180
list[T <= :py:class:`linebot.models.send_messages.SendMessage`]
181+
:param bool notification_disabled: (optional) True to disable push notification
182+
when the message is sent. The default value is False.
172183
:param timeout: (optional) How long to wait for the server
173184
to send data before giving up, as a float,
174185
or a (connect timeout, read timeout) float tuple.
@@ -179,7 +190,8 @@ def broadcast(self, messages, timeout=None):
179190
messages = [messages]
180191

181192
data = {
182-
'messages': [message.as_json_dict() for message in messages]
193+
'messages': [message.as_json_dict() for message in messages],
194+
'notificationDisabled': notification_disabled,
183195
}
184196

185197
self._post(

tests/api/test_send_audio_message.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def test_push_audio_message(self):
6262
json.loads(request.body),
6363
{
6464
"to": "to",
65+
'notificationDisabled': False,
6566
"messages": self.message
6667
}
6768
)
@@ -85,6 +86,7 @@ def test_reply_audio_message(self):
8586
json.loads(request.body),
8687
{
8788
"replyToken": "replyToken",
89+
'notificationDisabled': False,
8890
"messages": self.message
8991
}
9092
)
@@ -108,6 +110,7 @@ def test_multicast_audio_message(self):
108110
json.loads(request.body),
109111
{
110112
"to": ['to1', 'to2'],
113+
'notificationDisabled': False,
111114
"messages": self.message
112115
}
113116
)

tests/api/test_send_image_message.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def test_push_image_message(self):
6161
json.loads(request.body),
6262
{
6363
"to": "to",
64+
'notificationDisabled': False,
6465
"messages": self.message
6566
}
6667
)
@@ -84,6 +85,7 @@ def test_reply_image_message(self):
8485
json.loads(request.body),
8586
{
8687
"replyToken": "replyToken",
88+
'notificationDisabled': False,
8789
"messages": self.message
8890
}
8991
)
@@ -107,6 +109,7 @@ def test_multicast_image_message(self):
107109
json.loads(request.body),
108110
{
109111
"to": ['to1', 'to2'],
112+
'notificationDisabled': False,
110113
"messages": self.message
111114
}
112115
)

tests/api/test_send_imagemap_message.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def test_push_imagemap_message(self):
129129
json.loads(request.body),
130130
{
131131
"to": "to",
132+
'notificationDisabled': False,
132133
"messages": self.message
133134
}
134135
)
@@ -152,6 +153,7 @@ def test_reply_imagemap_message(self):
152153
json.loads(request.body),
153154
{
154155
"replyToken": "replyToken",
156+
'notificationDisabled': False,
155157
"messages": self.message
156158
}
157159
)
@@ -175,6 +177,7 @@ def test_multicast_imagemap_message(self):
175177
json.loads(request.body),
176178
{
177179
"to": ['to1', 'to2'],
180+
'notificationDisabled': False,
178181
"messages": self.message
179182
}
180183
)

tests/api/test_send_location_message.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def test_push_location_message(self):
6565
json.loads(request.body),
6666
{
6767
"to": "to",
68+
'notificationDisabled': False,
6869
"messages": self.message
6970
}
7071
)
@@ -88,6 +89,7 @@ def test_reply_location_message(self):
8889
json.loads(request.body),
8990
{
9091
"replyToken": "replyToken",
92+
'notificationDisabled': False,
9193
"messages": self.message
9294
}
9395
)
@@ -111,6 +113,7 @@ def test_multicast_location_message(self):
111113
json.loads(request.body),
112114
{
113115
"to": ['to1', 'to2'],
116+
'notificationDisabled': False,
114117
"messages": self.message
115118
}
116119
)

tests/api/test_send_sticker_message.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def test_push_sticker_message(self):
6060
json.loads(request.body),
6161
{
6262
"to": "to",
63+
'notificationDisabled': False,
6364
"messages": self.message
6465
}
6566
)
@@ -83,6 +84,7 @@ def test_reply_sticker_message(self):
8384
json.loads(request.body),
8485
{
8586
"replyToken": "replyToken",
87+
'notificationDisabled': False,
8688
"messages": self.message
8789
}
8890
)
@@ -106,6 +108,7 @@ def test_multicast_sticker_message(self):
106108
json.loads(request.body),
107109
{
108110
"to": ['to1', 'to2'],
111+
'notificationDisabled': False,
109112
"messages": self.message
110113
}
111114
)

tests/api/test_send_template_message.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ def test_push_buttons_template_message(self):
380380
json.loads(request.body),
381381
{
382382
"to": "to",
383+
'notificationDisabled': False,
383384
"messages": self.button_message
384385
}
385386
)
@@ -403,6 +404,7 @@ def test_reply_buttons_template_message(self):
403404
json.loads(request.body),
404405
{
405406
"replyToken": "replyToken",
407+
'notificationDisabled': False,
406408
"messages": self.button_message
407409
}
408410
)
@@ -426,6 +428,7 @@ def test_push_confirm_template_message(self):
426428
json.loads(request.body),
427429
{
428430
"to": "to",
431+
'notificationDisabled': False,
429432
"messages": self.confirm_message
430433
}
431434
)
@@ -450,6 +453,7 @@ def test_reply_confirm_template_message(self):
450453
json.loads(request.body),
451454
{
452455
"replyToken": "replyToken",
456+
'notificationDisabled': False,
453457
"messages": self.confirm_message
454458
}
455459
)
@@ -473,6 +477,7 @@ def test_push_carousel_template_message(self):
473477
json.loads(request.body),
474478
{
475479
"to": "to",
480+
'notificationDisabled': False,
476481
"messages": self.carousel_message
477482
}
478483
)
@@ -496,6 +501,7 @@ def test_reply_carousel_template_message(self):
496501
json.loads(request.body),
497502
{
498503
"replyToken": "replyToken",
504+
'notificationDisabled': False,
499505
"messages": self.carousel_message
500506
}
501507
)
@@ -519,6 +525,7 @@ def test_multicast_carousel_template_message(self):
519525
json.loads(request.body),
520526
{
521527
"to": ['to1', 'to2'],
528+
'notificationDisabled': False,
522529
"messages": self.carousel_message
523530
}
524531
)
@@ -542,6 +549,7 @@ def test_push_image_carousel_template_message(self):
542549
json.loads(request.body),
543550
{
544551
"to": "to",
552+
'notificationDisabled': False,
545553
"messages": self.image_carousel_message
546554
}
547555
)
@@ -565,6 +573,7 @@ def test_reply_image_carousel_template_message(self):
565573
json.loads(request.body),
566574
{
567575
"replyToken": "replyToken",
576+
'notificationDisabled': False,
568577
"messages": self.image_carousel_message
569578
}
570579
)
@@ -588,6 +597,7 @@ def test_multicast_image_carousel_template_message(self):
588597
json.loads(request.body),
589598
{
590599
"to": ['to1', 'to2'],
600+
'notificationDisabled': False,
591601
"messages": self.image_carousel_message
592602
}
593603
)

tests/api/test_send_text_message.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def test_push_text_message(self):
5454
json.loads(request.body),
5555
{
5656
"to": "to",
57+
'notificationDisabled': False,
5758
"messages": self.message
5859
}
5960
)
@@ -77,6 +78,7 @@ def test_reply_text_message(self):
7778
json.loads(request.body),
7879
{
7980
"replyToken": "replyToken",
81+
'notificationDisabled': False,
8082
"messages": self.message
8183
}
8284
)
@@ -100,6 +102,46 @@ def test_multicast_text_message(self):
100102
json.loads(request.body),
101103
{
102104
"to": ['to1', 'to2'],
105+
'notificationDisabled': False,
106+
"messages": self.message
107+
}
108+
)
109+
110+
@responses.activate
111+
def test_broadcast_text_message(self):
112+
responses.add(
113+
responses.POST,
114+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/message/broadcast',
115+
json={}, status=200
116+
)
117+
118+
self.tested.broadcast(self.text_message)
119+
120+
request = responses.calls[0].request
121+
self.assertEqual(
122+
request.url,
123+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/message/broadcast')
124+
self.assertEqual(request.method, 'POST')
125+
self.assertEqual(
126+
json.loads(request.body),
127+
{
128+
'notificationDisabled': False,
129+
"messages": self.message
130+
}
131+
)
132+
133+
# call with notification_disable=True
134+
self.tested.broadcast(self.text_message, notification_disabled=True)
135+
136+
request = responses.calls[1].request
137+
self.assertEqual(
138+
request.url,
139+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/message/broadcast')
140+
self.assertEqual(request.method, 'POST')
141+
self.assertEqual(
142+
json.loads(request.body),
143+
{
144+
'notificationDisabled': True,
103145
"messages": self.message
104146
}
105147
)

tests/api/test_send_text_message_with_quick_reply.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def test_push_text_message_with_quick_reply(self):
8181
json.loads(request.body),
8282
{
8383
"to": "to",
84+
'notificationDisabled': False,
8485
"messages": [
8586
{
8687
"type": "text",

0 commit comments

Comments
 (0)