Skip to content

Commit 06e0eae

Browse files
author
Shoya Shiraki
authored
Add sender properties (#247)
* add sender model * add test with sender * add comment
1 parent be395fd commit 06e0eae

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

linebot/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
StickerSendMessage,
168168
QuickReply,
169169
QuickReplyButton,
170+
Sender,
170171
)
171172
from .sources import ( # noqa
172173
Source,

linebot/models/send_messages.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@
2727
class SendMessage(with_metaclass(ABCMeta, Base)):
2828
"""Abstract Base Class of SendMessage."""
2929

30-
def __init__(self, quick_reply=None, **kwargs):
30+
def __init__(self, quick_reply=None, sender=None, **kwargs):
3131
"""__init__ method.
3232
3333
:param quick_reply: QuickReply object
3434
:type quick_reply: T <= :py:class:`linebot.models.send_messages.QuickReply`
35+
:param sender: Sender object
36+
:type sender: T <= :py:class:`linebot.models.send_messages.Sender`
3537
:param kwargs:
3638
"""
3739
super(SendMessage, self).__init__(**kwargs)
3840

3941
self.type = None
4042
self.quick_reply = self.get_or_new_from_json_dict(quick_reply, QuickReply)
43+
self.sender = self.get_or_new_from_json_dict(sender, Sender)
4144

4245

4346
class TextSendMessage(SendMessage):
@@ -232,3 +235,21 @@ def __init__(self, image_url=None, action=None, **kwargs):
232235
self.type = 'action'
233236
self.image_url = image_url
234237
self.action = get_action(action)
238+
239+
240+
class Sender(with_metaclass(ABCMeta, Base)):
241+
"""Sender.
242+
243+
https://developers.line.biz/en/reference/messaging-api/#icon-nickname-switch
244+
"""
245+
246+
def __init__(self, name=None, icon_url=None, **kwargs):
247+
"""__init__ method.
248+
249+
:param str name: Display name
250+
:param str icon_url: Icon image URL
251+
"""
252+
super(Sender, self).__init__(**kwargs)
253+
254+
self.name = name
255+
self.icon_url = icon_url
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 json
18+
import unittest
19+
20+
import responses
21+
22+
from linebot import (
23+
LineBotApi
24+
)
25+
from linebot.models import (
26+
TextSendMessage, Sender
27+
)
28+
29+
30+
class TestLineBotApi(unittest.TestCase):
31+
def setUp(self):
32+
self.tested = LineBotApi('channel_secret')
33+
34+
# test data
35+
self.text_message = TextSendMessage(text='Hello, world')
36+
self.message = [{"type": "text", "text": "Hello, world"}]
37+
38+
@responses.activate
39+
def test_push_text_message_with_quick_reply(self):
40+
responses.add(
41+
responses.POST,
42+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/message/push',
43+
json={}, status=200
44+
)
45+
46+
self.tested.push_message('to',
47+
TextSendMessage(
48+
text='Hello, world',
49+
sender=Sender(
50+
name='Cony',
51+
icon_url='https://example.com/original.jpg'
52+
)))
53+
54+
request = responses.calls[0].request
55+
self.assertEqual(request.method, 'POST')
56+
self.assertEqual(
57+
request.url,
58+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/message/push')
59+
self.assertEqual(
60+
json.loads(request.body),
61+
{
62+
"to": "to",
63+
'notificationDisabled': False,
64+
"messages": [
65+
{
66+
"type": "text",
67+
"text": "Hello, world",
68+
"sender": {
69+
"name": "Cony",
70+
"iconUrl": "https://example.com/original.jpg"
71+
}
72+
}
73+
]
74+
}
75+
)
76+
77+
78+
if __name__ == '__main__':
79+
unittest.main()

0 commit comments

Comments
 (0)