【问题标题】:How generate multilevel Viber keyboard如何生成多级 Viber 键盘
【发布时间】:2020-08-05 14:24:36
【问题描述】:

我正在尝试使用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 的键盘?

【问题讨论】:

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


    【解决方案1】:

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

    所以如果你需要建立这样的菜单:

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

    【讨论】:

      猜你喜欢
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多