From 783e8dc65bb1e46e0c6da09378bd6a3ed15f272a Mon Sep 17 00:00:00 2001 From: okue Date: Tue, 12 Nov 2019 00:31:14 +0900 Subject: [PATCH 1/9] define response object for void functions --- linebot/api.py | 19 ++++++++---- linebot/models/__init__.py | 4 +++ linebot/models/responses.py | 60 +++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/linebot/api.py b/linebot/api.py index 111d8448..20b23994 100644 --- a/linebot/api.py +++ b/linebot/api.py @@ -27,7 +27,8 @@ MessageDeliveryBroadcastResponse, MessageDeliveryMulticastResponse, MessageDeliveryPushResponse, MessageDeliveryReplyResponse, InsightMessageDeliveryResponse, InsightFollowersResponse, InsightDemographicResponse, - InsightMessageEventResponse, + InsightMessageEventResponse, ReplyMessageResponse, PushMessageResponse, + MultiCastResponse, BroadCastResponse, ) @@ -103,10 +104,12 @@ def reply_message(self, reply_token, messages, notification_disabled=False, time 'notificationDisabled': notification_disabled, } - self._post( + response = self._post( '/v2/bot/message/reply', data=json.dumps(data), timeout=timeout ) + return ReplyMessageResponse(response=response) + def push_message(self, to, messages, notification_disabled=False, timeout=None): """Call push message API. @@ -136,10 +139,12 @@ def push_message(self, to, messages, notification_disabled=False, timeout=None): 'notificationDisabled': notification_disabled, } - self._post( + response = self._post( '/v2/bot/message/push', data=json.dumps(data), timeout=timeout ) + return PushMessageResponse(response=response) + def multicast(self, to, messages, notification_disabled=False, timeout=None): """Call multicast API. @@ -171,10 +176,12 @@ def multicast(self, to, messages, notification_disabled=False, timeout=None): 'notificationDisabled': notification_disabled, } - self._post( + response = self._post( '/v2/bot/message/multicast', data=json.dumps(data), timeout=timeout ) + return MultiCastResponse(response=response) + def broadcast(self, messages, notification_disabled=False, timeout=None): """Call broadcast API. @@ -202,10 +209,12 @@ def broadcast(self, messages, notification_disabled=False, timeout=None): 'notificationDisabled': notification_disabled, } - self._post( + response = self._post( '/v2/bot/message/broadcast', data=json.dumps(data), timeout=timeout ) + return BroadCastResponse(response=response) + def get_message_delivery_broadcast(self, date, timeout=None): """Get number of sent broadcast messages. diff --git a/linebot/models/__init__.py b/linebot/models/__init__.py index 3d24f44e..d5ce92d3 100644 --- a/linebot/models/__init__.py +++ b/linebot/models/__init__.py @@ -121,6 +121,10 @@ InsightFollowersResponse, InsightDemographicResponse, InsightMessageEventResponse, + ReplyMessageResponse, + PushMessageResponse, + BroadCastResponse, + MultiCastResponse, ) from .rich_menu import ( # noqa RichMenu, diff --git a/linebot/models/responses.py b/linebot/models/responses.py index c8542026..39635e49 100644 --- a/linebot/models/responses.py +++ b/linebot/models/responses.py @@ -25,6 +25,66 @@ from .rich_menu import RichMenuSize, RichMenuArea +class ReplyMessageResponse(object): + """ReplyMessageResponse. + + https://developers.line.biz/en/reference/messaging-api/#send-reply-message + """ + + def __init__(self, response): + """__init__ method. + + :param response: HttpResponse object + :type response: T <= :py:class:`linebot.http_client.HttpResponse` + """ + self.response = response + + +class PushMessageResponse(object): + """PushMessageResponse. + + https://developers.line.biz/en/reference/messaging-api/#send-push-message + """ + + def __init__(self, response): + """__init__ method. + + :param response: HttpResponse object + :type response: T <= :py:class:`linebot.http_client.HttpResponse` + """ + self.response = response + + +class MultiCastResponse(object): + """MultiCastResponse. + + https://developers.line.biz/en/reference/messaging-api/#send-multicast-message + """ + + def __init__(self, response): + """__init__ method. + + :param response: HttpResponse object + :type response: T <= :py:class:`linebot.http_client.HttpResponse` + """ + self.response = response + + +class BroadCastResponse(object): + """BroadCastResponse. + + https://developers.line.biz/en/reference/messaging-api/#send-broadcast-message + """ + + def __init__(self, response): + """__init__ method. + + :param response: HttpResponse object + :type response: T <= :py:class:`linebot.http_client.HttpResponse` + """ + self.response = response + + class Profile(Base): """Profile. From 6db01c2dd42ef50e5f540154bb5eef8103816829 Mon Sep 17 00:00:00 2001 From: okue Date: Tue, 12 Nov 2019 14:58:06 +0900 Subject: [PATCH 2/9] define only for broadcast api --- linebot/api.py | 18 +++------- linebot/models/__init__.py | 3 -- linebot/models/responses.py | 52 ++--------------------------- tests/api/test_send_text_message.py | 5 +-- 4 files changed, 11 insertions(+), 67 deletions(-) diff --git a/linebot/api.py b/linebot/api.py index 20b23994..6cc43034 100644 --- a/linebot/api.py +++ b/linebot/api.py @@ -27,8 +27,7 @@ MessageDeliveryBroadcastResponse, MessageDeliveryMulticastResponse, MessageDeliveryPushResponse, MessageDeliveryReplyResponse, InsightMessageDeliveryResponse, InsightFollowersResponse, InsightDemographicResponse, - InsightMessageEventResponse, ReplyMessageResponse, PushMessageResponse, - MultiCastResponse, BroadCastResponse, + InsightMessageEventResponse, BroadCastResponse, ) @@ -104,12 +103,10 @@ def reply_message(self, reply_token, messages, notification_disabled=False, time 'notificationDisabled': notification_disabled, } - response = self._post( + self._post( '/v2/bot/message/reply', data=json.dumps(data), timeout=timeout ) - return ReplyMessageResponse(response=response) - def push_message(self, to, messages, notification_disabled=False, timeout=None): """Call push message API. @@ -139,12 +136,10 @@ def push_message(self, to, messages, notification_disabled=False, timeout=None): 'notificationDisabled': notification_disabled, } - response = self._post( + self._post( '/v2/bot/message/push', data=json.dumps(data), timeout=timeout ) - return PushMessageResponse(response=response) - def multicast(self, to, messages, notification_disabled=False, timeout=None): """Call multicast API. @@ -176,12 +171,10 @@ def multicast(self, to, messages, notification_disabled=False, timeout=None): 'notificationDisabled': notification_disabled, } - response = self._post( + self._post( '/v2/bot/message/multicast', data=json.dumps(data), timeout=timeout ) - return MultiCastResponse(response=response) - def broadcast(self, messages, notification_disabled=False, timeout=None): """Call broadcast API. @@ -213,7 +206,7 @@ def broadcast(self, messages, notification_disabled=False, timeout=None): '/v2/bot/message/broadcast', data=json.dumps(data), timeout=timeout ) - return BroadCastResponse(response=response) + return BroadCastResponse(request_id=response.headers.get('X-Line-Request-Id')) def get_message_delivery_broadcast(self, date, timeout=None): """Get number of sent broadcast messages. @@ -940,7 +933,6 @@ def get_insight_demographic(self, timeout=None): https://developers.line.biz/en/reference/messaging-api/#get-demographic - :param str date: Date for which to retrieve the number of followers. :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. diff --git a/linebot/models/__init__.py b/linebot/models/__init__.py index d5ce92d3..cc1018d4 100644 --- a/linebot/models/__init__.py +++ b/linebot/models/__init__.py @@ -121,10 +121,7 @@ InsightFollowersResponse, InsightDemographicResponse, InsightMessageEventResponse, - ReplyMessageResponse, - PushMessageResponse, BroadCastResponse, - MultiCastResponse, ) from .rich_menu import ( # noqa RichMenu, diff --git a/linebot/models/responses.py b/linebot/models/responses.py index 39635e49..438c5dde 100644 --- a/linebot/models/responses.py +++ b/linebot/models/responses.py @@ -25,64 +25,18 @@ from .rich_menu import RichMenuSize, RichMenuArea -class ReplyMessageResponse(object): - """ReplyMessageResponse. - - https://developers.line.biz/en/reference/messaging-api/#send-reply-message - """ - - def __init__(self, response): - """__init__ method. - - :param response: HttpResponse object - :type response: T <= :py:class:`linebot.http_client.HttpResponse` - """ - self.response = response - - -class PushMessageResponse(object): - """PushMessageResponse. - - https://developers.line.biz/en/reference/messaging-api/#send-push-message - """ - - def __init__(self, response): - """__init__ method. - - :param response: HttpResponse object - :type response: T <= :py:class:`linebot.http_client.HttpResponse` - """ - self.response = response - - -class MultiCastResponse(object): - """MultiCastResponse. - - https://developers.line.biz/en/reference/messaging-api/#send-multicast-message - """ - - def __init__(self, response): - """__init__ method. - - :param response: HttpResponse object - :type response: T <= :py:class:`linebot.http_client.HttpResponse` - """ - self.response = response - - class BroadCastResponse(object): """BroadCastResponse. https://developers.line.biz/en/reference/messaging-api/#send-broadcast-message """ - def __init__(self, response): + def __init__(self, request_id=None): """__init__ method. - :param response: HttpResponse object - :type response: T <= :py:class:`linebot.http_client.HttpResponse` + :param str request_id: Request ID. A unique ID is generated for each request """ - self.response = response + self.request_id = request_id class Profile(Base): diff --git a/tests/api/test_send_text_message.py b/tests/api/test_send_text_message.py index 1256324f..d3590b61 100644 --- a/tests/api/test_send_text_message.py +++ b/tests/api/test_send_text_message.py @@ -112,7 +112,7 @@ def test_broadcast_text_message(self): responses.add( responses.POST, LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/message/broadcast', - json={}, status=200 + json={}, status=200, headers={'X-Line-Request-Id': 'request_id_test'} ) self.tested.broadcast(self.text_message) @@ -131,7 +131,7 @@ def test_broadcast_text_message(self): ) # call with notification_disable=True - self.tested.broadcast(self.text_message, notification_disabled=True) + response = self.tested.broadcast(self.text_message, notification_disabled=True) request = responses.calls[1].request self.assertEqual( @@ -145,6 +145,7 @@ def test_broadcast_text_message(self): "messages": self.message } ) + self.assertEqual('request_id_test', response.request_id) if __name__ == '__main__': From a28235e2f5f0a2de8b881e6a90a2d4111660c417 Mon Sep 17 00:00:00 2001 From: okue Date: Tue, 12 Nov 2019 16:26:54 +0900 Subject: [PATCH 3/9] fix comments --- linebot/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/linebot/api.py b/linebot/api.py index 6cc43034..1ddaa678 100644 --- a/linebot/api.py +++ b/linebot/api.py @@ -145,7 +145,7 @@ def multicast(self, to, messages, notification_disabled=False, timeout=None): https://developers.line.biz/en/reference/messaging-api/#send-multicast-message - Send messages to multiple users at any time. + Sends push messages to multiple users at any time. Messages cannot be sent to groups or rooms. :param to: IDs of the receivers Max: 150 users @@ -180,7 +180,7 @@ def broadcast(self, messages, notification_disabled=False, timeout=None): https://developers.line.biz/en/reference/messaging-api/#send-broadcast-message - Send messages to multiple users at any time. + Sends push messages to multiple users at any time. :param messages: Messages. Max: 5 From 7d3af45a4d5163e7c15af7fd23dff15b3001ad3c Mon Sep 17 00:00:00 2001 From: okue Date: Tue, 12 Nov 2019 16:27:02 +0900 Subject: [PATCH 4/9] update readme --- README.rst | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index d10bbdce..cebc243f 100644 --- a/README.rst +++ b/README.rst @@ -102,8 +102,8 @@ Create a new LineBotApi instance. You can override the ``timeout`` value for each method. -reply\_message(self, reply\_token, messages, timeout=None) -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +reply\_message(self, reply\_token, messages, notification_disabled=False, timeout=None) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Respond to events from users, groups, and rooms. You can get a reply\_token from a webhook event object. @@ -114,8 +114,8 @@ https://developers.line.biz/en/reference/messaging-api/#send-reply-message line_bot_api.reply_message(reply_token, TextSendMessage(text='Hello World!')) -push\_message(self, to, messages, timeout=None) -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +push\_message(self, to, messages, notification_disabled=False, timeout=None) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Send messages to users, groups, and rooms at any time. @@ -125,10 +125,10 @@ https://developers.line.biz/en/reference/messaging-api/#send-push-message line_bot_api.push_message(to, TextSendMessage(text='Hello World!')) -multicast(self, to, messages, timeout=None) -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +multicast(self, to, messages, notification_disabled=False, timeout=None) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -Send messages to multiple users at any time. +Sends push messages to multiple users at any time. Messages cannot be sent to groups or rooms. https://developers.line.biz/en/reference/messaging-api/#send-multicast-message @@ -136,6 +136,17 @@ https://developers.line.biz/en/reference/messaging-api/#send-multicast-message line_bot_api.multicast(['to1', 'to2'], TextSendMessage(text='Hello World!')) +broadcast(self, messages, notification_disabled=False, timeout=None) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Sends push messages to multiple users at any time. + +https://developers.line.biz/en/reference/messaging-api/#send-broadcast-message + +.. code:: python + + line_bot_api.broadcast(TextSendMessage(text='Hello World!')) + get\_profile(self, user\_id, timeout=None) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -505,7 +516,8 @@ https://developers.line.biz/en/reference/messaging-api/#get-message-event .. code:: python - insight = line_bot_api.get_insight_message_event() + broadcast_response = line_bot_api.broadcast(TextSendMessage(text='Hello World!')) + insight = line_bot_api.get_insight_message_event(broadcast_response.request_id) print(insight.overview) ※ Error handling From ed8e646ed70d3ba338453cc72b0fdd30ff347808 Mon Sep 17 00:00:00 2001 From: okue Date: Tue, 12 Nov 2019 16:57:45 +0900 Subject: [PATCH 5/9] fix line length --- linebot/api.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/linebot/api.py b/linebot/api.py index 1ddaa678..ce714cf1 100644 --- a/linebot/api.py +++ b/linebot/api.py @@ -145,7 +145,8 @@ def multicast(self, to, messages, notification_disabled=False, timeout=None): https://developers.line.biz/en/reference/messaging-api/#send-multicast-message - Sends push messages to multiple users at any time. Messages cannot be sent to groups or rooms. + Sends push messages to multiple users at any time. + Messages cannot be sent to groups or rooms. :param to: IDs of the receivers Max: 150 users From 2c815a2a2e1db7eb7b3130dea9756ce89dd7e57f Mon Sep 17 00:00:00 2001 From: okue Date: Tue, 12 Nov 2019 17:10:45 +0900 Subject: [PATCH 6/9] rename response object --- linebot/api.py | 4 ++-- linebot/models/__init__.py | 2 +- linebot/models/responses.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/linebot/api.py b/linebot/api.py index ce714cf1..6791a130 100644 --- a/linebot/api.py +++ b/linebot/api.py @@ -27,7 +27,7 @@ MessageDeliveryBroadcastResponse, MessageDeliveryMulticastResponse, MessageDeliveryPushResponse, MessageDeliveryReplyResponse, InsightMessageDeliveryResponse, InsightFollowersResponse, InsightDemographicResponse, - InsightMessageEventResponse, BroadCastResponse, + InsightMessageEventResponse, BroadcastResponse, ) @@ -207,7 +207,7 @@ def broadcast(self, messages, notification_disabled=False, timeout=None): '/v2/bot/message/broadcast', data=json.dumps(data), timeout=timeout ) - return BroadCastResponse(request_id=response.headers.get('X-Line-Request-Id')) + return BroadcastResponse(request_id=response.headers.get('X-Line-Request-Id')) def get_message_delivery_broadcast(self, date, timeout=None): """Get number of sent broadcast messages. diff --git a/linebot/models/__init__.py b/linebot/models/__init__.py index cc1018d4..84601b10 100644 --- a/linebot/models/__init__.py +++ b/linebot/models/__init__.py @@ -121,7 +121,7 @@ InsightFollowersResponse, InsightDemographicResponse, InsightMessageEventResponse, - BroadCastResponse, + BroadcastResponse, ) from .rich_menu import ( # noqa RichMenu, diff --git a/linebot/models/responses.py b/linebot/models/responses.py index 438c5dde..7e699709 100644 --- a/linebot/models/responses.py +++ b/linebot/models/responses.py @@ -25,8 +25,8 @@ from .rich_menu import RichMenuSize, RichMenuArea -class BroadCastResponse(object): - """BroadCastResponse. +class BroadcastResponse(object): + """BroadcastResponse. https://developers.line.biz/en/reference/messaging-api/#send-broadcast-message """ From a13e1c6d8dd0cabc83ad55f6c2db7b54813d045d Mon Sep 17 00:00:00 2001 From: okue Date: Tue, 12 Nov 2019 17:11:56 +0900 Subject: [PATCH 7/9] modify readme --- README.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index cebc243f..fe097a6f 100644 --- a/README.rst +++ b/README.rst @@ -103,7 +103,7 @@ Create a new LineBotApi instance. You can override the ``timeout`` value for each method. reply\_message(self, reply\_token, messages, notification_disabled=False, timeout=None) -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Respond to events from users, groups, and rooms. You can get a reply\_token from a webhook event object. @@ -126,9 +126,9 @@ https://developers.line.biz/en/reference/messaging-api/#send-push-message line_bot_api.push_message(to, TextSendMessage(text='Hello World!')) multicast(self, to, messages, notification_disabled=False, timeout=None) -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -Sends push messages to multiple users at any time. Messages cannot be sent to groups or rooms. +Send push messages to multiple users at any time. Messages cannot be sent to groups or rooms. https://developers.line.biz/en/reference/messaging-api/#send-multicast-message @@ -139,7 +139,7 @@ https://developers.line.biz/en/reference/messaging-api/#send-multicast-message broadcast(self, messages, notification_disabled=False, timeout=None) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -Sends push messages to multiple users at any time. +Send push messages to multiple users at any time. https://developers.line.biz/en/reference/messaging-api/#send-broadcast-message @@ -472,7 +472,7 @@ https://developers.line.biz/en/reference/messaging-api/#revoke-channel-access-to line_bot_api.revoke_channel_token() get\_insight\_message\_delivery(self, date, timeout=None) -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Get the number of messages sent on a specified day. @@ -484,7 +484,7 @@ https://developers.line.biz/en/reference/messaging-api/#get-number-of-delivery-m print(insight.api_broadcast) get\_insight\_followers(self, date, timeout=None) -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Get the number of users who have added the bot on or before a specified date. @@ -496,7 +496,7 @@ https://developers.line.biz/en/reference/messaging-api/#get-number-of-followers print(insight.followers) get\_insight\_demographic(self, timeout=None) -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Retrieve the demographic attributes for a bot's friends. @@ -521,7 +521,7 @@ https://developers.line.biz/en/reference/messaging-api/#get-message-event print(insight.overview) ※ Error handling -^^^^^^^^^^^^^^^^ +^^^^^^^^^^^^^^^^^ If the LINE API server returns an error, LineBotApi raises LineBotApiError. From c5aa002ac16a5d7dbeb61b2a36c9aa18b38d587a Mon Sep 17 00:00:00 2001 From: okue Date: Wed, 13 Nov 2019 17:20:41 +0900 Subject: [PATCH 8/9] modify test --- tests/api/test_send_text_message.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/api/test_send_text_message.py b/tests/api/test_send_text_message.py index d3590b61..4703d855 100644 --- a/tests/api/test_send_text_message.py +++ b/tests/api/test_send_text_message.py @@ -129,6 +129,7 @@ def test_broadcast_text_message(self): "messages": self.message } ) + self.assertEqual('request_id_test', response.request_id) # call with notification_disable=True response = self.tested.broadcast(self.text_message, notification_disabled=True) From a11b1afed1b45480e49a8c4aa35b6fd96e5d0fb0 Mon Sep 17 00:00:00 2001 From: okue Date: Wed, 13 Nov 2019 17:23:57 +0900 Subject: [PATCH 9/9] fix test --- tests/api/test_send_text_message.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/api/test_send_text_message.py b/tests/api/test_send_text_message.py index 4703d855..005d4244 100644 --- a/tests/api/test_send_text_message.py +++ b/tests/api/test_send_text_message.py @@ -115,7 +115,7 @@ def test_broadcast_text_message(self): json={}, status=200, headers={'X-Line-Request-Id': 'request_id_test'} ) - self.tested.broadcast(self.text_message) + response = self.tested.broadcast(self.text_message) request = responses.calls[0].request self.assertEqual(