Repository: line/line-bot-sdk-python
Pull request #312 (2021-01-20 06:54:59)

Add mention property to TextMessageEvent
# Overview
Messaging API update for January 2021
https://developers.line.biz/en/news/2021/01/20/messaging-api-update-january-2021/

**Text message type properties**

## Related issue
- Messaging API - January 2021 update #313

nanato12: I created a new `Mention` object.
Modify `TextMessage.mentionees` -> `TextMessage.mention.mentionees`

Please review.
And added test.
nanato12: Please re-review( d94b215 & de8bfe6 )

Patch:
diff --git a/linebot/models/mention.py b/linebot/models/mention.py
new file mode 100644
index 0000000..e54ac79
--- /dev/null
+++ b/linebot/models/mention.py
@@ -0,0 +1,42 @@
+# -*- coding: utf-8 -*-
+
+#  Licensed under the Apache License, Version 2.0 (the "License"); you may
+#  not use this file except in compliance with the License. You may obtain
+#  a copy of the License at
+#
+#       https://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#  License for the specific language governing permissions and limitations
+#  under the License.
+
+"""linebot.models.mention module."""
+
+from __future__ import unicode_literals
+
+from abc import ABCMeta
+
+from future.utils import with_metaclass
+
+from .base import Base
+
+
+class Mention(with_metaclass(ABCMeta, Base)):
+    """Mention.
+
+    https://developers.line.biz/en/reference/messaging-api/#text-message
+
+    Object containing the contents of the mentioned user.
+    """
+
+    def __init__(self, mentionees=None, **kwargs):
+        """__init__ method.
+
+        :param List mentionees: Array of LINE mentionee objects
+        :param kwargs:
+        """
+        super(Mention, self).__init__(**kwargs)
+
+        self.mentionees = mentionees
diff --git a/linebot/models/mentionee.py b/linebot/models/mentionee.py
new file mode 100644
index 0000000..911bc72
--- /dev/null
+++ b/linebot/models/mentionee.py
@@ -0,0 +1,46 @@
+# -*- coding: utf-8 -*-
+
+#  Licensed under the Apache License, Version 2.0 (the "License"); you may
+#  not use this file except in compliance with the License. You may obtain
+#  a copy of the License at
+#
+#       https://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#  License for the specific language governing permissions and limitations
+#  under the License.
+
+"""linebot.models.mentionee module."""
+
+from __future__ import unicode_literals
+
+from abc import ABCMeta
+
+from future.utils import with_metaclass
+
+from .base import Base
+
+
+class Mentionee(with_metaclass(ABCMeta, Base)):
+    """Mentionee.
+
+    https://developers.line.biz/en/reference/messaging-api/#text-message
+
+    Mentioned user information.
+    """
+
+    def __init__(self, index=None, length=None, user_id=None, **kwargs):
+        """__init__ method.
+
+        :param int index: Index position of the user mention for a character
+        :param int length: Length of the text of the mentioned user
+        :param str user_id: User ID
+        :param kwargs:
+        """
+        super(Mentionee, self).__init__(**kwargs)
+
+        self.index = index
+        self.length = length
+        self.user_id = user_id
diff --git a/linebot/models/messages.py b/linebot/models/messages.py
index 49ca300..fc51975 100644
--- a/linebot/models/messages.py
+++ b/linebot/models/messages.py
@@ -21,6 +21,8 @@ from abc import ABCMeta
 from future.utils import with_metaclass
 
 from linebot.models.emojis import Emojis
+from .mention import Mention
+from .mentionee import Mentionee
 from .base import Base
 
 
@@ -47,12 +49,13 @@ class TextMessage(Message):
     Message object which contains the text sent from the source.
     """
 
-    def __init__(self, id=None, text=None, emojis=None, **kwargs):
+    def __init__(self, id=None, text=None, emojis=None, mention=None, **kwargs):
         """__init__ method.
 
         :param str id: Message ID
         :param str text: Message text
         :param List sticon: Array of LINE emoji objects
+        :param object mention: LINE mention object
         :param kwargs:
         """
         super(TextMessage, self).__init__(id=id, **kwargs)
@@ -71,6 +74,21 @@ class TextMessage(Message):
         else:
             self.emojis = emojis
 
+        if mention:
+            mention_object = self.get_or_new_from_json_dict(
+                mention, Mention
+            )
+            mentionees = []
+            for mentionee in mention_object.mentionees:
+                mentionee_object = self.get_or_new_from_json_dict(
+                    mentionee, Mentionee
+                )
+                if mentionee_object:
+                    mentionees.append(mentionee_object)
+            self.mention = Mention(mentionees)
+        else:
+            self.mention = mention
+
 
 class ImageMessage(Message):
     """ImageMessage.


Test patch:
diff --git a/tests/models/test_text_message.py b/tests/models/test_text_message.py
index ed416a2..d19670c 100644
--- a/tests/models/test_text_message.py
+++ b/tests/models/test_text_message.py
@@ -18,6 +18,8 @@ import unittest
 
 from linebot.models import TextMessage
 from linebot.models.emojis import Emojis
+from linebot.models.mentionee import Mentionee
+from linebot.models.mention import Mention
 from tests.models.serialize_test_case import SerializeTestCase
 
 
@@ -56,6 +58,32 @@ class TestTextMessage(SerializeTestCase):
             TextMessage(**arg).as_json_dict()
         )
 
+    def test_mention(self):
+        arg = {
+            "type": "text",
+            "text": "@example Hello, world! (love)",
+            "mention": Mention(
+                mentionees=[
+                    Mentionee(
+                        index=0,
+                        length=8,
+                        user_id="U850014438e..."
+                    )
+                ]
+            ),
+        }
+        self.assertEqual(
+            self.serialize_as_dict(arg, type=self.TEXT),
+            TextMessage(**arg).as_json_dict(),
+        )
+
+    def test_null_mention(self):
+        arg = {"type": "text", "text": "Hello, world!"}
+        self.assertEqual(
+            self.serialize_as_dict(arg, type=self.TEXT),
+            TextMessage(**arg).as_json_dict(),
+        )
+
 
 if __name__ == '__main__':
     unittest.main()