Skip to content

Commit 07b82ac

Browse files
authored
Merge pull request #329 from nanato12/master
Add mentionees property to TextMessageEvent
2 parents 334ae57 + 23e113a commit 07b82ac

File tree

3 files changed

+220
-3
lines changed

3 files changed

+220
-3
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2021 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\MessageEvent;
20+
21+
class MentioneeInfo
22+
{
23+
/** @var int */
24+
private $index;
25+
/** @var int */
26+
private $length;
27+
/** @var string */
28+
private $userId;
29+
30+
/**
31+
* Mentionee Info Constructor
32+
*
33+
* @param array $mentioneeInfo
34+
*/
35+
public function __construct($mentioneeInfo)
36+
{
37+
$this->index = $mentioneeInfo['index'];
38+
$this->length = $mentioneeInfo['length'];
39+
if (isset($mentioneeInfo['userId'])) {
40+
$this->userId = $mentioneeInfo['userId'];
41+
}
42+
}
43+
44+
/**
45+
* Returns Index position of the user mention for a character.
46+
*
47+
* @return int
48+
*/
49+
public function getIndex()
50+
{
51+
return $this->index;
52+
}
53+
54+
/**
55+
* Returns length of the text of the mentioned user.
56+
*
57+
* @return int
58+
*/
59+
public function getLength()
60+
{
61+
return $this->length;
62+
}
63+
64+
/**
65+
* Returns User ID of the mentioned user.
66+
*
67+
* @return string|null
68+
*/
69+
public function getUserId()
70+
{
71+
return $this->userId;
72+
}
73+
}

src/LINEBot/Event/MessageEvent/TextMessage.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ class TextMessage extends MessageEvent
3434
*/
3535
private $emojis;
3636

37+
/**
38+
* mentioned user Info List
39+
*
40+
* @var array|null
41+
*/
42+
private $mentionees;
43+
3744
/**
3845
* TextMessage constructor.
3946
*
@@ -47,6 +54,11 @@ public function __construct($event)
4754
return new EmojiInfo($emojiInfo);
4855
}, $this->message['emojis']);
4956
}
57+
if (isset($this->message['mention'])) {
58+
$this->mentionees = array_map(function ($mentioneeInfo) {
59+
return new MentioneeInfo($mentioneeInfo);
60+
}, $this->message['mention']['mentionees']);
61+
}
5062
}
5163

5264
/**
@@ -68,4 +80,14 @@ public function getEmojis()
6880
{
6981
return $this->emojis;
7082
}
83+
84+
/**
85+
* Returns mentioned user info list of the messages.
86+
*
87+
* @return array|null
88+
*/
89+
public function getMentionees()
90+
{
91+
return $this->mentionees;
92+
}
7193
}

tests/LINEBot/EventRequestParserTest.php

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ class EventRequestParserTest extends TestCase
509509
]
510510
}
511511
}
512-
},
512+
},
513513
{
514514
"type":"message",
515515
"mode":"active",
@@ -549,6 +549,68 @@ class EventRequestParserTest extends TestCase
549549
"trackingId": "track_id"
550550
},
551551
"replyToken":"replytoken"
552+
},
553+
{
554+
"replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
555+
"type": "message",
556+
"mode": "active",
557+
"timestamp": 1462629479859,
558+
"source": {
559+
"type": "user",
560+
"userId": "U4af4980629..."
561+
},
562+
"message": {
563+
"id": "325708",
564+
"type": "text",
565+
"text": "@example Hello, world! (love)",
566+
"mention": {
567+
"mentionees": [
568+
{
569+
"index": 0,
570+
"length": 8,
571+
"userId": "U0123456789abcd0123456789abcdef"
572+
}
573+
]
574+
}
575+
}
576+
},
577+
{
578+
"replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
579+
"type": "message",
580+
"mode": "active",
581+
"timestamp": 1462629479859,
582+
"source": {
583+
"type": "user",
584+
"userId": "U0123456789abcd0123456789abcdef"
585+
},
586+
"message": {
587+
"id": "325708",
588+
"type": "text",
589+
"text": "@example message without mentionee userId",
590+
"mention": {
591+
"mentionees": [
592+
{
593+
"index": 0,
594+
"length": 8
595+
}
596+
]
597+
}
598+
}
599+
},
600+
{
601+
"replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
602+
"type": "message",
603+
"mode": "active",
604+
"timestamp": 1462629479859,
605+
"source": {
606+
"type": "user",
607+
"userId": "U0123456789abcd0123456789abcdef"
608+
},
609+
"message": {
610+
"id": "325708",
611+
"type": "text",
612+
"text": "message without mention"
613+
}
552614
}
553615
]
554616
}
@@ -565,13 +627,13 @@ public function testParseEventRequest()
565627
}), ['channelSecret' => 'testsecret']);
566628
list($destination, $events) = $bot->parseEventRequest(
567629
$this::$json,
568-
'72gRU3rSwbF9yWd6+dqOK7IwIbDE+/TLu56PKoysdHE=',
630+
'Q4tp1jGo39vhlcbd4QiQ/9I+zoJDwGIkPP22wgoOjDI=',
569631
false
570632
);
571633

572634
$this->assertEquals($destination, 'U0123456789abcdef0123456789abcd');
573635

574-
$this->assertEquals(count($events), 32);
636+
$this->assertEquals(count($events), 35);
575637

576638
{
577639
// text
@@ -987,5 +1049,65 @@ public function testParseEventRequest()
9871049
/** @var UnsendMessage $event */
9881050
$this->assertEquals('track_id', $event->getTrackingId());
9891051
}
1052+
1053+
{
1054+
// text
1055+
$event = $events[32];
1056+
$this->assertEquals(1462629479859, $event->getTimestamp());
1057+
$this->assertEquals('active', $event->getMode());
1058+
$this->assertTrue($event->isUserEvent());
1059+
$this->assertEquals('U4af4980629...', $event->getUserId());
1060+
$this->assertEquals('U4af4980629...', $event->getEventSourceId());
1061+
$this->assertInstanceOf('LINE\LINEBot\Event\MessageEvent', $event);
1062+
$this->assertInstanceOf('LINE\LINEBot\Event\MessageEvent\TextMessage', $event);
1063+
/** @var TextMessage $event */
1064+
$this->assertEquals('nHuyWiB7yP5Zw52FIkcQobQuGDXCTA', $event->getReplyToken());
1065+
$this->assertEquals('325708', $event->getMessageId());
1066+
$this->assertEquals('text', $event->getMessageType());
1067+
$this->assertEquals('@example Hello, world! (love)', $event->getText());
1068+
$mentioneeInfo = $event->getMentionees()[0];
1069+
$this->assertEquals(0, $mentioneeInfo->getIndex());
1070+
$this->assertEquals(8, $mentioneeInfo->getLength());
1071+
$this->assertEquals('U0123456789abcd0123456789abcdef', $mentioneeInfo->getUserId());
1072+
}
1073+
1074+
{
1075+
// text without mentionee userId
1076+
$event = $events[33];
1077+
$this->assertEquals(1462629479859, $event->getTimestamp());
1078+
$this->assertEquals('active', $event->getMode());
1079+
$this->assertTrue($event->isUserEvent());
1080+
$this->assertEquals('U0123456789abcd0123456789abcdef', $event->getUserId());
1081+
$this->assertEquals('U0123456789abcd0123456789abcdef', $event->getEventSourceId());
1082+
$this->assertInstanceOf('LINE\LINEBot\Event\MessageEvent', $event);
1083+
$this->assertInstanceOf('LINE\LINEBot\Event\MessageEvent\TextMessage', $event);
1084+
/** @var TextMessage $event */
1085+
$this->assertEquals('nHuyWiB7yP5Zw52FIkcQobQuGDXCTA', $event->getReplyToken());
1086+
$this->assertEquals('325708', $event->getMessageId());
1087+
$this->assertEquals('text', $event->getMessageType());
1088+
$this->assertEquals('@example message without mentionee userId', $event->getText());
1089+
$mentioneeInfo = $event->getMentionees()[0];
1090+
$this->assertEquals(0, $mentioneeInfo->getIndex());
1091+
$this->assertEquals(8, $mentioneeInfo->getLength());
1092+
$this->assertEquals(null, $mentioneeInfo->getUserId());
1093+
}
1094+
1095+
{
1096+
// text without mention
1097+
$event = $events[34];
1098+
$this->assertEquals(1462629479859, $event->getTimestamp());
1099+
$this->assertEquals('active', $event->getMode());
1100+
$this->assertTrue($event->isUserEvent());
1101+
$this->assertEquals('U0123456789abcd0123456789abcdef', $event->getUserId());
1102+
$this->assertEquals('U0123456789abcd0123456789abcdef', $event->getEventSourceId());
1103+
$this->assertInstanceOf('LINE\LINEBot\Event\MessageEvent', $event);
1104+
$this->assertInstanceOf('LINE\LINEBot\Event\MessageEvent\TextMessage', $event);
1105+
/** @var TextMessage $event */
1106+
$this->assertEquals('nHuyWiB7yP5Zw52FIkcQobQuGDXCTA', $event->getReplyToken());
1107+
$this->assertEquals('325708', $event->getMessageId());
1108+
$this->assertEquals('text', $event->getMessageType());
1109+
$this->assertEquals('message without mention', $event->getText());
1110+
$this->assertEquals(null, $event->getMentionees());
1111+
}
9901112
}
9911113
}

0 commit comments

Comments
 (0)