Skip to content

Commit 71474a6

Browse files
authored
Support Rich menu alias APIs (#332)
* Create publish-to-pypi.yml * Create python-package.yml * Rename python-package.yml to ci.yml * Update ci.yml * Update ci.yml * remove 2.7 * remove 3.4, 3.5 DEPRECATION: Python 3.4 support has been deprecated. pip 19.1 will be the last one supporting it. Please upgrade your Python as Python 3.4 won't be maintained after March 2019 (cf PEP 429). * Update tox.ini * Update tox.ini * Update tox.ini * Update README.rst * Update README.rst * Update README.rst * Update ci.yml * remove travis yml * Rename ci.yml to auto-testing.yml * fix python version to 3.x * feat: add rich menu switch action * feat: add alias models * feat: add rich menu alias get/get list/update/delete api * test: add rich menu alias switch action test * test: add alias test cases * fix lint * fix typo * fix lint * fix document link error
1 parent bcf3c69 commit 71474a6

File tree

6 files changed

+455
-18
lines changed

6 files changed

+455
-18
lines changed

linebot/api.py

Lines changed: 114 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
AudienceGroup, ClickAudienceGroup, ImpAudienceGroup, GetAuthorityLevel, Audience,
3333
CreateAudienceGroup
3434
)
35-
from .models.responses import Group, UserIds
35+
from .models.responses import Group, UserIds, RichMenuAliasResponse, RichMenuAliasListResponse
3636

3737

3838
class LineBotApi(object):
@@ -657,7 +657,7 @@ def leave_room(self, room_id, timeout=None):
657657
def get_rich_menu(self, rich_menu_id, timeout=None):
658658
"""Call get rich menu API.
659659
660-
https://developers.line.me/en/docs/messaging-api/reference/#get-rich-menu
660+
https://developers.line.biz/en/reference/messaging-api/#get-rich-menu
661661
662662
:param str rich_menu_id: ID of the rich menu
663663
:param timeout: (optional) How long to wait for the server
@@ -675,10 +675,49 @@ def get_rich_menu(self, rich_menu_id, timeout=None):
675675

676676
return RichMenuResponse.new_from_json_dict(response.json)
677677

678+
def get_rich_menu_alias(self, rich_menu_alias_id=None, timeout=None):
679+
"""Call get rich menu alias API.
680+
681+
https://developers.line.biz/en/reference/messaging-api/#get-rich-menu-alias-by-id
682+
683+
:param str rich_menu_alias_id: ID of an uploaded rich menu alias.
684+
:param timeout: (optional) How long to wait for the server
685+
to send data before giving up, as a float,
686+
or a (connect timeout, read timeout) float tuple.
687+
Default is self.http_client.timeout
688+
:type timeout: float | tuple(float, float)
689+
:rtype: :py:class:`linebot.models.responses.RichMenuAliasResponse`
690+
:return: RichMenuAliasResponse instance
691+
"""
692+
response = self._get(
693+
'/v2/bot/richmenu/alias/{rich_menu_id}'.format(rich_menu_id=rich_menu_alias_id),
694+
timeout=timeout
695+
)
696+
return RichMenuAliasResponse.new_from_json_dict(response.json)
697+
698+
def get_rich_menu_alias_list(self, timeout=None):
699+
"""Call get rich menu alias list API.
700+
701+
https://developers.line.biz/en/reference/messaging-api/#update-rich-menu-alias
702+
703+
:param timeout: (optional) How long to wait for the server
704+
to send data before giving up, as a float,
705+
or a (connect timeout, read timeout) float tuple.
706+
Default is self.http_client.timeout
707+
:type timeout: float | tuple(float, float)
708+
:rtype: :py:class:`linebot.models.responses.RichMenuAliasListResponse`
709+
:return: RichMenuAliasListResponse instance
710+
"""
711+
response = self._get(
712+
'/v2/bot/richmenu/alias/list',
713+
timeout=timeout
714+
)
715+
return RichMenuAliasListResponse.new_from_json_dict(response.json)
716+
678717
def create_rich_menu(self, rich_menu, timeout=None):
679718
"""Call create rich menu API.
680719
681-
https://developers.line.me/en/docs/messaging-api/reference/#create-rich-menu
720+
https://developers.line.biz/en/reference/messaging-api/#create-rich-menu
682721
683722
:param rich_menu: Inquired to create a rich menu object.
684723
:type rich_menu: T <= :py:class:`linebot.models.rich_menu.RichMenu`
@@ -696,10 +735,51 @@ def create_rich_menu(self, rich_menu, timeout=None):
696735

697736
return response.json.get('richMenuId')
698737

738+
def create_rich_menu_alias(self, rich_menu_alias, timeout=None):
739+
"""Call create rich menu alias API.
740+
741+
https://developers.line.biz/en/reference/messaging-api/#create-rich-menu-alias
742+
743+
:param rich_menu_alias: Inquired to create a rich menu alias object.
744+
:type rich_menu_alias: T <= :py:class:`linebot.models.rich_menu.RichMenuAlias`
745+
:param timeout: (optional) How long to wait for the server
746+
to send data before giving up, as a float,
747+
or a (connect timeout, read timeout) float tuple.
748+
Default is self.http_client.timeout
749+
:type timeout: float | tuple(float, float)
750+
:rtype: str
751+
:return: rich menu id
752+
"""
753+
self._post(
754+
'/v2/bot/richmenu/alias', data=rich_menu_alias.as_json_string(), timeout=timeout
755+
)
756+
757+
def update_rich_menu_alias(self, rich_menu_alias_id, rich_menu_alias, timeout=None):
758+
"""Call update rich menu alias API.
759+
760+
https://developers.line.biz/en/reference/messaging-api/#update-rich-menu-alias
761+
762+
:param str rich_menu_alias_id: ID of an uploaded rich menu alias.
763+
:param rich_menu_alias: Inquired to create a rich menu alias object.
764+
:type rich_menu_alias: T <= :py:class:`linebot.models.rich_menu.RichMenuAlias`
765+
:param timeout: (optional) How long to wait for the server
766+
to send data before giving up, as a float,
767+
or a (connect timeout, read timeout) float tuple.
768+
Default is self.http_client.timeout
769+
:type timeout: float | tuple(float, float)
770+
:rtype: str
771+
:return: rich menu id
772+
"""
773+
self._post(
774+
'/v2/bot/richmenu/alias/{rich_menu_id}'.format(rich_menu_id=rich_menu_alias_id),
775+
data=rich_menu_alias.as_json_string(),
776+
timeout=timeout
777+
)
778+
699779
def delete_rich_menu(self, rich_menu_id, timeout=None):
700780
"""Call delete rich menu API.
701781
702-
https://developers.line.me/en/docs/messaging-api/reference/#delete-rich-menu
782+
https://developers.line.biz/en/reference/messaging-api/#delete-rich-menu
703783
704784
:param str rich_menu_id: ID of an uploaded rich menu
705785
:param timeout: (optional) How long to wait for the server
@@ -713,10 +793,28 @@ def delete_rich_menu(self, rich_menu_id, timeout=None):
713793
timeout=timeout
714794
)
715795

796+
def delete_rich_menu_alias(self, rich_menu_alias_id, timeout=None):
797+
"""Call delete rich menu alias API.
798+
799+
https://developers.line.biz/en/reference/messaging-api/#delete-rich-menu-alias
800+
801+
:param str rich_menu_alias_id: ID of an uploaded rich menu alias.
802+
:param timeout: (optional) How long to wait for the server
803+
to send data before giving up, as a float,
804+
or a (connect timeout, read timeout) float tuple.
805+
Default is self.http_client.timeout
806+
:type timeout: float | tuple(float, float)
807+
"""
808+
self._delete(
809+
'/v2/bot/richmenu/alias/{rich_menu_alias_id}'.format(
810+
rich_menu_alias_id=rich_menu_alias_id),
811+
timeout=timeout
812+
)
813+
716814
def get_rich_menu_id_of_user(self, user_id, timeout=None):
717815
"""Call get rich menu ID of user API.
718816
719-
https://developers.line.me/en/docs/messaging-api/reference/#get-rich-menu-id-of-user
817+
https://developers.line.biz/en/reference/messaging-api/#get-rich-menu-id-of-user
720818
721819
:param str user_id: IDs of the user
722820
:param timeout: (optional) How long to wait for the server
@@ -737,7 +835,7 @@ def get_rich_menu_id_of_user(self, user_id, timeout=None):
737835
def link_rich_menu_to_user(self, user_id, rich_menu_id, timeout=None):
738836
"""Call link rich menu to user API.
739837
740-
https://developers.line.me/en/docs/messaging-api/reference/#link-rich-menu-to-user
838+
https://developers.line.biz/en/reference/messaging-api/#link-rich-menu-to-user
741839
742840
:param str user_id: ID of the user
743841
:param str rich_menu_id: ID of an uploaded rich menu
@@ -782,7 +880,7 @@ def link_rich_menu_to_users(self, user_ids, rich_menu_id, timeout=None):
782880
def unlink_rich_menu_from_user(self, user_id, timeout=None):
783881
"""Call unlink rich menu from user API.
784882
785-
https://developers.line.me/en/docs/messaging-api/reference/#unlink-rich-menu-from-user
883+
https://developers.line.biz/en/reference/messaging-api#unlink-rich-menu-from-user
786884
787885
:param str user_id: ID of the user
788886
:param timeout: (optional) How long to wait for the server
@@ -821,7 +919,7 @@ def unlink_rich_menu_from_users(self, user_ids, timeout=None):
821919
def get_rich_menu_image(self, rich_menu_id, timeout=None):
822920
"""Call download rich menu image API.
823921
824-
https://developers.line.me/en/docs/messaging-api/reference/#download-rich-menu-image
922+
https://developers.line.biz/en/reference/messaging-api#download-rich-menu-image
825923
826924
:param str rich_menu_id: ID of the rich menu with the image to be downloaded
827925
:param timeout: (optional) How long to wait for the server
@@ -1300,14 +1398,14 @@ def add_audiences_to_audience_group(self, audience_group_id, audiences,
13001398
if audiences:
13011399
audiences = [Audience.new_from_json_dict(audience) for audience in audiences]
13021400
response = self._put(
1303-
'/v2/bot/audienceGroup/upload',
1304-
data=json.dumps({
1305-
"audienceGroupId": audience_group_id,
1306-
"audiences": [audience.as_json_dict() for audience in audiences],
1307-
"uploadDescription": upload_description,
1308-
}),
1309-
timeout=timeout
1310-
)
1401+
'/v2/bot/audienceGroup/upload',
1402+
data=json.dumps({
1403+
"audienceGroupId": audience_group_id,
1404+
"audiences": [audience.as_json_dict() for audience in audiences],
1405+
"uploadDescription": upload_description,
1406+
}),
1407+
timeout=timeout
1408+
)
13111409

13121410
return response.json
13131411

linebot/models/actions.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def get_action(action):
3434
'camera': CameraAction,
3535
'cameraRoll': CameraRollAction,
3636
'location': LocationAction,
37+
'richmenuswitch': RichMenuSwitchAction,
3738
}
3839
)
3940
return action_obj
@@ -270,3 +271,33 @@ def __init__(self, label=None, **kwargs):
270271

271272
self.type = 'location'
272273
self.label = label
274+
275+
276+
class RichMenuSwitchAction(Action):
277+
"""RichMenuSwitchAction.
278+
279+
https://developers.line.biz/en/reference/messaging-api/#richmenu-switch-action
280+
281+
This action can be configured only with rich menus.
282+
It can't be used for Flex Messages or quick replies.
283+
When you tap a rich menu associated with this action,
284+
you can switch between rich menus,
285+
and a postback event including the rich menu alias ID selected
286+
by the user is returned via a webhook.
287+
"""
288+
289+
def __init__(self, label=None, rich_menu_alias_id=None, data=None, **kwargs):
290+
"""__init__ method.
291+
292+
:param str label: Label for the action
293+
:param str rich_menu_alias_id: Rich menu alias ID to switch to.
294+
:param str data: String returned by the postback.data property
295+
of the postback event via a webhook
296+
:param kwargs:
297+
"""
298+
super(RichMenuSwitchAction, self).__init__(**kwargs)
299+
300+
self.type = 'richmenuswitch'
301+
self.label = label
302+
self.rich_menu_alias_id = rich_menu_alias_id
303+
self.data = data

linebot/models/responses.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
GenderInsight, AreaInsight, MessageInsight, ClickInsight,
2323
MessageStatistics, JobInsight,
2424
)
25-
from .rich_menu import RichMenuSize, RichMenuArea
25+
from .rich_menu import RichMenuSize, RichMenuArea, RichMenuAlias
2626

2727

2828
class BroadcastResponse(object):
@@ -194,6 +194,48 @@ def __init__(self, rich_menu_id=None, size=None, selected=None, name=None,
194194
self.areas = new_areas
195195

196196

197+
class RichMenuAliasResponse(Base):
198+
"""RichMenuAliasResponse.
199+
200+
https://developers.line.biz/en/reference/messaging-api/#get-rich-menu-alias-by-id-response
201+
"""
202+
203+
def __init__(self, rich_menu_alias_id=None, rich_menu_id=None, **kwargs):
204+
"""__init__ method.
205+
206+
:param str rich_menu_alias_id: Rich menu alias ID.
207+
:param str rich_menu_id: Rich menu ID.
208+
:param kwargs:
209+
"""
210+
super(RichMenuAliasResponse, self).__init__(**kwargs)
211+
212+
self.rich_menu_alias_id = rich_menu_alias_id
213+
self.rich_menu_id = rich_menu_id
214+
215+
216+
class RichMenuAliasListResponse(Base):
217+
"""RichMenuAliasListResponse.
218+
219+
https://developers.line.biz/en/reference/messaging-api/#get-rich-menu-alias-list-response
220+
"""
221+
222+
def __init__(self, aliases=None, **kwargs):
223+
"""__init__ method.
224+
225+
:param aliases: Array of rich menu alias objects
226+
:type areas: list[T <= :py:class:`linebot.models.RichMenuAlias`]
227+
:param kwargs:
228+
"""
229+
super(RichMenuAliasListResponse, self).__init__(**kwargs)
230+
new_aliases = []
231+
if aliases:
232+
for alias in aliases:
233+
new_aliases.append(
234+
self.get_or_new_from_json_dict(alias, RichMenuAlias)
235+
)
236+
self.aliases = new_aliases
237+
238+
197239
class MessageQuotaResponse(Base):
198240
"""MessageQuotaResponse.
199241

linebot/models/rich_menu.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,23 @@ def __init__(self, x=None, y=None, width=None, height=None, **kwargs):
124124
self.y = y
125125
self.width = width
126126
self.height = height
127+
128+
129+
class RichMenuAlias(with_metaclass(ABCMeta, Base)):
130+
"""RichMenuAlias.
131+
132+
https://developers.line.biz/en/reference/messaging-api/#create-rich-menu-alias
133+
"""
134+
135+
def __init__(self, rich_menu_alias_id=None, rich_menu_id=None, **kwargs):
136+
"""__init__ method.
137+
138+
:param string rich_menu_alias_id: Rich menu alias ID,
139+
which can be any ID, unique for each channel.
140+
:param string rich_menu_id: The rich menu ID to be associated with the rich menu alias.
141+
:param kwargs:
142+
"""
143+
super(RichMenuAlias, self).__init__(**kwargs)
144+
145+
self.rich_menu_alias_id = rich_menu_alias_id
146+
self.rich_menu_id = rich_menu_id

0 commit comments

Comments
 (0)