Skip to content

Commit 616b306

Browse files
authored
Impl delivery/{push, multicast, reply}, and add tests (#184)
1 parent 5ce4d6d commit 616b306

File tree

4 files changed

+230
-6
lines changed

4 files changed

+230
-6
lines changed

linebot/api.py

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
from .http_client import HttpClient, RequestsHttpClient
2424
from .models import (
2525
Error, Profile, MemberIds, Content, RichMenuResponse, MessageQuotaResponse,
26-
MessageQuotaConsumptionResponse, MessageDeliveryBroadcastResponse, IssueLinkTokenResponse,
27-
IssueChannelTokenResponse,
26+
MessageQuotaConsumptionResponse, IssueLinkTokenResponse, IssueChannelTokenResponse,
27+
MessageDeliveryBroadcastResponse, MessageDeliveryMulticastResponse,
28+
MessageDeliveryPushResponse, MessageDeliveryReplyResponse,
2829
)
2930

3031

@@ -220,6 +221,69 @@ def get_message_delivery_broadcast(self, date, timeout=None):
220221

221222
return MessageDeliveryBroadcastResponse.new_from_json_dict(response.json)
222223

224+
def get_message_delivery_reply(self, date, timeout=None):
225+
"""Get number of sent reply messages.
226+
227+
https://developers.line.biz/en/reference/messaging-api/#get-number-of-reply-messages
228+
229+
Gets the number of messages sent with the /bot/message/reply endpoint.
230+
231+
:param str date: Date the messages were sent. The format is `yyyyMMdd` (Timezone is UTC+9).
232+
:param timeout: (optional) How long to wait for the server
233+
to send data before giving up, as a float,
234+
or a (connect timeout, read timeout) float tuple.
235+
Default is self.http_client.timeout
236+
:type timeout: float | tuple(float, float)
237+
"""
238+
response = self._get(
239+
'/v2/bot/message/delivery/reply?date={date}'.format(date=date),
240+
timeout=timeout
241+
)
242+
243+
return MessageDeliveryReplyResponse.new_from_json_dict(response.json)
244+
245+
def get_message_delivery_push(self, date, timeout=None):
246+
"""Get number of sent push messages.
247+
248+
https://developers.line.biz/en/reference/messaging-api/#get-number-of-push-messages
249+
250+
Gets the number of messages sent with the /bot/message/push endpoint.
251+
252+
:param str date: Date the messages were sent. The format is `yyyyMMdd` (Timezone is UTC+9).
253+
:param timeout: (optional) How long to wait for the server
254+
to send data before giving up, as a float,
255+
or a (connect timeout, read timeout) float tuple.
256+
Default is self.http_client.timeout
257+
:type timeout: float | tuple(float, float)
258+
"""
259+
response = self._get(
260+
'/v2/bot/message/delivery/push?date={date}'.format(date=date),
261+
timeout=timeout
262+
)
263+
264+
return MessageDeliveryPushResponse.new_from_json_dict(response.json)
265+
266+
def get_message_delivery_multicast(self, date, timeout=None):
267+
"""Get number of sent multicast messages.
268+
269+
https://developers.line.biz/en/reference/messaging-api/#get-number-of-multicast-messages
270+
271+
Gets the number of messages sent with the /bot/message/multicast endpoint.
272+
273+
:param str date: Date the messages were sent. The format is `yyyyMMdd` (Timezone is UTC+9).
274+
:param timeout: (optional) How long to wait for the server
275+
to send data before giving up, as a float,
276+
or a (connect timeout, read timeout) float tuple.
277+
Default is self.http_client.timeout
278+
:type timeout: float | tuple(float, float)
279+
"""
280+
response = self._get(
281+
'/v2/bot/message/delivery/multicast?date={date}'.format(date=date),
282+
timeout=timeout
283+
)
284+
285+
return MessageDeliveryMulticastResponse.new_from_json_dict(response.json)
286+
223287
def get_profile(self, user_id, timeout=None):
224288
"""Call get profile API.
225289

linebot/models/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
MessageQuotaResponse,
100100
MessageQuotaConsumptionResponse,
101101
MessageDeliveryBroadcastResponse,
102+
MessageDeliveryReplyResponse,
103+
MessageDeliveryMulticastResponse,
104+
MessageDeliveryPushResponse,
102105
Content as MessageContent, # backward compatibility,
103106
IssueLinkTokenResponse,
104107
IssueChannelTokenResponse,

linebot/models/responses.py

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,7 @@ def __init__(self, total_usage=None, **kwargs):
190190

191191

192192
class MessageDeliveryBroadcastResponse(Base):
193-
"""MessageDeliveryBroadcastResponse.
194-
195-
https://developers.line.biz/en/reference/messaging-api/#get-number-of-broadcast-messages
196-
"""
193+
"""MessageDeliveryBroadcastResponse."""
197194

198195
def __init__(self, status=None, success=None, **kwargs):
199196
"""__init__ method.
@@ -209,6 +206,57 @@ def __init__(self, status=None, success=None, **kwargs):
209206
self.success = success
210207

211208

209+
class MessageDeliveryReplyResponse(Base):
210+
"""MessageDeliveryReplyResponse."""
211+
212+
def __init__(self, status=None, success=None, **kwargs):
213+
"""__init__ method.
214+
215+
:param str status: Status of the counting process.
216+
:param int success: The number of messages sent with the Messaging API on the
217+
date specified in date.
218+
:param kwargs:
219+
"""
220+
super(MessageDeliveryReplyResponse, self).__init__(**kwargs)
221+
222+
self.status = status
223+
self.success = success
224+
225+
226+
class MessageDeliveryPushResponse(Base):
227+
"""MessageDeliveryPushResponse."""
228+
229+
def __init__(self, status=None, success=None, **kwargs):
230+
"""__init__ method.
231+
232+
:param str status: Status of the counting process.
233+
:param int success: The number of messages sent with the Messaging API on the
234+
date specified in date.
235+
:param kwargs:
236+
"""
237+
super(MessageDeliveryPushResponse, self).__init__(**kwargs)
238+
239+
self.status = status
240+
self.success = success
241+
242+
243+
class MessageDeliveryMulticastResponse(Base):
244+
"""MessageDeliveryMulticastResponse."""
245+
246+
def __init__(self, status=None, success=None, **kwargs):
247+
"""__init__ method.
248+
249+
:param str status: Status of the counting process.
250+
:param int success: The number of messages sent with the Messaging API on the
251+
date specified in date.
252+
:param kwargs:
253+
"""
254+
super(MessageDeliveryMulticastResponse, self).__init__(**kwargs)
255+
256+
self.status = status
257+
self.success = success
258+
259+
212260
class IssueLinkTokenResponse(Base):
213261
"""IssueLinkTokenResponse.
214262

tests/api/test_get_delivery.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
from __future__ import unicode_literals, absolute_import
16+
17+
import unittest
18+
19+
import responses
20+
21+
from linebot import (
22+
LineBotApi
23+
)
24+
25+
26+
class TestLineBotApi(unittest.TestCase):
27+
def setUp(self):
28+
self.tested = LineBotApi('channel_secret')
29+
self.date = '20190101'
30+
31+
@responses.activate
32+
def test_get_delivery_broadcast(self):
33+
responses.add(
34+
responses.GET,
35+
LineBotApi.DEFAULT_API_ENDPOINT +
36+
'/v2/bot/message/delivery/broadcast?date={}'.format(self.date),
37+
json={
38+
'status': 'ready',
39+
'success': 10000
40+
},
41+
status=200
42+
)
43+
44+
res = self.tested.get_message_delivery_broadcast(self.date)
45+
request = responses.calls[0].request
46+
self.assertEqual('GET', request.method)
47+
self.assertEqual('ready', res.status)
48+
self.assertEqual(10000, res.success)
49+
50+
@responses.activate
51+
def test_get_delivery_push(self):
52+
responses.add(
53+
responses.GET,
54+
LineBotApi.DEFAULT_API_ENDPOINT +
55+
'/v2/bot/message/delivery/push?date={}'.format(self.date),
56+
json={
57+
'status': 'ready',
58+
'success': 10000
59+
},
60+
status=200
61+
)
62+
63+
res = self.tested.get_message_delivery_push(self.date)
64+
request = responses.calls[0].request
65+
self.assertEqual('GET', request.method)
66+
self.assertEqual('ready', res.status)
67+
self.assertEqual(10000, res.success)
68+
69+
@responses.activate
70+
def test_get_delivery_reply(self):
71+
responses.add(
72+
responses.GET,
73+
LineBotApi.DEFAULT_API_ENDPOINT +
74+
'/v2/bot/message/delivery/reply?date={}'.format(self.date),
75+
json={
76+
'status': 'ready',
77+
'success': 10000
78+
},
79+
status=200
80+
)
81+
82+
res = self.tested.get_message_delivery_reply(self.date)
83+
request = responses.calls[0].request
84+
self.assertEqual('GET', request.method)
85+
self.assertEqual('ready', res.status)
86+
self.assertEqual(10000, res.success)
87+
88+
@responses.activate
89+
def test_get_delivery_multicast(self):
90+
responses.add(
91+
responses.GET,
92+
LineBotApi.DEFAULT_API_ENDPOINT +
93+
'/v2/bot/message/delivery/multicast?date={}'.format(self.date),
94+
json={
95+
'status': 'ready',
96+
'success': 10000
97+
},
98+
status=200
99+
)
100+
101+
res = self.tested.get_message_delivery_multicast(self.date)
102+
request = responses.calls[0].request
103+
self.assertEqual('GET', request.method)
104+
self.assertEqual('ready', res.status)
105+
self.assertEqual(10000, res.success)
106+
107+
108+
if __name__ == '__main__':
109+
unittest.main()

0 commit comments

Comments
 (0)