如何生成多级Viber键盘

2024-06-28 19:32:07 发布

您现在位置: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机器人的键盘


Tags: columnstextmessagestatus机器人键盘replyrows
1条回答
网友
1楼 · 发布于 2024-06-28 19:32:07

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

keyboard-design

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

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

然后,对于每个按钮,您需要设置每个按钮应占据的位置。 前4个按钮-将使用3列(共6列),最后第5个按钮将使用6列(共6列)。 每个按钮可以选择1行(如果您想要小按钮)或2行(如果您想要大按钮)。无法使用包含3行的按钮

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

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

相关问题 更多 >