Skip to content

Commit 00866f7

Browse files
authored
Define response object for broadcast api (#223)
1 parent 2a6488c commit 00866f7

File tree

5 files changed

+51
-20
lines changed

5 files changed

+51
-20
lines changed

README.rst

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ Create a new LineBotApi instance.
102102
103103
You can override the ``timeout`` value for each method.
104104

105-
reply\_message(self, reply\_token, messages, timeout=None)
106-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
105+
reply\_message(self, reply\_token, messages, notification_disabled=False, timeout=None)
106+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
107107

108108
Respond to events from users, groups, and rooms. You can get a
109109
reply\_token from a webhook event object.
@@ -114,8 +114,8 @@ https://developers.line.biz/en/reference/messaging-api/#send-reply-message
114114
115115
line_bot_api.reply_message(reply_token, TextSendMessage(text='Hello World!'))
116116
117-
push\_message(self, to, messages, timeout=None)
118-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
117+
push\_message(self, to, messages, notification_disabled=False, timeout=None)
118+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
119119

120120
Send messages to users, groups, and rooms at any time.
121121

@@ -125,17 +125,28 @@ https://developers.line.biz/en/reference/messaging-api/#send-push-message
125125
126126
line_bot_api.push_message(to, TextSendMessage(text='Hello World!'))
127127
128-
multicast(self, to, messages, timeout=None)
129-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128+
multicast(self, to, messages, notification_disabled=False, timeout=None)
129+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
130130

131-
Send messages to multiple users at any time.
131+
Send push messages to multiple users at any time. Messages cannot be sent to groups or rooms.
132132

133133
https://developers.line.biz/en/reference/messaging-api/#send-multicast-message
134134

135135
.. code:: python
136136
137137
line_bot_api.multicast(['to1', 'to2'], TextSendMessage(text='Hello World!'))
138138
139+
broadcast(self, messages, notification_disabled=False, timeout=None)
140+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
141+
142+
Send push messages to multiple users at any time.
143+
144+
https://developers.line.biz/en/reference/messaging-api/#send-broadcast-message
145+
146+
.. code:: python
147+
148+
line_bot_api.broadcast(TextSendMessage(text='Hello World!'))
149+
139150
get\_profile(self, user\_id, timeout=None)
140151
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
141152

@@ -461,7 +472,7 @@ https://developers.line.biz/en/reference/messaging-api/#revoke-channel-access-to
461472
line_bot_api.revoke_channel_token(<access_token>)
462473
463474
get\_insight\_message\_delivery(self, date, timeout=None)
464-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
475+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
465476

466477
Get the number of messages sent on a specified day.
467478

@@ -473,7 +484,7 @@ https://developers.line.biz/en/reference/messaging-api/#get-number-of-delivery-m
473484
print(insight.api_broadcast)
474485
475486
get\_insight\_followers(self, date, timeout=None)
476-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
487+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
477488

478489
Get the number of users who have added the bot on or before a specified date.
479490

@@ -485,7 +496,7 @@ https://developers.line.biz/en/reference/messaging-api/#get-number-of-followers
485496
print(insight.followers)
486497
487498
get\_insight\_demographic(self, timeout=None)
488-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
499+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
489500

490501
Retrieve the demographic attributes for a bot's friends.
491502

@@ -505,11 +516,12 @@ https://developers.line.biz/en/reference/messaging-api/#get-message-event
505516

506517
.. code:: python
507518
508-
insight = line_bot_api.get_insight_message_event()
519+
broadcast_response = line_bot_api.broadcast(TextSendMessage(text='Hello World!'))
520+
insight = line_bot_api.get_insight_message_event(broadcast_response.request_id)
509521
print(insight.overview)
510522
511523
※ Error handling
512-
^^^^^^^^^^^^^^^^
524+
^^^^^^^^^^^^^^^^^
513525

514526
If the LINE API server returns an error, LineBotApi raises LineBotApiError.
515527

linebot/api.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
MessageDeliveryBroadcastResponse, MessageDeliveryMulticastResponse,
2828
MessageDeliveryPushResponse, MessageDeliveryReplyResponse,
2929
InsightMessageDeliveryResponse, InsightFollowersResponse, InsightDemographicResponse,
30-
InsightMessageEventResponse,
30+
InsightMessageEventResponse, BroadcastResponse,
3131
)
3232

3333

@@ -145,7 +145,8 @@ def multicast(self, to, messages, notification_disabled=False, timeout=None):
145145
146146
https://developers.line.biz/en/reference/messaging-api/#send-multicast-message
147147
148-
Send messages to multiple users at any time.
148+
Sends push messages to multiple users at any time.
149+
Messages cannot be sent to groups or rooms.
149150
150151
:param to: IDs of the receivers
151152
Max: 150 users
@@ -180,7 +181,7 @@ def broadcast(self, messages, notification_disabled=False, timeout=None):
180181
181182
https://developers.line.biz/en/reference/messaging-api/#send-broadcast-message
182183
183-
Send messages to multiple users at any time.
184+
Sends push messages to multiple users at any time.
184185
185186
:param messages: Messages.
186187
Max: 5
@@ -202,10 +203,12 @@ def broadcast(self, messages, notification_disabled=False, timeout=None):
202203
'notificationDisabled': notification_disabled,
203204
}
204205

205-
self._post(
206+
response = self._post(
206207
'/v2/bot/message/broadcast', data=json.dumps(data), timeout=timeout
207208
)
208209

210+
return BroadcastResponse(request_id=response.headers.get('X-Line-Request-Id'))
211+
209212
def get_message_delivery_broadcast(self, date, timeout=None):
210213
"""Get number of sent broadcast messages.
211214
@@ -931,7 +934,6 @@ def get_insight_demographic(self, timeout=None):
931934
932935
https://developers.line.biz/en/reference/messaging-api/#get-demographic
933936
934-
:param str date: Date for which to retrieve the number of followers.
935937
:param timeout: (optional) How long to wait for the server
936938
to send data before giving up, as a float,
937939
or a (connect timeout, read timeout) float tuple.

linebot/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
InsightFollowersResponse,
122122
InsightDemographicResponse,
123123
InsightMessageEventResponse,
124+
BroadcastResponse,
124125
)
125126
from .rich_menu import ( # noqa
126127
RichMenu,

linebot/models/responses.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@
2525
from .rich_menu import RichMenuSize, RichMenuArea
2626

2727

28+
class BroadcastResponse(object):
29+
"""BroadcastResponse.
30+
31+
https://developers.line.biz/en/reference/messaging-api/#send-broadcast-message
32+
"""
33+
34+
def __init__(self, request_id=None):
35+
"""__init__ method.
36+
37+
:param str request_id: Request ID. A unique ID is generated for each request
38+
"""
39+
self.request_id = request_id
40+
41+
2842
class Profile(Base):
2943
"""Profile.
3044

tests/api/test_send_text_message.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ def test_broadcast_text_message(self):
112112
responses.add(
113113
responses.POST,
114114
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/message/broadcast',
115-
json={}, status=200
115+
json={}, status=200, headers={'X-Line-Request-Id': 'request_id_test'}
116116
)
117117

118-
self.tested.broadcast(self.text_message)
118+
response = self.tested.broadcast(self.text_message)
119119

120120
request = responses.calls[0].request
121121
self.assertEqual(
@@ -129,9 +129,10 @@ def test_broadcast_text_message(self):
129129
"messages": self.message
130130
}
131131
)
132+
self.assertEqual('request_id_test', response.request_id)
132133

133134
# call with notification_disable=True
134-
self.tested.broadcast(self.text_message, notification_disabled=True)
135+
response = self.tested.broadcast(self.text_message, notification_disabled=True)
135136

136137
request = responses.calls[1].request
137138
self.assertEqual(
@@ -145,6 +146,7 @@ def test_broadcast_text_message(self):
145146
"messages": self.message
146147
}
147148
)
149+
self.assertEqual('request_id_test', response.request_id)
148150

149151

150152
if __name__ == '__main__':

0 commit comments

Comments
 (0)