如何生成多级Viber键盘

时间:2020-08-05 14:24:36

标签: python viber viber-api viber-bot viber-bot-python

我正在尝试使用viber-bot-python和以下代码为Viber机器人构建键盘:

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

我已经阅读了有关keyboard design的主题,但没有帮助。 它有什么问题,以及如何为Viber Bot正确构建键盘?

1 个答案:

答案 0 :(得分:0)

keyboard-design主题中,您可能误解了图像。这意味着单个按钮可以放在该网格(6x2)中。

keyboard-design

因此,如果您需要构建如下菜单:

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

然后为每个按钮设置每个按钮应放置的位置。 前4个按钮-将占据 3(共6个)列,后5个按钮- 6(共6个)。 每个按钮可以占用 1行(如果需要小按钮)或 2行(如果需要大按钮)。三行按钮是不可能的。

现在您的代码应如下所示:

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