昨天嘗試過了 Richmenu ,相信各位也都感覺到了它可以方便我們的許多操作了吧。但是如果想要放在 Richmenu 的操作較多(ex. 10個以上),只有這樣小小一塊的 Richmenu 感覺塞不太下,勉強塞下了又顯得每個區塊非常小、非常擁擠,這時候又該怎麼辦呢?
這時候 Line 官方提供了一個特殊的 Action ,叫做 RichMenuSwitchAction ,就是可以將我們的選單在多個 RichMenu 之間切換,這樣就可以讓我們不用再要塞多少個操作糾結了,只要做好切換,想塞多少就塞多少,真的是非常的美好。
那麼我們具體要如何實現呢?
首先,我們依舊還是要準備好另一張圖片,最好是有明顯區別的圖片(這就是我昨天為什麼要生出兩張的用意),接著我們改寫一下昨天的設定工具,改成如下所示。
import argparse
import os
from linebot import LineBotApi
from linebot.models.actions import MessageAction, PostbackAction, URIAction, DatetimePickerAction, RichMenuSwitchAction
from linebot.models.rich_menu import RichMenu, RichMenuAlias, RichMenuArea, RichMenuBounds, RichMenuSize
rich_menu_list = [
{
"richmenu_alias_name": "my-richmenu-1",
"richmenu_image": "resource/CMYK.jpg",
"richmenu_content": RichMenu(
size=RichMenuSize(width=2500, height=1686),
selected=True,
name="my-richmenu-1",
chat_bar_text="選單",
areas=[
# 左上
RichMenuArea(
RichMenuBounds(x=0, y=0, width=1250, height=843),
MessageAction(label="左上按鈕", text="左上按鈕")
),
# 右上
RichMenuArea(
RichMenuBounds(x=1251, y=0, width=1250, height=843),
MessageAction(label="右上按鈕", text="右上按鈕")
),
# 左下
RichMenuArea(
RichMenuBounds(x=0, y=844, width=1250, height=843),
MessageAction(label="左下按鈕", text="左下按鈕")
),
# 右下
RichMenuArea(
RichMenuBounds(x=1251, y=844, width=1250, height=843),
RichMenuSwitchAction(
label="右下按鈕", rich_menu_alias_id="my-richmenu-2", data="右下按鈕")
)
]
)
},
{
"richmenu_alias_name": "my-richmenu-2",
"richmenu_image": "resource/RGBK.jpg",
"richmenu_content": RichMenu(
size=RichMenuSize(width=2500, height=1686),
selected=True,
name="my-richmenu-2",
chat_bar_text="選單",
areas=[
# 左上
RichMenuArea(
RichMenuBounds(x=0, y=0, width=1250, height=843),
MessageAction(label="左上按鈕", text="左上按鈕")
),
# 右上
RichMenuArea(
RichMenuBounds(x=1251, y=0, width=1250, height=843),
MessageAction(label="右上按鈕", text="右上按鈕")
),
# 左下
RichMenuArea(
RichMenuBounds(x=0, y=844, width=1250, height=843),
MessageAction(label="左下按鈕", text="左下按鈕")
),
# 右下
RichMenuArea(
RichMenuBounds(x=1251, y=844, width=1250, height=843),
RichMenuSwitchAction(
label="右下按鈕", rich_menu_alias_id="my-richmenu-1", data="右下按鈕")
)
]
)
}
]
if __name__ == "__main__":
line_bot_api = LineBotApi(os.environ['CHANNEL_ACCESS_TOKEN'])
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--create", action="store_true",
help="Create a new RichMenu")
parser.add_argument("-g", "--get", action="store_true",
help="Get default RichMenu")
parser.add_argument("-l", "--list", action="store_true",
help="Get all RichMenu")
parser.add_argument("-d", "--delete", action="store_true",
help="Delete default RichMenu")
args = parser.parse_args()
if args.create:
flag = False
for richmenu in rich_menu_list:
print(richmenu["richmenu_alias_name"])
richmenu_id = line_bot_api.create_rich_menu(
richmenu["richmenu_content"])
with open(richmenu["richmenu_image"], "rb") as fp:
line_bot_api.set_rich_menu_image(
richmenu_id, content_type="image/jpeg", content=fp)
line_bot_api.create_rich_menu_alias(RichMenuAlias(
richmenu["richmenu_alias_name"], richmenu_id))
if flag is False:
flag = True
line_bot_api.set_default_rich_menu(richmenu_id)
elif args.get:
print(line_bot_api.get_rich_menu(line_bot_api.get_default_rich_menu()))
elif args.list:
for r in line_bot_api.get_rich_menu_list():
print(r)
elif args.delete:
richmenu_id = line_bot_api.get_default_rich_menu()
richmenu_alias_list = line_bot_api.get_rich_menu_alias_list().as_json_dict()[
"aliases"]
alias = {index: data["richMenuId"]
for index, data in enumerate(richmenu_alias_list)}
if richmenu_id in alias.values():
line_bot_api.delete_rich_menu_alias(line_bot_api.get_rich_menu_alias(
richmenu_alias_list[list(alias.values()).index(richmenu_id)]["richMenuAliasId"]))
line_bot_api.delete_rich_menu(richmenu_id)
只要將兩個 RichMenu 的其中一個 Button 設定為 RichMenuSwitchAction ,並且都指定為另一個 RichMenu 的 Alias ID 就可以達成 RichMenu 切換了。
就算之後兩個 RichMenu 還是不夠,那就放進更多個 Richmenu ,然後設定好如何切換即可, Line 一個 ChatBot 可以有 1000 個 RichMenuAlias ,應該足夠滿足全部人的使用。
那麼就先到這邊, RichMenu 搭配 RichMenuSwitchAction 可以把 Line 的選單放入更多方便使用者的操作,提高使用者的體驗,可惜不能將 CameraAction 放在 RichMenu ,不然我就可以讓它常駐在下方了。
大家掰~掰~