Skip to content

Commit a3d0a2e

Browse files
authored
feat: support LINE emoji in messages (#262)
1 parent ee255e7 commit a3d0a2e

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

linebot/models/messages.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from future.utils import with_metaclass
2222

23+
from linebot.models.sticon import Sticon
2324
from .base import Base
2425

2526

@@ -46,17 +47,28 @@ class TextMessage(Message):
4647
Message object which contains the text sent from the source.
4748
"""
4849

49-
def __init__(self, id=None, text=None, **kwargs):
50+
def __init__(self, id=None, text=None, sticon=None, **kwargs):
5051
"""__init__ method.
5152
5253
:param str id: Message ID
5354
:param str text: Message text
55+
:param List sticon: Array of LINE emoji objects
5456
:param kwargs:
5557
"""
5658
super(TextMessage, self).__init__(id=id, **kwargs)
5759

5860
self.type = 'text'
5961
self.text = text
62+
if sticon:
63+
new_sticons = []
64+
for action in sticon:
65+
sticon_obj = self.get_or_new_from_json_dict(
66+
action, Sticon
67+
)
68+
if sticon_obj:
69+
new_sticons.append(sticon_obj)
70+
self.sticon = new_sticons
71+
self.sticon = sticon
6072

6173

6274
class ImageMessage(Message):

linebot/models/sticon.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.sticon 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 Sticon(with_metaclass(ABCMeta, Base)):
27+
"""Sticon.
28+
29+
https://developers.line.biz/en/reference/messaging-api/#text-message
30+
31+
"""
32+
33+
def __init__(self, index=None, product_id=None, sticon_id=None, **kwargs):
34+
"""__init__ method.
35+
36+
:param kwargs:
37+
"""
38+
super(Sticon, self).__init__(**kwargs)
39+
40+
self.index = index
41+
self.product_id = product_id
42+
self.sticon_id = sticon_id

tests/models/test_text_message.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
from __future__ import unicode_literals, absolute_import
16+
17+
import unittest
18+
19+
from linebot.models import TextMessage
20+
from linebot.models.sticon import Sticon
21+
from tests.models.serialize_test_case import SerializeTestCase
22+
23+
24+
class TestTextMessage(SerializeTestCase):
25+
def test_sticon(self):
26+
arg = {
27+
"type": "text",
28+
"text": "$ LINE emoji $",
29+
'sticon': [
30+
Sticon(
31+
index=0,
32+
product_id='5ac1bfd5040ab15980c9b435',
33+
sticon_id='001'
34+
),
35+
Sticon(
36+
index=13,
37+
product_id='5ac1bfd5040ab15980c9b435',
38+
sticon_id='002'
39+
),
40+
]
41+
}
42+
self.assertEqual(
43+
self.serialize_as_dict(arg, type=self.TEXT),
44+
TextMessage(**arg).as_json_dict()
45+
)
46+
47+
def test_null_sticon(self):
48+
arg = {
49+
"type": "text",
50+
"text": "\uDBC0\uDC84 LINE original emoji"
51+
}
52+
self.assertEqual(
53+
self.serialize_as_dict(arg, type=self.TEXT),
54+
TextMessage(**arg).as_json_dict()
55+
)
56+
57+
58+
if __name__ == '__main__':
59+
unittest.main()

0 commit comments

Comments
 (0)