Repository: line/line-bot-sdk-python
Pull request #320 (2021-02-22 10:10:21)

Feature/get follow ids
Closed: #319 


Patch:
diff --git a/linebot/api.py b/linebot/api.py
index 9565c61..8bc5df0 100644
--- a/linebot/api.py
+++ b/linebot/api.py
@@ -30,7 +30,7 @@ from .models import (
     InsightMessageEventResponse, BroadcastResponse, NarrowcastResponse,
     MessageProgressNarrowcastResponse, BotInfo, GetWebhookResponse, TestWebhookResponse,
 )
-from .models.responses import Group
+from .models.responses import Group, UserIds
 
 
 class LineBotApi(object):
@@ -1218,6 +1218,29 @@ class LineBotApi(object):
 
         return TestWebhookResponse.new_from_json_dict(response.json)
 
+    def get_followers_ids(self, start=None, timeout=None):
+        """Get a list of users who added your LINE Official Account as a friend.
+
+        https://developers.line.biz/en/reference/messaging-api/#get-follower-ids
+
+        :param str start: Get the next array of user IDs.
+        :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.UserIds`
+        """
+        params = None if start is None else {'start': start}
+
+        response = self._get(
+            '/v2/bot/followers/ids',
+            params=params,
+            timeout=timeout
+        )
+
+        return UserIds.new_from_json_dict(response.json)
+
     def _get(self, path, endpoint=None, params=None, headers=None, stream=False, timeout=None):
         url = (endpoint or self.endpoint) + path
 
diff --git a/linebot/models/responses.py b/linebot/models/responses.py
index f6d0d4c..7593daa 100644
--- a/linebot/models/responses.py
+++ b/linebot/models/responses.py
@@ -572,3 +572,26 @@ class TestWebhookResponse(Base):
         self.status_code = status_code
         self.reason = reason
         self.detail = detail
+
+
+class UserIds(Base):
+    """UserIds.
+
+    https://developers.line.biz/en/reference/messaging-api/#get-follower-ids
+    """
+
+    def __init__(self, user_ids=None, next=None, **kwargs):
+        """__init__ method.
+
+        :param user_ids: List of user IDs of users
+            that have added the LINE Official Account as a friend.
+            Max: 300 user IDs
+        :type user_ids: list[str]
+        :param str next: continuationToken.
+            A continuation token to get the next array of user IDs.
+        :param kwargs:
+        """
+        super(UserIds, self).__init__(**kwargs)
+
+        self.user_ids = user_ids
+        self.next = next


Test patch:
diff --git a/tests/api/test_get_member_ids.py b/tests/api/test_get_member_ids.py
index 938cd99..e0d2654 100644
--- a/tests/api/test_get_member_ids.py
+++ b/tests/api/test_get_member_ids.py
@@ -117,6 +117,50 @@ class TestLineBotApi(unittest.TestCase):
         self.assertEqual(member_ids_response.member_ids, ['U1', 'U2'])
         self.assertEqual(member_ids_response.next, 'continuationToken2')
 
+    @responses.activate
+    def test_get_follower_user_ids(self):
+        responses.add(
+            responses.GET,
+            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/followers/ids',
+            json={
+                'userIds': ['U1', 'U2']
+            },
+            status=200
+        )
+
+        member_ids_response = self.tested.get_followers_ids()
+
+        request = responses.calls[0].request
+        self.assertEqual(request.method, 'GET')
+        self.assertEqual(
+            request.url,
+            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/followers/ids')
+        self.assertEqual(member_ids_response.user_ids, ['U1', 'U2'])
+        self.assertEqual(member_ids_response.next, None)
+
+    @responses.activate
+    def test_get_follower_user_ids_with_start(self):
+        responses.add(
+            responses.GET,
+            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/followers/ids',
+            json={
+                'userIds': ['U1', 'U2'],
+                'next': 'continuationToken2'
+            },
+            status=200
+        )
+
+        member_ids_response = self.tested.get_followers_ids(start='continuationToken1')
+
+        request = responses.calls[0].request
+        self.assertEqual(request.method, 'GET')
+        self.assertEqual(
+            request.url,
+            LineBotApi.DEFAULT_API_ENDPOINT +
+            '/v2/bot/followers/ids?start=continuationToken1')
+        self.assertEqual(member_ids_response.user_ids, ['U1', 'U2'])
+        self.assertEqual(member_ids_response.next, 'continuationToken2')
+
 
 if __name__ == '__main__':
     unittest.main()