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

#リッチメニューの作成手順
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/331630/e0b55891-7834-2505-897c-d50c1b8b1057.png)

```python: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
```
**１．リッチメニュー画像の詳細を定義**
-
```python
# 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](https://developers.line.biz/en/reference/messaging-api/#size-object)**
画像サイズ（pixels）は以下のみ対応
2500x1686, 2500x843, 1200x810, 1200x405, 800x540, 800x270

**chat_bar_text**
下図のようにチャットバーに表示される
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/331630/668a7f55-1cb0-b038-816c-17cc64ec2714.png)

**[areas](https://developers.line.biz/en/reference/messaging-api/#area-object)**
action範囲を各ボタンごとに**RichMenuArea**を設定する

＜RichMenuArea＞
・bunds: [範囲指定](https://developers.line.biz/en/reference/messaging-api/#bounds-object):
・action: [アクション指定](https://developers.line.biz/en/reference/messaging-api/#action-objects)

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

```python
richMenuId = line_bot_api.create_rich_menu(rich_menu=rich_menu_to_create)
```

**２．リッチメニュー画像をアップロード**
-
```python
# 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)
```

**３．リッチメニューをデフォルトに設定**
-
```python
# set the default rich menu
    line_bot_api.set_default_rich_menu(richMenuId)
```

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


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

```python
rich_menu_list = line_bot_api.get_rich_menu_list()
```

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

```python
line_bot_api.delete_rich_menu(rich_menu.rich_menu_id)
```


#引用
[【LINE Developers】リッチメニューを使う](https://developers.line.biz/ja/docs/messaging-api/using-rich-menus/)