Skip to content

Commit 38528aa

Browse files
authored
Merge pull request #104 from Vaduz/feature/member_ids_profile
Support group/room member profile/ids API
2 parents d229c16 + 9547f3f commit 38528aa

File tree

4 files changed

+308
-2
lines changed

4 files changed

+308
-2
lines changed

src/LINEBot.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,116 @@ public function validateSignature($body, $signature)
204204
{
205205
return SignatureValidator::validateSignature($body, $this->channelSecret, $signature);
206206
}
207+
208+
/**
209+
* Gets the user profile of a member of a group that the bot is in.
210+
* This can be the user ID of a user who has not added the bot as a friend or has blocked the bot.
211+
*
212+
* @param string $groupId Identifier of the group
213+
* @param string $userId Identifier of the user
214+
* @return Response
215+
*/
216+
public function getGroupMemberProfile($groupId, $userId)
217+
{
218+
$url = sprintf('%s/v2/bot/group/%s/member/%s', $this->endpointBase, urlencode($groupId), urlencode($userId));
219+
return $this->httpClient->get($url, []);
220+
}
221+
222+
/**
223+
* Gets the user profile of a member of a room that the bot is in.
224+
* This can be the user ID of a user who has not added the bot as a friend or has blocked the bot.
225+
*
226+
* @param string $roomId Identifier of the room
227+
* @param string $userId Identifier of the user
228+
* @return Response
229+
*/
230+
public function getRoomMemberProfile($roomId, $userId)
231+
{
232+
$url = sprintf('%s/v2/bot/room/%s/member/%s', $this->endpointBase, urlencode($roomId), urlencode($userId));
233+
return $this->httpClient->get($url, []);
234+
}
235+
236+
/**
237+
* Gets the user IDs of the members of a group that the bot is in.
238+
* This includes the user IDs of users who have not added the bot as a friend or has blocked the bot.
239+
*
240+
* This feature is only available for LINE@ Approved accounts or official accounts.
241+
*
242+
* @param string $groupId Identifier of the group
243+
* @param string $start continuationToken
244+
* @return Response
245+
*/
246+
public function getGroupMemberIds($groupId, $start = null)
247+
{
248+
$url = sprintf('%s/v2/bot/group/%s/members/ids', $this->endpointBase, urlencode($groupId));
249+
$params = is_null($start) ? [] : ['start' => $start];
250+
return $this->httpClient->get($url, $params);
251+
}
252+
253+
/**
254+
* Gets the user IDs of the members of a room that the bot is in.
255+
* This includes the user IDs of users who have not added the bot as a friend or has blocked the bot.
256+
*
257+
* This feature is only available for LINE@ Approved accounts or official accounts.
258+
*
259+
* @param string $roomId Identifier of the room
260+
* @param string $start continuationToken
261+
* @return Response
262+
*/
263+
public function getRoomMemberIds($roomId, $start = null)
264+
{
265+
$url = sprintf('%s/v2/bot/room/%s/members/ids', $this->endpointBase, urlencode($roomId));
266+
$params = is_null($start) ? [] : ['start' => $start];
267+
return $this->httpClient->get($url, $params);
268+
}
269+
270+
/**
271+
* Gets the user IDs of the members of a group that the bot is in.
272+
* This includes the user IDs of users who have not added the bot as a friend or has blocked the bot.
273+
* This method gets all of the members by calling getGroupMemberIds() continually using token
274+
*
275+
* This feature is only available for LINE@ Approved accounts or official accounts.
276+
*
277+
* @param string $groupId Identifier of the group
278+
* @return array memberIds
279+
* @see LINE\LINEBot\getGroupMemberIds()
280+
*/
281+
public function getAllGroupMemberIds($groupId)
282+
{
283+
$memberIds = [];
284+
$continuationToken = null;
285+
do {
286+
$response = $this->getGroupMemberIds($groupId, $continuationToken);
287+
$data = $response->getJSONDecodedBody();
288+
$memberIds = array_merge($memberIds, $data['memberIds']);
289+
$continuationToken = isset($data['next']) ? $data['next'] : null;
290+
} while ($continuationToken);
291+
292+
return $memberIds;
293+
}
294+
295+
/**
296+
* Gets the user IDs of the members of a room that the bot is in.
297+
* This includes the user IDs of users who have not added the bot as a friend or has blocked the bot.
298+
* This method gets all of the members by calling getRoomMemberIds() continually using token
299+
*
300+
* This feature is only available for LINE@ Approved accounts or official accounts.
301+
*
302+
* @param string $roomId Identifier of the room
303+
* @return array memberIds
304+
* @see LINE\LINEBot\getRoomMemberIds()
305+
*/
306+
public function getAllRoomMemberIds($roomId)
307+
{
308+
$memberIds = [];
309+
$continuationToken = null;
310+
do {
311+
$response = $this->getRoomMemberIds($roomId, $continuationToken);
312+
$data = $response->getJSONDecodedBody();
313+
$memberIds = array_merge($memberIds, $data['memberIds']);
314+
$continuationToken = isset($data['next']) ? $data['next'] : null;
315+
} while ($continuationToken);
316+
317+
return $memberIds;
318+
}
207319
}

tests/LINEBot/GetMemberIdsTest.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2016 LINE Corporation
5+
*
6+
* LINE Corporation licenses this file to you under the Apache License,
7+
* version 2.0 (the "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at:
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
* License for the specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
namespace LINE\Tests\LINEBot;
20+
21+
use LINE\LINEBot;
22+
use LINE\Tests\LINEBot\Util\DummyHttpClient;
23+
24+
class GetMemberIdsTest extends \PHPUnit_Framework_TestCase
25+
{
26+
public function testGetGroupMemberIds()
27+
{
28+
$mock = function ($testRunner, $httpMethod, $url, $data) {
29+
/** @var \PHPUnit_Framework_TestCase $testRunner */
30+
$testRunner->assertEquals('GET', $httpMethod);
31+
$testRunner->assertEquals('https://api.line.me/v2/bot/group/GROUP_ID/members/ids', $url);
32+
33+
if (!$data) {
34+
return [
35+
'memberIds' => ['Uxxxxxxxxxxxxxx1', 'Uxxxxxxxxxxxxxx2', 'Uxxxxxxxxxxxxxx3'],
36+
'next' => 'testContinuationToken'
37+
];
38+
} else {
39+
$testRunner->assertEquals(['start' => 'testContinuationToken'], $data);
40+
return ['memberIds' => ['Uxxxxxxxxxxxxxx4', 'Uxxxxxxxxxxxxxx5']];
41+
}
42+
};
43+
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
44+
45+
// First time call
46+
$res = $bot->getGroupMemberIds('GROUP_ID');
47+
48+
$this->assertEquals(200, $res->getHTTPStatus());
49+
$this->assertTrue($res->isSucceeded());
50+
51+
$data = $res->getJSONDecodedBody();
52+
$this->assertEquals(['Uxxxxxxxxxxxxxx1', 'Uxxxxxxxxxxxxxx2', 'Uxxxxxxxxxxxxxx3'], $data['memberIds']);
53+
$this->assertEquals('testContinuationToken', $data['next']);
54+
55+
// Second time call
56+
$res = $bot->getGroupMemberIds('GROUP_ID', 'testContinuationToken');
57+
58+
$this->assertEquals(200, $res->getHTTPStatus());
59+
$this->assertTrue($res->isSucceeded());
60+
61+
$data = $res->getJSONDecodedBody();
62+
$this->assertEquals(['Uxxxxxxxxxxxxxx4', 'Uxxxxxxxxxxxxxx5'], $data['memberIds']);
63+
$this->assertFalse(array_key_exists('next', $data));
64+
65+
// test getAllGroupMemberIds()
66+
$memberIds = $bot->getAllGroupMemberIds('GROUP_ID');
67+
$this->assertEquals(
68+
['Uxxxxxxxxxxxxxx1', 'Uxxxxxxxxxxxxxx2', 'Uxxxxxxxxxxxxxx3', 'Uxxxxxxxxxxxxxx4', 'Uxxxxxxxxxxxxxx5'],
69+
$memberIds
70+
);
71+
}
72+
73+
public function testGetRoomMemberIds()
74+
{
75+
$mock = function ($testRunner, $httpMethod, $url, $data) {
76+
/** @var \PHPUnit_Framework_TestCase $testRunner */
77+
$testRunner->assertEquals('GET', $httpMethod);
78+
$testRunner->assertEquals('https://api.line.me/v2/bot/room/ROOM_ID/members/ids', $url);
79+
80+
if (!$data) {
81+
return [
82+
'memberIds' => ['Uxxxxxxxxxxxxxx1', 'Uxxxxxxxxxxxxxx2', 'Uxxxxxxxxxxxxxx3'],
83+
'next' => 'testContinuationToken'
84+
];
85+
} else {
86+
$testRunner->assertEquals(['start' => 'testContinuationToken'], $data);
87+
return ['memberIds' => ['Uxxxxxxxxxxxxxx4', 'Uxxxxxxxxxxxxxx5']];
88+
}
89+
};
90+
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
91+
92+
// First time call
93+
$res = $bot->getRoomMemberIds('ROOM_ID');
94+
95+
$this->assertEquals(200, $res->getHTTPStatus());
96+
$this->assertTrue($res->isSucceeded());
97+
98+
$data = $res->getJSONDecodedBody();
99+
$this->assertEquals(['Uxxxxxxxxxxxxxx1', 'Uxxxxxxxxxxxxxx2', 'Uxxxxxxxxxxxxxx3'], $data['memberIds']);
100+
$this->assertEquals('testContinuationToken', $data['next']);
101+
102+
// Second time call
103+
$res = $bot->getRoomMemberIds('ROOM_ID', 'testContinuationToken');
104+
105+
$this->assertEquals(200, $res->getHTTPStatus());
106+
$this->assertTrue($res->isSucceeded());
107+
108+
$data = $res->getJSONDecodedBody();
109+
$this->assertEquals(['Uxxxxxxxxxxxxxx4', 'Uxxxxxxxxxxxxxx5'], $data['memberIds']);
110+
$this->assertFalse(array_key_exists('next', $data));
111+
112+
// test getAllGroupMemberIds()
113+
$memberIds = $bot->getAllRoomMemberIds('ROOM_ID');
114+
$this->assertEquals(
115+
['Uxxxxxxxxxxxxxx1', 'Uxxxxxxxxxxxxxx2', 'Uxxxxxxxxxxxxxx3', 'Uxxxxxxxxxxxxxx4', 'Uxxxxxxxxxxxxxx5'],
116+
$memberIds
117+
);
118+
}
119+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2016 LINE Corporation
5+
*
6+
* LINE Corporation licenses this file to you under the Apache License,
7+
* version 2.0 (the "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at:
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
* License for the specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
namespace LINE\Tests\LINEBot;
20+
21+
use LINE\LINEBot;
22+
use LINE\Tests\LINEBot\Util\DummyHttpClient;
23+
24+
class GetMemberProfileTest extends \PHPUnit_Framework_TestCase
25+
{
26+
public function testGetGroupMemberIds()
27+
{
28+
$mock = function ($testRunner, $httpMethod, $url, $data) {
29+
/** @var \PHPUnit_Framework_TestCase $testRunner */
30+
$testRunner->assertEquals('GET', $httpMethod);
31+
$testRunner->assertEquals('https://api.line.me/v2/bot/group/GROUP_ID/member/MEMBER_ID', $url);
32+
return [
33+
'displayName' => 'LINE taro',
34+
'userId' => 'Uxxxxxxxxxxxxxx1',
35+
'pictureUrl' => 'https://example.com/profile.png',
36+
];
37+
};
38+
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
39+
40+
$res = $bot->getGroupMemberProfile('GROUP_ID', 'MEMBER_ID');
41+
42+
$this->assertEquals(200, $res->getHTTPStatus());
43+
$this->assertTrue($res->isSucceeded());
44+
45+
$data = $res->getJSONDecodedBody();
46+
$this->assertEquals('LINE taro', $data['displayName']);
47+
$this->assertEquals('Uxxxxxxxxxxxxxx1', $data['userId']);
48+
$this->assertEquals('https://example.com/profile.png', $data['pictureUrl']);
49+
}
50+
51+
public function testGetRoomMemberIds()
52+
{
53+
$mock = function ($testRunner, $httpMethod, $url, $data) {
54+
/** @var \PHPUnit_Framework_TestCase $testRunner */
55+
$testRunner->assertEquals('GET', $httpMethod);
56+
$testRunner->assertEquals('https://api.line.me/v2/bot/room/ROOM_ID/member/MEMBER_ID', $url);
57+
return [
58+
'displayName' => 'LINE taro',
59+
'userId' => 'Uxxxxxxxxxxxxxx1',
60+
'pictureUrl' => 'https://example.com/profile.png',
61+
];
62+
};
63+
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
64+
65+
$res = $bot->getRoomMemberProfile('ROOM_ID', 'MEMBER_ID');
66+
67+
$this->assertEquals(200, $res->getHTTPStatus());
68+
$this->assertTrue($res->isSucceeded());
69+
70+
$data = $res->getJSONDecodedBody();
71+
$this->assertEquals('LINE taro', $data['displayName']);
72+
$this->assertEquals('Uxxxxxxxxxxxxxx1', $data['userId']);
73+
$this->assertEquals('https://example.com/profile.png', $data['pictureUrl']);
74+
}
75+
}

tests/LINEBot/Util/DummyHttpClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public function __construct(\PHPUnit_Framework_TestCase $testRunner, \Closure $m
3838
* @param string $url
3939
* @return Response
4040
*/
41-
public function get($url)
41+
public function get($url, $data = null)
4242
{
43-
$ret = call_user_func($this->mock, $this->testRunner, 'GET', $url, []);
43+
$ret = call_user_func($this->mock, $this->testRunner, 'GET', $url, is_null($data) ? [] : $data);
4444
return new Response(200, json_encode($ret));
4545
}
4646

0 commit comments

Comments
 (0)