Skip to content

Commit 0e1ecf2

Browse files
authored
Merge pull request #53 from turfaa/get-profile-from-group-and-room
Add Get Group/Room Member Profile API
2 parents c5d9097 + c4bb7a7 commit 0e1ecf2

File tree

5 files changed

+206
-2
lines changed

5 files changed

+206
-2
lines changed

README.rst

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ https://devdocs.line.me/en/#multicast
125125
126126
line_bot_api.multicast(['to1', 'to2'], TextSendMessage(text='Hello World!'))
127127
128-
129128
get\_profile(self, user\_id, timeout=None)
130129
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
131130

@@ -142,6 +141,39 @@ https://devdocs.line.me/en/#bot-api-get-profile
142141
print(profile.picture_url)
143142
print(profile.status_message)
144143
144+
get\_group\_member\_profile(self, group\_id, user\_id, timeout=None)
145+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
146+
147+
Gets the user profile of a member of a group that the bot is in. This can be
148+
the user ID of a user who has not added the bot as a friend or has blocked
149+
the bot.
150+
151+
https://devdocs.line.me/en/#get-group-room-member-profile
152+
153+
.. code:: python
154+
155+
profile = line_bot_api.get_group_member_profile(group_id, user_id)
156+
157+
print(profile.display_name)
158+
print(profile.user_id)
159+
print(profile.picture_url)
160+
161+
get\_room\_member\_profile(self, room\_id, user\_id, timeout=None)
162+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
163+
164+
Gets the user profile of a member of a room that the bot is in. This can be the
165+
user ID of a user who has not added the bot as a friend or has blocked the bot.
166+
167+
https://devdocs.line.me/en/#get-group-room-member-profile
168+
169+
.. code:: python
170+
171+
profile = line_bot_api.get_room_member_profile(room_id, user_id)
172+
173+
print(profile.display_name)
174+
print(profile.user_id)
175+
print(profile.picture_url)
176+
145177
get\_message\_content(self, message\_id, timeout=None)
146178
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147179

linebot/api.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,64 @@ def get_profile(self, user_id, timeout=None):
179179

180180
return Profile.new_from_json_dict(response.json)
181181

182+
def get_group_member_profile(self, group_id, user_id, timeout=None):
183+
"""Call get group member profile API.
184+
185+
https://devdocs.line.me/en/#get-group-room-member-profile
186+
187+
Gets the user profile of a member of a group that
188+
the bot is in. This can be the user ID of a user who has
189+
not added the bot as a friend or has blocked the bot.
190+
191+
:param str user_id: User ID
192+
:param str group_id: Group ID
193+
:param timeout: (optional) How long to wait for the server
194+
to send data before giving up, as a float,
195+
or a (connect timeout, readtimeout) float tuple.
196+
Default is self.http_client.timeout
197+
:type timeout: float | tuple(float, float)
198+
:rtype: :py:class:`linebot.models.responses.Profile`
199+
:return: Profile instance
200+
"""
201+
response = self._get(
202+
'/v2/bot/group/{group_id}/member/{user_id}'.format(
203+
user_id=user_id,
204+
group_id=group_id
205+
),
206+
timeout=timeout
207+
)
208+
209+
return Profile.new_from_json_dict(response.json)
210+
211+
def get_room_member_profile(self, room_id, user_id, timeout=None):
212+
"""Call get room member profile API.
213+
214+
https://devdocs.line.me/en/#get-group-room-member-profile
215+
216+
Gets the user profile of a member of a room that
217+
the bot is in. This can be the user ID of a user who has
218+
not added the bot as a friend or has blocked the bot.
219+
220+
:param str user_id: User ID
221+
:param str room_id: Room ID
222+
:param timeout: (optional) How long to wait for the server
223+
to send data before giving up, as a float,
224+
or a (connect timeout, readtimeout) float tuple.
225+
Default is self.http_client.timeout
226+
:type timeout: float | tuple(float, float)
227+
:rtype: :py:class:`linebot.models.responses.Profile`
228+
:return: Profile instance
229+
"""
230+
response = self._get(
231+
'/v2/bot/room/{room_id}/member/{user_id}'.format(
232+
user_id=user_id,
233+
room_id=room_id
234+
),
235+
timeout=timeout
236+
)
237+
238+
return Profile.new_from_json_dict(response.json)
239+
182240
def get_message_content(self, message_id, timeout=None):
183241
"""Call get content API.
184242
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
from linebot import (
21+
LineBotApi
22+
)
23+
24+
25+
class TestLineBotApi(unittest.TestCase):
26+
def setUp(self):
27+
self.tested = LineBotApi('channel_secret')
28+
29+
@responses.activate
30+
def test_get_profile(self):
31+
responses.add(
32+
responses.GET,
33+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/group/group_id/' +
34+
'member/user_id',
35+
json={
36+
"displayName": "LINE taro",
37+
"userId": "Uxxxxxxxxxxxxxx...",
38+
"pictureUrl": "http://obs.line-apps.com/..."
39+
},
40+
status=200
41+
)
42+
43+
profile = self.tested.get_group_member_profile('group_id', 'user_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/' +
50+
'member/user_id')
51+
self.assertEqual(profile.display_name, 'LINE taro')
52+
self.assertEqual(profile.user_id, 'Uxxxxxxxxxxxxxx...')
53+
self.assertEqual(profile.picture_url, 'http://obs.line-apps.com/...')
54+
55+
56+
if __name__ == '__main__':
57+
unittest.main()

tests/api/test_get_profile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ def test_get_profile(self):
5454

5555

5656
if __name__ == '__main__':
57-
unittest.maAAin()
57+
unittest.main()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
from linebot import (
21+
LineBotApi
22+
)
23+
24+
25+
class TestLineBotApi(unittest.TestCase):
26+
def setUp(self):
27+
self.tested = LineBotApi('channel_secret')
28+
29+
@responses.activate
30+
def test_get_profile(self):
31+
responses.add(
32+
responses.GET,
33+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/room/room_id/' +
34+
'member/user_id',
35+
json={
36+
"displayName": "LINE taro",
37+
"userId": "Uxxxxxxxxxxxxxx...",
38+
"pictureUrl": "http://obs.line-apps.com/..."
39+
},
40+
status=200
41+
)
42+
43+
profile = self.tested.get_room_member_profile('room_id', 'user_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/room/room_id/' +
50+
'member/user_id')
51+
self.assertEqual(profile.display_name, 'LINE taro')
52+
self.assertEqual(profile.user_id, 'Uxxxxxxxxxxxxxx...')
53+
self.assertEqual(profile.picture_url, 'http://obs.line-apps.com/...')
54+
55+
56+
if __name__ == '__main__':
57+
unittest.main()

0 commit comments

Comments
 (0)