Как создать многоуровневую клавиатуру Viber - PullRequest
0 голосов
/ 05 августа 2020

Я пытаюсь создать клавиатуру для бота Viber, используя viber-bot- python и следующий код:

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'
        }
    ]
}

И ожидаю, что получится следующая структура:

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

Но я получаю следующее исключение:

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)]

Я читал topi c про дизайн клавиатуры , но это не помогает. Что в нем не так и как правильно собрать клавиатуру для Viber Bot?

1 Ответ

0 голосов
/ 05 августа 2020

Возможно вы неправильно поняли изображение в клавиатуре топи c. Это означает, что одна кнопка может уместиться в этой сетке (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'
        }
    ]
}
...