diff --git a/examples/flask-kitchensink/app.py b/examples/flask-kitchensink/app.py index 63dc012fc..bce109909 100644 --- a/examples/flask-kitchensink/app.py +++ b/examples/flask-kitchensink/app.py @@ -116,6 +116,50 @@ def handle_text_message(event): line_bot_api.reply_message( event.reply_token, TextSendMessage(text="Bot can't use profile API without user ID")) + elif text == 'quota': + quota = line_bot_api.get_message_quota() + line_bot_api.reply_message( + event.reply_token, [ + TextSendMessage(text='type: ' + quota.type), + TextSendMessage(text='value: ' + str(quota.value)) + ] + ) + elif text == 'quota_consumption': + quota_consumption = line_bot_api.get_message_quota_consumption() + line_bot_api.reply_message( + event.reply_token, [ + TextSendMessage(text='total usage: ' + str(quota_consumption.total_usage)), + ] + ) + elif text == 'push': + line_bot_api.push_message( + event.source.user_id, [ + TextSendMessage(text='PUSH!'), + ] + ) + elif text == 'multicast': + line_bot_api.multicast( + [event.source.user_id], [ + TextSendMessage(text='THIS IS A MULTICAST MESSAGE'), + ] + ) + elif text == 'broadcast': + line_bot_api.broadcast( + [ + TextSendMessage(text='THIS IS A BROADCAST MESSAGE'), + ] + ) + elif text.startswith('broadcast '): # broadcast 20190505 + date = text.split(' ')[1] + print("Getting broadcast result: " + date) + result = line_bot_api.get_message_delivery_broadcast(date) + line_bot_api.reply_message( + event.reply_token, [ + TextSendMessage(text='Number of sent broadcast messages: ' + date), + TextSendMessage(text='status: ' + str(result.status)), + TextSendMessage(text='success: ' + str(result.success)), + ] + ) elif text == 'bye': if isinstance(event.source, SourceGroup): line_bot_api.reply_message( diff --git a/linebot/api.py b/linebot/api.py index 70f7fafb1..dbafedc9f 100644 --- a/linebot/api.py +++ b/linebot/api.py @@ -22,7 +22,8 @@ from .exceptions import LineBotApiError from .http_client import HttpClient, RequestsHttpClient from .models import ( - Error, Profile, MemberIds, Content, RichMenuResponse + Error, Profile, MemberIds, Content, RichMenuResponse, MessageQuotaResponse, + MessageQuotaConsumptionResponse, MessageDeliveryBroadcastResponse ) @@ -157,6 +158,55 @@ def multicast(self, to, messages, timeout=None): '/v2/bot/message/multicast', data=json.dumps(data), timeout=timeout ) + def broadcast(self, messages, timeout=None): + """Call broadcast API. + + https://developers.line.biz/en/reference/messaging-api/#send-broadcast-message + + Send messages to multiple users at any time. + + :param messages: Messages. + Max: 5 + :type messages: T <= :py:class:`linebot.models.send_messages.SendMessage` | + list[T <= :py:class:`linebot.models.send_messages.SendMessage`] + :param timeout: (optional) How long to wait for the server + to send data before giving up, as a float, + or a (connect timeout, read timeout) float tuple. + Default is self.http_client.timeout + :type timeout: float | tuple(float, float) + """ + if not isinstance(messages, (list, tuple)): + messages = [messages] + + data = { + 'messages': [message.as_json_dict() for message in messages] + } + + self._post( + '/v2/bot/message/broadcast', data=json.dumps(data), timeout=timeout + ) + + def get_message_delivery_broadcast(self, date, timeout=None): + """Get number of sent broadcast messages. + + https://developers.line.biz/en/reference/messaging-api/#get-number-of-broadcast-messages + + Gets the number of messages sent with the /bot/message/broadcast endpoint. + + :param str date: Date the messages were sent. The format is `yyyyMMdd`(Timezone is UTC+9). + :param timeout: (optional) How long to wait for the server + to send data before giving up, as a float, + or a (connect timeout, read timeout) float tuple. + Default is self.http_client.timeout + :type timeout: float | tuple(float, float) + """ + response = self._get( + '/v2/bot/message/delivery/broadcast?date={date}'.format(date=date), + timeout=timeout + ) + + return MessageDeliveryBroadcastResponse.new_from_json_dict(response.json) + def get_profile(self, user_id, timeout=None): """Call get profile API. @@ -519,7 +569,7 @@ def get_rich_menu_list(self, timeout=None): or a (connect timeout, read timeout) float tuple. Default is self.http_client.timeout :type timeout: float | tuple(float, float) - :rtype: list(T <= :py:class:`linebot.models.reponse.RichMenuResponse`) + :rtype: list(T <= :py:class:`linebot.models.responses.RichMenuResponse`) :return: list[RichMenuResponse] instance """ response = self._get( @@ -533,6 +583,46 @@ def get_rich_menu_list(self, timeout=None): return result + def get_message_quota(self, timeout=None): + """Call Get the target limit for additional messages. + + https://developers.line.biz/en/reference/messaging-api/#get-quota + + :param timeout: (optional) How long to wait for the server + to send data before giving up, as a float, + or a (connect timeout, read timeout) float tuple. + Default is self.http_client.timeout + :type timeout: float | tuple(float, float) + :rtype: :py:class:`linebot.models.responses.MessageQuotaResponse` + :return: MessageQuotaResponse instance + """ + response = self._get( + '/v2/bot/message/quota', + timeout=timeout + ) + + return MessageQuotaResponse.new_from_json_dict(response.json) + + def get_message_quota_consumption(self, timeout=None): + """Get number of messages sent this month. + + https://developers.line.biz/en/reference/messaging-api/#get-consumption + + :param timeout: (optional) How long to wait for the server + to send data before giving up, as a float, + or a (connect timeout, read timeout) float tuple. + Default is self.http_client.timeout + :type timeout: float | tuple(float, float) + :rtype: :py:class:`linebot.models.responses.MessageQuotaConsumptionResponse` + :return: MessageQuotaConsumptionResponse instance + """ + response = self._get( + '/v2/bot/message/quota/consumption', + timeout=timeout + ) + + return MessageQuotaConsumptionResponse.new_from_json_dict(response.json) + def _get(self, path, params=None, headers=None, stream=False, timeout=None): url = self.endpoint + path diff --git a/linebot/models/__init__.py b/linebot/models/__init__.py index f8894ea3a..a9bb7b689 100644 --- a/linebot/models/__init__.py +++ b/linebot/models/__init__.py @@ -92,6 +92,9 @@ MemberIds, Content, RichMenuResponse, + MessageQuotaResponse, + MessageQuotaConsumptionResponse, + MessageDeliveryBroadcastResponse, Content as MessageContent, # backward compatibility ) from .rich_menu import ( # noqa diff --git a/linebot/models/responses.py b/linebot/models/responses.py index 8cd6e08fb..ab09af4d5 100644 --- a/linebot/models/responses.py +++ b/linebot/models/responses.py @@ -150,3 +150,60 @@ def __init__(self, rich_menu_id=None, size=None, selected=None, name=None, self.get_or_new_from_json_dict(area, RichMenuArea) ) self.areas = new_areas + + +class MessageQuotaResponse(Base): + """MessageQuotaResponse. + + https://developers.line.biz/en/reference/messaging-api/#get-quota + """ + + def __init__(self, type=None, value=None, **kwargs): + """__init__ method. + + :param str type: Quota limitation type + :param int value: The target limit for additional messages in the current month. + This property is returned when the type property has a value of limited. + :param kwargs: + """ + super(MessageQuotaResponse, self).__init__(**kwargs) + + self.type = type + self.value = value + + +class MessageQuotaConsumptionResponse(Base): + """MessageQuotaConsumptionResponse. + + https://developers.line.biz/en/reference/messaging-api/#get-consumption + """ + + def __init__(self, total_usage=None, **kwargs): + """__init__ method. + + :param str total_usage: The number of sent messages in the current month + :param kwargs: + """ + super(MessageQuotaConsumptionResponse, self).__init__(**kwargs) + + self.total_usage = total_usage + + +class MessageDeliveryBroadcastResponse(Base): + """MessageDeliveryBroadcastResponse. + + https://developers.line.biz/en/reference/messaging-api/#get-number-of-broadcast-messages + """ + + def __init__(self, status=None, success=None, **kwargs): + """__init__ method. + + :param str status: Status of the counting process. + :param int success: The number of messages sent with the Messaging API on the + date specified in date. + :param kwargs: + """ + super(MessageDeliveryBroadcastResponse, self).__init__(**kwargs) + + self.status = status + self.success = success