Skip to content

Commit 908c2fe

Browse files
authored
Support statistics per aggregation unit (#373)
* Assign a unit name to any aggregation unit when sending messages * Get statistics per unit * Get number of units used this month * Get name list of units used this month * Update async_api.py * modify sample based on lint error and warning
1 parent 00b4ae5 commit 908c2fe

9 files changed

+851
-21
lines changed

README.rst

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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, notification_disabled=False, timeout=None)
118-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
117+
push\_message(self, to, messages, notification_disabled=False, custom_aggregation_units=None, timeout=None)
118+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
119119

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

@@ -125,8 +125,8 @@ 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, notification_disabled=False, timeout=None)
129-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128+
multicast(self, to, messages, notification_disabled=False, custom_aggregation_units=None, timeout=None)
129+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
130130

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

@@ -590,8 +590,45 @@ https://developers.line.biz/en/reference/messaging-api/#get-message-event
590590
broadcast_response = line_bot_api.broadcast(TextSendMessage(text='Hello World!'))
591591
insight = line_bot_api.get_insight_message_event(broadcast_response.request_id)
592592
print(insight.overview)
593-
594-
get\_bot_info(self, timeout=None)
593+
594+
get\_statistics\_per\_unit(self, custom_aggregation_unit, from_date, to_date, timeout=None)
595+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
596+
597+
Return statistics about how users interact with push and multicast messages.
598+
599+
https://developers.line.biz/en/reference/partner-docs/#get-statistics-per-unit
600+
601+
.. code:: python
602+
603+
unit_name = 'promotion_a'
604+
line_bot_api.push_message('to', TextSendMessage(text='Hello World!'), custom_aggregation_units=unit_name)
605+
insight = line_bot_api.get_statistics_per_unit(unit_name, '20210301', '20210331')
606+
print(insight.overview)
607+
608+
get\_number\_of\_units\_used\_this\_month(self, timeout=None)
609+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
610+
611+
Return the number of aggregation units used this month.
612+
613+
https://developers.line.biz/en/reference/partner-docs/#get-number-of-units-used-this-month
614+
615+
.. code:: python
616+
usage = line_bot_api.get_number_of_units_used_this_month()
617+
print(usage.num_of_custom_aggregation_units)
618+
619+
get\_name\_list\_of\_units\_used\_this\_month(self, limit=100, start=None, timeout=None)
620+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
621+
622+
Return the name list of units used this month for statistics aggregation.
623+
624+
https://developers.line.biz/en/reference/partner-docs/#get-name-list-of-units-used-this-month
625+
626+
.. code:: python
627+
name_list = line_bot_api.get_name_list_of_units_used_this_month()
628+
print(name_list.custom_aggregation_units)
629+
print(name_list.next)
630+
631+
get\_bot\_info(self, timeout=None)
595632
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
596633

597634
Get bot's basic information.

linebot/api.py

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
)
3333
from .models.responses import (
3434
Group, UserIds, RichMenuAliasResponse, RichMenuAliasListResponse, ChannelAccessTokens,
35-
IssueChannelTokenResponseV2, VerifyChannelTokenResponseV2, ValidAccessTokenKeyIDsResponse
35+
IssueChannelTokenResponseV2, VerifyChannelTokenResponseV2, ValidAccessTokenKeyIDsResponse,
36+
InsightMessageEventOfCustomAggregationUnitResponse, AggregationInfoResponse,
37+
AggregationNameListResponse
3638
)
3739

3840

@@ -114,7 +116,8 @@ def reply_message(self, reply_token, messages, notification_disabled=False, time
114116

115117
def push_message(
116118
self, to, messages,
117-
retry_key=None, notification_disabled=False, timeout=None):
119+
retry_key=None, notification_disabled=False,
120+
custom_aggregation_units=None, timeout=None):
118121
"""Call push message API.
119122
120123
https://developers.line.biz/en/reference/messaging-api/#send-push-message
@@ -129,6 +132,11 @@ def push_message(
129132
:param retry_key: (optional) Arbitrarily generated UUID in hexadecimal notation.
130133
:param bool notification_disabled: (optional) True to disable push notification
131134
when the message is sent. The default value is False.
135+
:param custom_aggregation_units: (optional) Name of aggregation unit. Case-sensitive.
136+
Max unit: 1
137+
Max aggregation unit name length: 30 characters
138+
Supported character types: Half-width alphanumeric characters and underscore
139+
:type custom_aggregation_units: str | list[str]
132140
:param timeout: (optional) How long to wait for the server
133141
to send data before giving up, as a float,
134142
or a (connect timeout, read timeout) float tuple.
@@ -147,11 +155,17 @@ def push_message(
147155
'notificationDisabled': notification_disabled,
148156
}
149157

158+
if custom_aggregation_units is not None:
159+
if not isinstance(custom_aggregation_units, (list, tuple)):
160+
custom_aggregation_units = [custom_aggregation_units]
161+
data['customAggregationUnits'] = custom_aggregation_units
162+
150163
self._post(
151164
'/v2/bot/message/push', data=json.dumps(data), timeout=timeout
152165
)
153166

154-
def multicast(self, to, messages, retry_key=None, notification_disabled=False, timeout=None):
167+
def multicast(self, to, messages, retry_key=None, notification_disabled=False,
168+
custom_aggregation_units=None, timeout=None):
155169
"""Call multicast API.
156170
157171
https://developers.line.biz/en/reference/messaging-api/#send-multicast-message
@@ -169,6 +183,11 @@ def multicast(self, to, messages, retry_key=None, notification_disabled=False, t
169183
:param retry_key: (optional) Arbitrarily generated UUID in hexadecimal notation.
170184
:param bool notification_disabled: (optional) True to disable push notification
171185
when the message is sent. The default value is False.
186+
:param custom_aggregation_units: (optional) Name of aggregation unit. Case-sensitive.
187+
Max unit: 1
188+
Max aggregation unit name length: 30 characters
189+
Supported character types: Half-width alphanumeric characters and underscore
190+
:type custom_aggregation_units: str | list[str]
172191
:param timeout: (optional) How long to wait for the server
173192
to send data before giving up, as a float,
174193
or a (connect timeout, read timeout) float tuple.
@@ -187,6 +206,11 @@ def multicast(self, to, messages, retry_key=None, notification_disabled=False, t
187206
'notificationDisabled': notification_disabled,
188207
}
189208

209+
if custom_aggregation_units is not None:
210+
if not isinstance(custom_aggregation_units, (list, tuple)):
211+
custom_aggregation_units = [custom_aggregation_units]
212+
data['customAggregationUnits'] = custom_aggregation_units
213+
190214
self._post(
191215
'/v2/bot/message/multicast', data=json.dumps(data), timeout=timeout
192216
)
@@ -1718,6 +1742,75 @@ def get_channel_token_key_ids_v2_1(
17181742
timeout=timeout)
17191743
return ValidAccessTokenKeyIDsResponse.new_from_json_dict(response.json)
17201744

1745+
def get_statistics_per_unit(self, custom_aggregation_unit, from_date, to_date, timeout=None):
1746+
"""Return statistics about how users interact with push and multicast messages.
1747+
1748+
https://developers.line.biz/en/reference/partner-docs/#get-statistics-per-unit
1749+
1750+
:param str custom_aggregation_unit: Name of aggregation unit specified when sending
1751+
the message like `push_message(...)` and `multicast(...)`.
1752+
:param str from_date: Start date of aggregation period.
1753+
The format is `yyyyMMdd` (Timezone is UTC+9).
1754+
:param str to_date: End date of aggregation period.
1755+
The end date can be specified for up to 30 days later.
1756+
The format is `yyyyMMdd` (Timezone is UTC+9).
1757+
:param timeout: (optional) How long to wait for the server
1758+
to send data before giving up, as a float,
1759+
or a (connect timeout, read timeout) float tuple.
1760+
Default is self.http_client.timeout
1761+
:type timeout: float | tuple(float, float)
1762+
:rtype: :py:class:
1763+
`linebot.models.responses.InsightMessageEventOfCustomAggregationUnitResponse`
1764+
"""
1765+
response = self._get(
1766+
'/v2/bot/insight/message/event/aggregation?'
1767+
'customAggregationUnit={custom_aggregation_unit}&from={from_date}&to={to_date}'.format(
1768+
custom_aggregation_unit=custom_aggregation_unit,
1769+
from_date=from_date, to_date=to_date),
1770+
timeout=timeout
1771+
)
1772+
1773+
return InsightMessageEventOfCustomAggregationUnitResponse.new_from_json_dict(response.json)
1774+
1775+
def get_number_of_units_used_this_month(self, timeout=None):
1776+
"""Return the number of aggregation units used this month.
1777+
1778+
https://developers.line.biz/en/reference/partner-docs/#get-number-of-units-used-this-month
1779+
1780+
:param timeout: (optional) How long to wait for the server
1781+
to send data before giving up, as a float,
1782+
or a (connect timeout, read timeout) float tuple.
1783+
Default is self.http_client.timeout
1784+
:type timeout: float | tuple(float, float)
1785+
:rtype: :py:class: `linebot.models.responses.AggregationInfoResponse`
1786+
"""
1787+
response = self._get('/v2/bot/message/aggregation/info', timeout=timeout)
1788+
return AggregationInfoResponse.new_from_json_dict(response.json)
1789+
1790+
def get_name_list_of_units_used_this_month(self, limit=100, start=None, timeout=None):
1791+
"""Return the name list of units used this month for statistics aggregation.
1792+
1793+
:param int limit: Maximum number of aggregation units you can get per request.
1794+
If you don't specify a value, or if you specify a value greater than or equal to 100,
1795+
the maximum is 100.
1796+
:param str start: Get the next array of name list of units
1797+
:param timeout: (optional) How long to wait for the server
1798+
to send data before giving up, as a float,
1799+
or a (connect timeout, read timeout) float tuple.
1800+
Default is self.http_client.timeout
1801+
:type timeout: float | tuple(float, float)
1802+
:rtype: :py:class: `linebot.models.responses.AggregationNameListResponse`
1803+
"""
1804+
params = {'limit': limit} if start is None else {'limit': limit, 'start': start}
1805+
1806+
response = self._get(
1807+
'/v2/bot/message/aggregation/list',
1808+
params=params,
1809+
timeout=timeout
1810+
)
1811+
1812+
return AggregationNameListResponse.new_from_json_dict(response.json)
1813+
17211814
def _get(self, path, endpoint=None, params=None, headers=None, stream=False, timeout=None):
17221815
url = (endpoint or self.endpoint) + path
17231816

0 commit comments

Comments
 (0)