Skip to content

Commit 6dc0268

Browse files
authored
Add validate rich menu object API (#391)
1 parent ff369aa commit 6dc0268

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

README.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,27 @@ https://developers.line.biz/en/reference/messaging-api/#get-rich-menu
346346
rich_menu = line_bot_api.get_rich_menu(rich_menu_id)
347347
print(rich_menu.rich_menu_id)
348348
349+
validate\_rich\_menu\_object(self, rich\_menu, timeout=None)
350+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
351+
352+
Validate a rich menu object.
353+
You can verify that a rich menu object is valid as a request body for creating rich menu.
354+
355+
https://developers.line.biz/ja/reference/messaging-api/#validate-rich-menu-object
356+
357+
.. code:: python
358+
359+
rich_menu_to_validate = RichMenu(
360+
size=RichMenuSize(width=2500, height=843),
361+
selected=False,
362+
name="Nice richmenu",
363+
chat_bar_text="Tap here",
364+
areas=[RichMenuArea(
365+
bounds=RichMenuBounds(x=0, y=0, width=2500, height=843),
366+
action=URIAction(label='Go to line.me', uri='https://line.me'))]
367+
)
368+
line_bot_api.validate_rich_menu_object(rich_menu=rich_menu_to_create)
369+
349370
create\_rich\_menu(self, rich\_menu, timeout=None)
350371
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
351372

linebot/api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,24 @@ def get_rich_menu_alias_list(self, timeout=None):
739739
)
740740
return RichMenuAliasListResponse.new_from_json_dict(response.json)
741741

742+
def validate_rich_menu_object(self, rich_menu, timeout=None):
743+
"""Call validate rich menu object API.
744+
745+
https://developers.line.biz/ja/reference/messaging-api/#validate-rich-menu-object
746+
747+
:param rich_menu: Inquired to validate a rich menu object.
748+
:type rich_menu: T <= :py:class:`linebot.models.rich_menu.RichMenu`
749+
:param timeout: (optional) How long to wait for the server
750+
to send data before giving up, as a float,
751+
or a (connect timeout, read timeout) float tuple.
752+
Default is self.http_client.timeout
753+
:type timeout: float | tuple(float, float)
754+
"""
755+
self._post(
756+
'/v2/bot/richmenu/validate', data=rich_menu.as_json_string(),
757+
timeout=timeout
758+
)
759+
742760
def create_rich_menu(self, rich_menu, timeout=None):
743761
"""Call create rich menu API.
744762

linebot/async_api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,24 @@ async def get_rich_menu_alias_list(self, timeout=None):
796796
response = await self._get("/v2/bot/richmenu/alias/list", timeout=timeout)
797797
return RichMenuAliasListResponse.new_from_json_dict((await response.json))
798798

799+
async def validate_rich_menu_object(self, rich_menu, timeout=None):
800+
"""Call validate rich menu object API.
801+
802+
https://developers.line.biz/ja/reference/messaging-api/#validate-rich-menu-object
803+
804+
:param rich_menu: Inquired to create a rich menu object.
805+
:type rich_menu: T <= :py:class:`linebot.models.rich_menu.RichMenu`
806+
:param timeout: (optional) How long to wait for the server
807+
to send data before giving up, as a float,
808+
or a (connect timeout, read timeout) float tuple.
809+
Default is self.async_http_client.timeout
810+
:type timeout: float | tuple(float, float)
811+
"""
812+
await self._post(
813+
"/v2/bot/richmenu/validate", data=rich_menu.as_json_string(),
814+
timeout=timeout
815+
)
816+
799817
async def create_rich_menu(self, rich_menu, timeout=None):
800818
"""Call create rich menu API.
801819

tests/api/test_rich_menu.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,46 @@ def test_get_rich_menu(self):
114114
self.assertEqual(rich_menu.areas[0].action.type, 'postback')
115115
self.assertEqual(rich_menu.areas[0].action.data, 'action=buy&itemid=123')
116116

117+
@responses.activate
118+
def test_validate_rich_menu_object(self):
119+
responses.add(
120+
responses.POST,
121+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/validate',
122+
json={}, status=200
123+
)
124+
125+
rich_menu = RichMenu(
126+
size=RichMenuSize(
127+
width=2500,
128+
height=1686
129+
),
130+
selected=False,
131+
name="nice richmenu",
132+
chatBarText="touch me",
133+
areas=[
134+
RichMenuArea(
135+
RichMenuBounds(
136+
x=0,
137+
y=0,
138+
width=833,
139+
height=843
140+
),
141+
URITemplateAction(
142+
uri='https://line.me/R/nv/location/'
143+
)
144+
)
145+
]
146+
)
147+
148+
self.tested.validate_rich_menu_object(rich_menu)
149+
150+
request = responses.calls[0].request
151+
self.assertEqual(request.method, 'POST')
152+
self.assertEqual(
153+
request.url,
154+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/validate'
155+
)
156+
117157
@responses.activate
118158
def test_create_rich_menu(self):
119159
responses.add(

0 commit comments

Comments
 (0)