Skip to content

Commit 995dab2

Browse files
authored
Support get group summary, count and room count API (#272)
Closed #271
1 parent 2ff8fe1 commit 995dab2

File tree

5 files changed

+254
-0
lines changed

5 files changed

+254
-0
lines changed

README.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ https://developers.line.biz/en/reference/messaging-api/#get-profile
196196
print(profile.picture_url)
197197
print(profile.status_message)
198198
199+
get\_group\_summary(self, group\_id, timeout=None)
200+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
201+
Gets the group ID, group name, and group icon URL of a group
202+
where the LINE Official Account is a member.
203+
204+
https://developers.line.biz/en/reference/messaging-api/#get-group-summary
205+
206+
.. code:: python
207+
208+
summary = line_bot_api.get_group_summary(group_id)
209+
print(summary.group_id)
210+
print(summary.group_name)
211+
print(summary.picture_url)
212+
199213
get\_group\_member\_profile(self, group\_id, user\_id, timeout=None)
200214
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
201215

@@ -259,6 +273,30 @@ https://developers.line.biz/en/reference/messaging-api/#get-room-member-user-ids
259273
print(member_ids_res.member_ids)
260274
print(member_ids_res.next)
261275
276+
get\_group\_members\_count(self, group\_id, timeout=None)
277+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
278+
279+
Gets the count of members in a group.
280+
281+
https://developers.line.biz/en/reference/messaging-api/#get-members-group-count
282+
283+
.. code:: python
284+
285+
group_count = line_bot_api.get_group_members_count(group_id)
286+
print(group_count)
287+
288+
get\_room\_members\_count(self, room\_id, timeout=None)
289+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
290+
291+
Gets the count of members in a room.
292+
293+
https://developers.line.biz/en/reference/messaging-api/#get-members-room-count
294+
295+
.. code:: python
296+
297+
room_count = line_bot_api.get_room_members_count(room_id)
298+
print(room_count)
299+
262300
get\_message\_content(self, message\_id, timeout=None)
263301
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
264302

linebot/api.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
InsightMessageEventResponse, BroadcastResponse, NarrowcastResponse,
3131
MessageProgressNarrowcastResponse,
3232
)
33+
from .models.responses import Group
3334

3435

3536
class LineBotApi(object):
@@ -386,6 +387,76 @@ def get_profile(self, user_id, timeout=None):
386387

387388
return Profile.new_from_json_dict(response.json)
388389

390+
def get_group_summary(self, group_id, timeout=None):
391+
"""Call get group summary API.
392+
393+
https://developers.line.biz/en/reference/messaging-api/#get-group-summary
394+
395+
Gets the group ID, group name, and group icon URL of a group
396+
where the LINE Official Account is a member.
397+
398+
:param str group_id: Group ID
399+
:param timeout: (optional) How long to wait for the server
400+
to send data before giving up, as a float,
401+
or a (connect timeout, read timeout) float tuple.
402+
Default is self.http_client.timeout
403+
:type timeout: float | tuple(float, float)
404+
:rtype: :py:class:`linebot.models.responses.Group`
405+
:return: Profile instance
406+
"""
407+
response = self._get(
408+
'/v2/bot/group/{group_id}/summary'.format(group_id=group_id),
409+
timeout=timeout
410+
)
411+
412+
return Group.new_from_json_dict(response.json)
413+
414+
def get_group_members_count(self, group_id, timeout=None):
415+
"""Call get members in group count API.
416+
417+
https://developers.line.biz/en/reference/messaging-api/#get-members-group-count
418+
419+
Gets the count of members in a group.
420+
421+
:param str group_id: Group ID
422+
:param timeout: (optional) How long to wait for the server
423+
to send data before giving up, as a float,
424+
or a (connect timeout, read timeout) float tuple.
425+
Default is self.http_client.timeout
426+
:type timeout: float | tuple(float, float)
427+
:rtype: :py:class:`linebot.models.responses.Group`
428+
:return: Profile instance
429+
"""
430+
response = self._get(
431+
'/v2/bot/group/{group_id}/members/count'.format(group_id=group_id),
432+
timeout=timeout
433+
)
434+
435+
return response.json.get('count')
436+
437+
def get_room_members_count(self, room_id, timeout=None):
438+
"""Call get members in room count API.
439+
440+
https://developers.line.biz/en/reference/messaging-api/#get-members-room-count
441+
442+
Gets the count of members in a room.
443+
444+
:param str room_id: Room ID
445+
:param timeout: (optional) How long to wait for the server
446+
to send data before giving up, as a float,
447+
or a (connect timeout, read timeout) float tuple.
448+
Default is self.http_client.timeout
449+
:type timeout: float | tuple(float, float)
450+
:rtype: :py:class:`linebot.models.responses.Group`
451+
:return: Profile instance
452+
"""
453+
response = self._get(
454+
'/v2/bot/room/{room_id}/members/count'.format(room_id=room_id),
455+
timeout=timeout
456+
)
457+
458+
return response.json.get('count')
459+
389460
def get_group_member_profile(self, group_id, user_id, timeout=None):
390461
"""Call get group member profile API.
391462

linebot/models/responses.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,27 @@ def __init__(self, display_name=None, user_id=None, picture_url=None,
6565
self.language = language
6666

6767

68+
class Group(Base):
69+
"""Group.
70+
71+
https://developers.line.biz/en/reference/messaging-api/#get-group-id-response
72+
"""
73+
74+
def __init__(self, group_id=None, group_name=None, picture_url=None, **kwargs):
75+
"""__init__ method.
76+
77+
:param str group_id
78+
:param str group_name
79+
:param str picture_url
80+
:param kwargs:
81+
"""
82+
super(Group, self).__init__(**kwargs)
83+
84+
self.group_id = group_id
85+
self.group_name = group_name
86+
self.picture_url = picture_url
87+
88+
6889
class MemberIds(Base):
6990
"""MemberIds.
7091

tests/api/test_get_group.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
from __future__ import unicode_literals, absolute_import
16+
17+
import unittest
18+
19+
import responses
20+
21+
from linebot import (
22+
LineBotApi
23+
)
24+
25+
26+
class TestLineBotApi(unittest.TestCase):
27+
def setUp(self):
28+
self.tested = LineBotApi('channel_secret')
29+
30+
@responses.activate
31+
def test_get_group_summary(self):
32+
responses.add(
33+
responses.GET,
34+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/group/group_id/summary',
35+
json={
36+
"groupId": "Ca56f94637c...",
37+
"groupName": "Group name",
38+
"pictureUrl": "https://example.com/abcdefghijklmn"
39+
},
40+
status=200
41+
)
42+
43+
group = self.tested.get_group_summary('group_id')
44+
45+
request = responses.calls[0].request
46+
self.assertEqual(request.method, 'GET')
47+
self.assertEqual(
48+
request.url,
49+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/group/group_id/summary')
50+
self.assertEqual(group.group_id, 'Ca56f94637c...')
51+
self.assertEqual(group.group_name, 'Group name')
52+
self.assertEqual(group.picture_url, 'https://example.com/abcdefghijklmn')
53+
54+
@responses.activate
55+
def test_get_group_members_count(self):
56+
responses.add(
57+
responses.GET,
58+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/group/group_id/members/count',
59+
json={"count": 3},
60+
status=200
61+
)
62+
63+
count = self.tested.get_group_members_count('group_id')
64+
65+
request = responses.calls[0].request
66+
self.assertEqual(request.method, 'GET')
67+
self.assertEqual(
68+
request.url,
69+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/group/group_id/members/count')
70+
self.assertEqual(count, 3)
71+
72+
73+
if __name__ == '__main__':
74+
unittest.main()

tests/api/test_get_room.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
from __future__ import unicode_literals, absolute_import
16+
17+
import unittest
18+
19+
import responses
20+
21+
from linebot import (
22+
LineBotApi
23+
)
24+
25+
26+
class TestLineBotApi(unittest.TestCase):
27+
def setUp(self):
28+
self.tested = LineBotApi('channel_secret')
29+
30+
@responses.activate
31+
def test_get_room_members_count(self):
32+
responses.add(
33+
responses.GET,
34+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/room/room_id/members/count',
35+
json={"count": 3},
36+
status=200
37+
)
38+
39+
count = self.tested.get_room_members_count('room_id')
40+
41+
request = responses.calls[0].request
42+
self.assertEqual(request.method, 'GET')
43+
self.assertEqual(
44+
request.url,
45+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/room/room_id/members/count')
46+
self.assertEqual(count, 3)
47+
48+
49+
if __name__ == '__main__':
50+
unittest.main()

0 commit comments

Comments
 (0)