pythonviberviber-apiviber-botviber-bot-python

How generate multilevel Viber keyboard


I'm trying to build keyboard for Viber bot using viber-bot-python and following code:

keyboard = {
    "DefaultHeight": True,
    "BgColor": self.background_color,
    "Type": "keyboard",
    "Buttons": [
        {
            "Columns": 2,
            "Rows": 3,
            "BgColor": "#e6f5ff",
            "ActionType": 'reply',
            "ActionBody": '1',
            "ReplyType": "message",
            "Text": '1'
        },
        {
            "Columns": 2,
            "Rows": 3,
            "BgColor": "#e6f5ff",
            "ActionType": 'reply',
            "ActionBody": '2',
            "ReplyType": "message",
            "Text": '2'
        },
        {
            "Columns": 2,
            "Rows": 3,
            "BgColor": "#e6f5ff",
            "ActionType": 'reply',
            "ActionBody": '3',
            "ReplyType": "message",
            "Text": '3'
        },
        {
            "Columns": 2,
            "Rows": 3,
            "BgColor": "#e6f5ff",
            "ActionType": 'reply',
            "ActionBody": '4',
            "ReplyType": "message",
            "Text": '4'
        },
        {
            "Columns": 1,
            "Rows": 3,
            "BgColor": "#e6f5ff",
            "ActionType": 'reply',
            "ActionBody": '5',
            "ReplyType": "message",
            "Text": '5'
        }
    ]
}

And expect that will get following structure:

Select button:
[1] [2]
[3] [4]
[  5  ]

But I'm getting the following exception:

File "\venv\lib\site-packages\viberbot\api\message_sender.py", line 53, in _post_request
    raise Exception(u"failed with status: {0}, message: {1}".format(result['status'], result['status_message']))
Exception: failed with status: 3, message: keyboard is not valid. [numeric instance is greater than the required maximum (maximum: 2, found: 3)]

I've read topic about keyboard design, but it doesn't help. What is wrong with it and how to correctly build the keyboard for Viber Bot?


Solution

  • Probably you misunderstood image in keyboard-design topic. It means that a single button can fit place in that grid (6x2).

    keyboard-design

    So if you need to build menu like:

    Select button:
    [1] [2]
    [3] [4]
    [  5  ]
    

    Then for each button you need to set how much place each button should take. First 4 buttons - will take 3 (of 6) columns, and the last 5th - 6 (of 6). And each button can take 1 row (if you want small button) or 2 rows (if you want them big). Button with 3 rows is not possible.

    And now your code should look like following:

    keyboard = {
        "DefaultHeight": True,
        "BgColor": self.background_color,
        "Type": "keyboard",
        "Buttons": [
            {
                "Columns": 3,
                "Rows": 1,
                "BgColor": "#e6f5ff",
                "ActionType": 'reply',
                "ActionBody": '1',
                "ReplyType": "message",
                "Text": '1'
            },
            {
                "Columns": 3,
                "Rows": 1,
                "BgColor": "#e6f5ff",
                "ActionType": 'reply',
                "ActionBody": '2',
                "ReplyType": "message",
                "Text": '2'
            },
            {
                "Columns": 3,
                "Rows": 1,
                "BgColor": "#e6f5ff",
                "ActionType": 'reply',
                "ActionBody": '3',
                "ReplyType": "message",
                "Text": '3'
            },
            {
                "Columns": 3,
                "Rows": 1,
                "BgColor": "#e6f5ff",
                "ActionType": 'reply',
                "ActionBody": '4',
                "ReplyType": "message",
                "Text": '4'
            },
            {
                "Columns": 6,
                "Rows": 1,
                "BgColor": "#e6f5ff",
                "ActionType": 'reply',
                "ActionBody": '5',
                "ReplyType": "message",
                "Text": '5'
            }
        ]
    }