Skip to content

Commit c63864e

Browse files
authored
Add mention property to TextMessageEvent (#312)
* Add mentionee * Update messages * Fix messages * Update mentionee * Update Mentionee - modify docstring * Create Mention Object * Update TextMessage - delete mentionees property - add mention property - modify docstring * Update TestTextMessage - add test_mentionees - add test_null_mentionees * Update test_text_message * Update TextMessage * Update TestTextMessage * Update TestTextMessage
1 parent b445070 commit c63864e

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed

linebot/models/mention.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
"""linebot.models.mention module."""
16+
17+
from __future__ import unicode_literals
18+
19+
from abc import ABCMeta
20+
21+
from future.utils import with_metaclass
22+
23+
from .base import Base
24+
25+
26+
class Mention(with_metaclass(ABCMeta, Base)):
27+
"""Mention.
28+
29+
https://developers.line.biz/en/reference/messaging-api/#text-message
30+
31+
Object containing the contents of the mentioned user.
32+
"""
33+
34+
def __init__(self, mentionees=None, **kwargs):
35+
"""__init__ method.
36+
37+
:param List mentionees: Array of LINE mentionee objects
38+
:param kwargs:
39+
"""
40+
super(Mention, self).__init__(**kwargs)
41+
42+
self.mentionees = mentionees

linebot/models/mentionee.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
"""linebot.models.mentionee module."""
16+
17+
from __future__ import unicode_literals
18+
19+
from abc import ABCMeta
20+
21+
from future.utils import with_metaclass
22+
23+
from .base import Base
24+
25+
26+
class Mentionee(with_metaclass(ABCMeta, Base)):
27+
"""Mentionee.
28+
29+
https://developers.line.biz/en/reference/messaging-api/#text-message
30+
31+
Mentioned user information.
32+
"""
33+
34+
def __init__(self, index=None, length=None, user_id=None, **kwargs):
35+
"""__init__ method.
36+
37+
:param int index: Index position of the user mention for a character
38+
:param int length: Length of the text of the mentioned user
39+
:param str user_id: User ID
40+
:param kwargs:
41+
"""
42+
super(Mentionee, self).__init__(**kwargs)
43+
44+
self.index = index
45+
self.length = length
46+
self.user_id = user_id

linebot/models/messages.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from future.utils import with_metaclass
2222

2323
from linebot.models.emojis import Emojis
24+
from .mention import Mention
25+
from .mentionee import Mentionee
2426
from .base import Base
2527

2628

@@ -47,12 +49,13 @@ class TextMessage(Message):
4749
Message object which contains the text sent from the source.
4850
"""
4951

50-
def __init__(self, id=None, text=None, emojis=None, **kwargs):
52+
def __init__(self, id=None, text=None, emojis=None, mention=None, **kwargs):
5153
"""__init__ method.
5254
5355
:param str id: Message ID
5456
:param str text: Message text
5557
:param List sticon: Array of LINE emoji objects
58+
:param object mention: LINE mention object
5659
:param kwargs:
5760
"""
5861
super(TextMessage, self).__init__(id=id, **kwargs)
@@ -71,6 +74,21 @@ def __init__(self, id=None, text=None, emojis=None, **kwargs):
7174
else:
7275
self.emojis = emojis
7376

77+
if mention:
78+
mention_object = self.get_or_new_from_json_dict(
79+
mention, Mention
80+
)
81+
mentionees = []
82+
for mentionee in mention_object.mentionees:
83+
mentionee_object = self.get_or_new_from_json_dict(
84+
mentionee, Mentionee
85+
)
86+
if mentionee_object:
87+
mentionees.append(mentionee_object)
88+
self.mention = Mention(mentionees)
89+
else:
90+
self.mention = mention
91+
7492

7593
class ImageMessage(Message):
7694
"""ImageMessage.

tests/models/test_text_message.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
from linebot.models import TextMessage
2020
from linebot.models.emojis import Emojis
21+
from linebot.models.mentionee import Mentionee
22+
from linebot.models.mention import Mention
2123
from tests.models.serialize_test_case import SerializeTestCase
2224

2325

@@ -56,6 +58,32 @@ def test_null_emojis(self):
5658
TextMessage(**arg).as_json_dict()
5759
)
5860

61+
def test_mention(self):
62+
arg = {
63+
"type": "text",
64+
"text": "@example Hello, world! (love)",
65+
"mention": Mention(
66+
mentionees=[
67+
Mentionee(
68+
index=0,
69+
length=8,
70+
user_id="U850014438e..."
71+
)
72+
]
73+
),
74+
}
75+
self.assertEqual(
76+
self.serialize_as_dict(arg, type=self.TEXT),
77+
TextMessage(**arg).as_json_dict(),
78+
)
79+
80+
def test_null_mention(self):
81+
arg = {"type": "text", "text": "Hello, world!"}
82+
self.assertEqual(
83+
self.serialize_as_dict(arg, type=self.TEXT),
84+
TextMessage(**arg).as_json_dict(),
85+
)
86+
5987

6088
if __name__ == '__main__':
6189
unittest.main()

0 commit comments

Comments
 (0)