15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【LINE Messaging API】Pythonでリッチメニューを作成

Last updated at Posted at 2019-12-02

#リッチメニューの作成手順
image.png

createRichmenu.py
def createRichmenu():
    result = False
    try:
        # define a new richmenu
        rich_menu_to_create = RichMenu(
            size = RichMenuSize(width=1200, height=405),
            selected = True,
            name = 'richmenu for randomchat',
            chat_bar_text = 'TAP HERE',
            areas=[
                RichMenuArea(
                    bounds=RichMenuBounds(x=0, y=0, width=480, height=405),
                    action=MessageAction(text=config.REMOVE)
                ),
                RichMenuArea(
                    bounds=RichMenuBounds(x=480, y=0, width=720, height=405),
                    action=MessageAction(text=config.NEXT)
                )
            ]
        )
        richMenuId = line_bot_api.create_rich_menu(rich_menu=rich_menu_to_create)

        # upload an image for rich menu
        path = 'image path for richmenu'
        
        with open(path, 'rb') as f:
            line_bot_api.set_rich_menu_image(richMenuId, "image/jpeg", f)
        
        # set the default rich menu
        line_bot_api.set_default_rich_menu(richMenuId)

        result = True

    except Exception:
        result = False


    return result

1.リッチメニュー画像の詳細を定義

# define a new richmenu
    rich_menu_to_create = RichMenu(
        size = RichMenuSize(width=1200, height=405),
        selected = True,
        name = 'richmenu for randomchat',
        chat_bar_text = 'TAP HERE',
        areas=[
            RichMenuArea(
                bounds=RichMenuBounds(x=0, y=0, width=480, height=405),
                action=MessageAction(text=config.REMOVE)
            ),
            RichMenuArea(
                bounds=RichMenuBounds(x=480, y=0, width=720, height=405),
                action=MessageAction(text=config.NEXT)
            )
        ]
    )
    richMenuId = line_bot_api.create_rich_menu(rich_menu=rich_menu_to_create)

size
画像サイズ(pixels)は以下のみ対応
2500x1686, 2500x843, 1200x810, 1200x405, 800x540, 800x270

chat_bar_text
下図のようにチャットバーに表示される
image.png

areas
action範囲を各ボタンごとにRichMenuAreaを設定する

<RichMenuArea>
・bunds: 範囲指定:
・action: アクション指定

create rich menu API
APIを呼び、リッチメニューを作成する

richMenuId = line_bot_api.create_rich_menu(rich_menu=rich_menu_to_create)

2.リッチメニュー画像をアップロード

# upload an image for rich menu
    path = 'image path for richmenu'
    
    with open(path, 'rb') as f:
        line_bot_api.set_rich_menu_image(richMenuId, "image/jpeg", f)

3.リッチメニューをデフォルトに設定

# set the default rich menu
    line_bot_api.set_default_rich_menu(richMenuId)

#####以上で、リッチメニューの設定が完了する

他のAPI

リッチメニューリストを取得
リッチメニューIDが格納されたリストを取得する

rich_menu_list = line_bot_api.get_rich_menu_list()

リッチメニューを削除
削除するリッチメニューIDを指定する

line_bot_api.delete_rich_menu(rich_menu.rich_menu_id)

#引用
【LINE Developers】リッチメニューを使う

15
12
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?