[LINE Messaging API] Create a rich menu in Python

Rich menu creation procedure

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 rich menu image details **

# 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 Image size (pixels) supports only the following 2500x1686, 2500x843, 1200x810, 1200x405, 800x540, 800x270

chat_bar_text Displayed in the chat bar as shown below image.png

areas Set ** RichMenuArea ** for each button in the action range


RichMenuArea
-Bunds: Specify range: ・ Action: Specify action

create rich menu API Call API and create rich menu

richMenuId = line_bot_api.create_rich_menu(rich_menu=rich_menu_to_create)

** 2. Upload rich menu image **

# 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 rich menu as default **

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

** Other APIs **

** Get rich menu list ** Get the list that contains the rich menu ID

rich_menu_list = line_bot_api.get_rich_menu_list()

** Remove rich menu ** Specify the rich menu ID to delete

line_bot_api.delete_rich_menu(rich_menu.rich_menu_id)

Quote

[LINE Developers] Use rich menus

Recommended Posts

[LINE Messaging API] Create a rich menu in Python
[LINE Messaging API] Create a BOT that connects with someone with Python
Create a function in Python
Create a dictionary in Python
[LINE Messaging API] Create parrot return BOT with Python
Create a DI Container in Python
Create a binary file in Python
Create Gmail in Python without API
Create a Kubernetes Operator in Python
Create a random string in Python
Create a LINE Bot in Django
[LINE Messaging API] Issue channel access token v2.1 in Python
I made a Chatbot using LINE Messaging API and Python
[Python / Django] Create a web API that responds in JSON format
Create a simple GUI app in Python
Create a JSON object mapper in Python
A script that makes it easy to create rich menus with the LINE Messaging API
[Python] [LINE Bot] Create a parrot return LINE Bot
[GPS] Create a kml file in Python
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
Create a Vim + Python test environment in 1 minute
Create a GIF file using Pillow in Python
Create a LINE BOT with Minette for Python
I want to create a window in Python
Create a standard normal distribution graph in Python
How to create a JSON file in Python
Create a virtual environment with conda in Python
Make a rock-paper-scissors game in one line (python)
Create a simple momentum investment model in Python
Create a new page in confluence with Python
Create a datetime object from a string in Python (Python 3.3)
Create a package containing global commands in Python
How to create a Rest Api in Django
Create a MIDI file in Python using pretty_midi
Create a loop antenna pattern in Python in KiCad
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Create a Python module
Create SpatiaLite in Python
Evernote API in Python
Create a Python environment
C API in Python 3
Specify a subcommand as a command line argument in Python
Playing with a user-local artificial intelligence API in Python
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
In Python, create a decorator that dynamically accepts arguments Create a decorator
Create a fake Minecraft server in Python with Quarry
Hit Mastodon's API in Python
Take a screenshot in Python
Create a Wox plugin (Python)
Fizzbuzz in Python (in one line)
Create gif video in Python
Try LINE Notify in Python
Make a bookmarklet in Python
Create a python numpy array
Blender Python API in Houdini (Python 3)
Draw a heart in Python
Create a directory with python
I tried to create API list.csv in Python from swagger.yaml
Create a local scope in Python without polluting the namespace
Create a list in Python with all followers on twitter