Skip to content

Commit 8065bac

Browse files
authored
Merge pull request #203 from clarencetw/member_join_event_member_leave_event
Member join event member leave event
2 parents 3cf91f9 + 2ac82e1 commit 8065bac

File tree

4 files changed

+165
-2
lines changed

4 files changed

+165
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2019 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\LINEBot\Event;
20+
21+
/**
22+
* A class that represents the event of joining.
23+
*
24+
* @package LINE\LINEBot\Event
25+
*/
26+
class MemberJoinEvent extends BaseEvent
27+
{
28+
/**
29+
* MemberJoinEvent constructor.
30+
*
31+
* @param array $event
32+
*/
33+
public function __construct($event)
34+
{
35+
parent::__construct($event);
36+
}
37+
38+
/**
39+
* Returns joined members.
40+
*
41+
* @return string
42+
*/
43+
public function getMembers()
44+
{
45+
return $this->event['joined']['members'];
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2019 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\LINEBot\Event;
20+
21+
/**
22+
* A class that represents the event of leaving.
23+
*
24+
* @package LINE\LINEBot\Event
25+
*/
26+
class MemberLeaveEvent extends BaseEvent
27+
{
28+
/**
29+
* MemberLeaveEvent constructor.
30+
*
31+
* @param array $event
32+
*/
33+
public function __construct($event)
34+
{
35+
parent::__construct($event);
36+
}
37+
38+
/**
39+
* Returns left members.
40+
*
41+
* @return string
42+
*/
43+
public function getMembers()
44+
{
45+
return $this->event['left']['members'];
46+
}
47+
}

src/LINEBot/Event/Parser/EventRequestParser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class EventRequestParser
3636
'postback' => 'LINE\LINEBot\Event\PostbackEvent',
3737
'beacon' => 'LINE\LINEBot\Event\BeaconDetectionEvent',
3838
'accountLink' => 'LINE\LINEBot\Event\AccountLinkEvent',
39+
'memberJoined' => 'LINE\LINEBot\Event\MemberJoinEvent',
40+
'memberLeft' => 'LINE\LINEBot\Event\MemberLeaveEvent',
3941
];
4042

4143
private static $messageType2class = [

tests/LINEBot/EventRequestParserTest.php

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use LINE\LINEBot\Event\FollowEvent;
2525
use LINE\LINEBot\Event\JoinEvent;
2626
use LINE\LINEBot\Event\LeaveEvent;
27+
use LINE\LINEBot\Event\MemberJoinEvent;
28+
use LINE\LINEBot\Event\MemberLeaveEvent;
2729
use LINE\LINEBot\Event\MessageEvent\AudioMessage;
2830
use LINE\LINEBot\Event\MessageEvent\FileMessage;
2931
use LINE\LINEBot\Event\MessageEvent\ImageMessage;
@@ -348,6 +350,47 @@ class EventRequestParserTest extends TestCase
348350
"result": "failed",
349351
"nonce": "1234567890abcdefghijklmnopqrstuvwxyz"
350352
}
353+
},
354+
{
355+
"type":"memberJoined",
356+
"timestamp":12345678901234,
357+
"source":{
358+
"type":"group",
359+
"groupId":"groupid"
360+
},
361+
"joined": {
362+
"members": [
363+
{
364+
"type": "user",
365+
"userId": "U4af4980629..."
366+
},
367+
{
368+
"type": "user",
369+
"userId": "U91eeaf62d9..."
370+
}
371+
]
372+
},
373+
"replyToken":"replytoken"
374+
},
375+
{
376+
"type":"memberLeft",
377+
"timestamp":12345678901234,
378+
"source":{
379+
"type":"group",
380+
"groupId":"groupid"
381+
},
382+
"left": {
383+
"members": [
384+
{
385+
"type": "user",
386+
"userId": "U4af4980629..."
387+
},
388+
{
389+
"type": "user",
390+
"userId": "U91eeaf62d9..."
391+
}
392+
]
393+
}
351394
}
352395
]
353396
}
@@ -362,9 +405,9 @@ public function testParseEventRequest()
362405
{
363406
$bot = new LINEBot(new DummyHttpClient($this, function () {
364407
}), ['channelSecret' => 'testsecret']);
365-
$events = $bot->parseEventRequest($this::$json, 'uilGuZPX3SyyreXYIYla+I3kS48xg4+igqQZL33fc6M=');
408+
$events = $bot->parseEventRequest($this::$json, 'iiWsqJCsXZSzoKpuZPBk9Vqw3XiAl+AqLJLUKYEVf2I=');
366409

367-
$this->assertEquals(count($events), 24);
410+
$this->assertEquals(count($events), 26);
368411

369412
{
370413
// text
@@ -640,5 +683,29 @@ public function testParseEventRequest()
640683
$this->assertEquals(true, $event->isFailed());
641684
$this->assertEquals("1234567890abcdefghijklmnopqrstuvwxyz", $event->getNonce());
642685
}
686+
687+
{
688+
// member join
689+
$event = $events[24];
690+
$this->assertInstanceOf('LINE\LINEBot\Event\MemberJoinEvent', $event);
691+
/** @var MemberJoinEvent $event */
692+
$this->assertEquals('replytoken', $event->getReplyToken());
693+
$this->assertEquals(12345678901234, $event->getTimestamp());
694+
$members = $event->getMembers();
695+
$this->assertEquals(["type" => "user", "userId" => "U4af4980629..."], $members[0]);
696+
$this->assertEquals(["type" => "user", "userId" => "U91eeaf62d9..."], $members[1]);
697+
}
698+
699+
{
700+
// member leave
701+
$event = $events[25];
702+
$this->assertInstanceOf('LINE\LINEBot\Event\MemberLeaveEvent', $event);
703+
/** @var MemberLeaveEvent $event */
704+
$this->assertTrue($event->getReplyToken() === null);
705+
$this->assertEquals(12345678901234, $event->getTimestamp());
706+
$members = $event->getMembers();
707+
$this->assertEquals(["type" => "user", "userId" => "U4af4980629..."], $members[0]);
708+
$this->assertEquals(["type" => "user", "userId" => "U91eeaf62d9..."], $members[1]);
709+
}
643710
}
644711
}

0 commit comments

Comments
 (0)