Skip to content

Commit 1989bfb

Browse files
tokuhirombe-hase
authored andcommitted
Enhanced Messaging API features after redesign (04/18) #162 (#163)
* Enhanced Messaging API features after redesign (04/18) #162 Support following APIs. * Get the target limit for additional messages * Get number of messages sent this month * Send broadcast message * Get number of sent broadcast messages * polish styles * use user_id instead of sender_id (sender_id is deprecated)
1 parent fbf8688 commit 1989bfb

File tree

4 files changed

+196
-2
lines changed

4 files changed

+196
-2
lines changed

examples/flask-kitchensink/app.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,50 @@ def handle_text_message(event):
116116
line_bot_api.reply_message(
117117
event.reply_token,
118118
TextSendMessage(text="Bot can't use profile API without user ID"))
119+
elif text == 'quota':
120+
quota = line_bot_api.get_message_quota()
121+
line_bot_api.reply_message(
122+
event.reply_token, [
123+
TextSendMessage(text='type: ' + quota.type),
124+
TextSendMessage(text='value: ' + str(quota.value))
125+
]
126+
)
127+
elif text == 'quota_consumption':
128+
quota_consumption = line_bot_api.get_message_quota_consumption()
129+
line_bot_api.reply_message(
130+
event.reply_token, [
131+
TextSendMessage(text='total usage: ' + str(quota_consumption.total_usage)),
132+
]
133+
)
134+
elif text == 'push':
135+
line_bot_api.push_message(
136+
event.source.user_id, [
137+
TextSendMessage(text='PUSH!'),
138+
]
139+
)
140+
elif text == 'multicast':
141+
line_bot_api.multicast(
142+
[event.source.user_id], [
143+
TextSendMessage(text='THIS IS A MULTICAST MESSAGE'),
144+
]
145+
)
146+
elif text == 'broadcast':
147+
line_bot_api.broadcast(
148+
[
149+
TextSendMessage(text='THIS IS A BROADCAST MESSAGE'),
150+
]
151+
)
152+
elif text.startswith('broadcast '): # broadcast 20190505
153+
date = text.split(' ')[1]
154+
print("Getting broadcast result: " + date)
155+
result = line_bot_api.get_message_delivery_broadcast(date)
156+
line_bot_api.reply_message(
157+
event.reply_token, [
158+
TextSendMessage(text='Number of sent broadcast messages: ' + date),
159+
TextSendMessage(text='status: ' + str(result.status)),
160+
TextSendMessage(text='success: ' + str(result.success)),
161+
]
162+
)
119163
elif text == 'bye':
120164
if isinstance(event.source, SourceGroup):
121165
line_bot_api.reply_message(

linebot/api.py

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
from .exceptions import LineBotApiError
2323
from .http_client import HttpClient, RequestsHttpClient
2424
from .models import (
25-
Error, Profile, MemberIds, Content, RichMenuResponse
25+
Error, Profile, MemberIds, Content, RichMenuResponse, MessageQuotaResponse,
26+
MessageQuotaConsumptionResponse, MessageDeliveryBroadcastResponse
2627
)
2728

2829

@@ -157,6 +158,55 @@ def multicast(self, to, messages, timeout=None):
157158
'/v2/bot/message/multicast', data=json.dumps(data), timeout=timeout
158159
)
159160

161+
def broadcast(self, messages, timeout=None):
162+
"""Call broadcast API.
163+
164+
https://developers.line.biz/en/reference/messaging-api/#send-broadcast-message
165+
166+
Send messages to multiple users at any time.
167+
168+
:param messages: Messages.
169+
Max: 5
170+
:type messages: T <= :py:class:`linebot.models.send_messages.SendMessage` |
171+
list[T <= :py:class:`linebot.models.send_messages.SendMessage`]
172+
:param timeout: (optional) How long to wait for the server
173+
to send data before giving up, as a float,
174+
or a (connect timeout, read timeout) float tuple.
175+
Default is self.http_client.timeout
176+
:type timeout: float | tuple(float, float)
177+
"""
178+
if not isinstance(messages, (list, tuple)):
179+
messages = [messages]
180+
181+
data = {
182+
'messages': [message.as_json_dict() for message in messages]
183+
}
184+
185+
self._post(
186+
'/v2/bot/message/broadcast', data=json.dumps(data), timeout=timeout
187+
)
188+
189+
def get_message_delivery_broadcast(self, date, timeout=None):
190+
"""Get number of sent broadcast messages.
191+
192+
https://developers.line.biz/en/reference/messaging-api/#get-number-of-broadcast-messages
193+
194+
Gets the number of messages sent with the /bot/message/broadcast endpoint.
195+
196+
:param str date: Date the messages were sent. The format is `yyyyMMdd`(Timezone is UTC+9).
197+
:param timeout: (optional) How long to wait for the server
198+
to send data before giving up, as a float,
199+
or a (connect timeout, read timeout) float tuple.
200+
Default is self.http_client.timeout
201+
:type timeout: float | tuple(float, float)
202+
"""
203+
response = self._get(
204+
'/v2/bot/message/delivery/broadcast?date={date}'.format(date=date),
205+
timeout=timeout
206+
)
207+
208+
return MessageDeliveryBroadcastResponse.new_from_json_dict(response.json)
209+
160210
def get_profile(self, user_id, timeout=None):
161211
"""Call get profile API.
162212
@@ -519,7 +569,7 @@ def get_rich_menu_list(self, timeout=None):
519569
or a (connect timeout, read timeout) float tuple.
520570
Default is self.http_client.timeout
521571
:type timeout: float | tuple(float, float)
522-
:rtype: list(T <= :py:class:`linebot.models.reponse.RichMenuResponse`)
572+
:rtype: list(T <= :py:class:`linebot.models.responses.RichMenuResponse`)
523573
:return: list[RichMenuResponse] instance
524574
"""
525575
response = self._get(
@@ -533,6 +583,46 @@ def get_rich_menu_list(self, timeout=None):
533583

534584
return result
535585

586+
def get_message_quota(self, timeout=None):
587+
"""Call Get the target limit for additional messages.
588+
589+
https://developers.line.biz/en/reference/messaging-api/#get-quota
590+
591+
:param timeout: (optional) How long to wait for the server
592+
to send data before giving up, as a float,
593+
or a (connect timeout, read timeout) float tuple.
594+
Default is self.http_client.timeout
595+
:type timeout: float | tuple(float, float)
596+
:rtype: :py:class:`linebot.models.responses.MessageQuotaResponse`
597+
:return: MessageQuotaResponse instance
598+
"""
599+
response = self._get(
600+
'/v2/bot/message/quota',
601+
timeout=timeout
602+
)
603+
604+
return MessageQuotaResponse.new_from_json_dict(response.json)
605+
606+
def get_message_quota_consumption(self, timeout=None):
607+
"""Get number of messages sent this month.
608+
609+
https://developers.line.biz/en/reference/messaging-api/#get-consumption
610+
611+
:param timeout: (optional) How long to wait for the server
612+
to send data before giving up, as a float,
613+
or a (connect timeout, read timeout) float tuple.
614+
Default is self.http_client.timeout
615+
:type timeout: float | tuple(float, float)
616+
:rtype: :py:class:`linebot.models.responses.MessageQuotaConsumptionResponse`
617+
:return: MessageQuotaConsumptionResponse instance
618+
"""
619+
response = self._get(
620+
'/v2/bot/message/quota/consumption',
621+
timeout=timeout
622+
)
623+
624+
return MessageQuotaConsumptionResponse.new_from_json_dict(response.json)
625+
536626
def _get(self, path, params=None, headers=None, stream=False, timeout=None):
537627
url = self.endpoint + path
538628

linebot/models/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@
9292
MemberIds,
9393
Content,
9494
RichMenuResponse,
95+
MessageQuotaResponse,
96+
MessageQuotaConsumptionResponse,
97+
MessageDeliveryBroadcastResponse,
9598
Content as MessageContent, # backward compatibility
9699
)
97100
from .rich_menu import ( # noqa

linebot/models/responses.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,60 @@ def __init__(self, rich_menu_id=None, size=None, selected=None, name=None,
150150
self.get_or_new_from_json_dict(area, RichMenuArea)
151151
)
152152
self.areas = new_areas
153+
154+
155+
class MessageQuotaResponse(Base):
156+
"""MessageQuotaResponse.
157+
158+
https://developers.line.biz/en/reference/messaging-api/#get-quota
159+
"""
160+
161+
def __init__(self, type=None, value=None, **kwargs):
162+
"""__init__ method.
163+
164+
:param str type: Quota limitation type
165+
:param int value: The target limit for additional messages in the current month.
166+
This property is returned when the type property has a value of limited.
167+
:param kwargs:
168+
"""
169+
super(MessageQuotaResponse, self).__init__(**kwargs)
170+
171+
self.type = type
172+
self.value = value
173+
174+
175+
class MessageQuotaConsumptionResponse(Base):
176+
"""MessageQuotaConsumptionResponse.
177+
178+
https://developers.line.biz/en/reference/messaging-api/#get-consumption
179+
"""
180+
181+
def __init__(self, total_usage=None, **kwargs):
182+
"""__init__ method.
183+
184+
:param str total_usage: The number of sent messages in the current month
185+
:param kwargs:
186+
"""
187+
super(MessageQuotaConsumptionResponse, self).__init__(**kwargs)
188+
189+
self.total_usage = total_usage
190+
191+
192+
class MessageDeliveryBroadcastResponse(Base):
193+
"""MessageDeliveryBroadcastResponse.
194+
195+
https://developers.line.biz/en/reference/messaging-api/#get-number-of-broadcast-messages
196+
"""
197+
198+
def __init__(self, status=None, success=None, **kwargs):
199+
"""__init__ method.
200+
201+
:param str status: Status of the counting process.
202+
:param int success: The number of messages sent with the Messaging API on the
203+
date specified in date.
204+
:param kwargs:
205+
"""
206+
super(MessageDeliveryBroadcastResponse, self).__init__(**kwargs)
207+
208+
self.status = status
209+
self.success = success

0 commit comments

Comments
 (0)