Skip to content

Commit 3c43d9e

Browse files
authored
Feature/get follow ids (#320)
* feat: get followers ids api * fix: api query parameter * test: add get follower ids api * fix lint * fix lint
1 parent fa9e927 commit 3c43d9e

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

linebot/api.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
InsightMessageEventResponse, BroadcastResponse, NarrowcastResponse,
3131
MessageProgressNarrowcastResponse, BotInfo, GetWebhookResponse, TestWebhookResponse,
3232
)
33-
from .models.responses import Group
33+
from .models.responses import Group, UserIds
3434

3535

3636
class LineBotApi(object):
@@ -1218,6 +1218,29 @@ def test_webhook_endpoint(self, webhook_endpoint=None, timeout=None):
12181218

12191219
return TestWebhookResponse.new_from_json_dict(response.json)
12201220

1221+
def get_followers_ids(self, start=None, timeout=None):
1222+
"""Get a list of users who added your LINE Official Account as a friend.
1223+
1224+
https://developers.line.biz/en/reference/messaging-api/#get-follower-ids
1225+
1226+
:param str start: Get the next array of user IDs.
1227+
:param timeout: (optional) How long to wait for the server
1228+
to send data before giving up, as a float,
1229+
or a (connect timeout, read timeout) float tuple.
1230+
Default is self.http_client.timeout
1231+
:type timeout: float | tuple(float, float)
1232+
:rtype: :py:class:`linebot.models.responses.UserIds`
1233+
"""
1234+
params = None if start is None else {'start': start}
1235+
1236+
response = self._get(
1237+
'/v2/bot/followers/ids',
1238+
params=params,
1239+
timeout=timeout
1240+
)
1241+
1242+
return UserIds.new_from_json_dict(response.json)
1243+
12211244
def _get(self, path, endpoint=None, params=None, headers=None, stream=False, timeout=None):
12221245
url = (endpoint or self.endpoint) + path
12231246

linebot/models/responses.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,26 @@ def __init__(self, success=None, timestamp=None, status_code=None,
572572
self.status_code = status_code
573573
self.reason = reason
574574
self.detail = detail
575+
576+
577+
class UserIds(Base):
578+
"""UserIds.
579+
580+
https://developers.line.biz/en/reference/messaging-api/#get-follower-ids
581+
"""
582+
583+
def __init__(self, user_ids=None, next=None, **kwargs):
584+
"""__init__ method.
585+
586+
:param user_ids: List of user IDs of users
587+
that have added the LINE Official Account as a friend.
588+
Max: 300 user IDs
589+
:type user_ids: list[str]
590+
:param str next: continuationToken.
591+
A continuation token to get the next array of user IDs.
592+
:param kwargs:
593+
"""
594+
super(UserIds, self).__init__(**kwargs)
595+
596+
self.user_ids = user_ids
597+
self.next = next

tests/api/test_get_member_ids.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,50 @@ def test_get_room_member_ids_with_start(self):
117117
self.assertEqual(member_ids_response.member_ids, ['U1', 'U2'])
118118
self.assertEqual(member_ids_response.next, 'continuationToken2')
119119

120+
@responses.activate
121+
def test_get_follower_user_ids(self):
122+
responses.add(
123+
responses.GET,
124+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/followers/ids',
125+
json={
126+
'userIds': ['U1', 'U2']
127+
},
128+
status=200
129+
)
130+
131+
member_ids_response = self.tested.get_followers_ids()
132+
133+
request = responses.calls[0].request
134+
self.assertEqual(request.method, 'GET')
135+
self.assertEqual(
136+
request.url,
137+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/followers/ids')
138+
self.assertEqual(member_ids_response.user_ids, ['U1', 'U2'])
139+
self.assertEqual(member_ids_response.next, None)
140+
141+
@responses.activate
142+
def test_get_follower_user_ids_with_start(self):
143+
responses.add(
144+
responses.GET,
145+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/followers/ids',
146+
json={
147+
'userIds': ['U1', 'U2'],
148+
'next': 'continuationToken2'
149+
},
150+
status=200
151+
)
152+
153+
member_ids_response = self.tested.get_followers_ids(start='continuationToken1')
154+
155+
request = responses.calls[0].request
156+
self.assertEqual(request.method, 'GET')
157+
self.assertEqual(
158+
request.url,
159+
LineBotApi.DEFAULT_API_ENDPOINT +
160+
'/v2/bot/followers/ids?start=continuationToken1')
161+
self.assertEqual(member_ids_response.user_ids, ['U1', 'U2'])
162+
self.assertEqual(member_ids_response.next, 'continuationToken2')
163+
120164

121165
if __name__ == '__main__':
122166
unittest.main()

0 commit comments

Comments
 (0)