Skip to main content
 首页 » 编程设计

python之尝试在用 Python 编写的 Tropo 应用程序中创建简单的本地语法

2025年05月04日16del

我正在使用 Python 在 Tropo 中创建一个应用程序,我想知道我是否可以创建一个本地的小语法。我已经阅读了有关外部语法 SRGS 和 GRXML 的内容,但我可以在代码中使用 Python 列表创建一个吗?以下是我正在尝试做的事情。

food = ['cheeseburger', 'hot dog', 'salad'] 
 
ask("What food would you like?", 
    #{'choices': "cheeseburger, hot dog, salad", 
    {'choices': food, 
    'attempts':3, 
    'onChoice': fill, 
    'onBadChoice': nomatch, 
    'onTimeout': noinput }) 

上面的代码编译通过了,但是遇到这个问题就挂了。

请您参考如下方法:

如果您希望 Tropo 询问调用者“您想要什么食物”,并尝试 3 次尝试使用“食物”中的一个短语来回答,请尝试发送以下 json 响应:

{ 
  "tropo": [ 
    { 
      "ask": { 
        "choices": { 
          "value": "cheeseburger, hot dog, salad", 
          "mode": "speech", 
          "terminator": "#" 
        }, 
        "attempts": 3, 
        "name": "foodchoice", 
        "recognizer": null, 
        "required": null, 
        "say": { 
          "value": "What food would you like" 
        } 
      } 
    }, 
    { 
      "on": { 
        "event": "continue", 
        "name": null, 
        "next": "/fill", 
        "required": true 
      } 
    }, 
    { 
      "on": { 
        "event": "incomplete", 
        "name": null, 
        "next": "/noinput", 
        "required": true 
      } 
    }, 
    { 
      "on": { 
        "event": "error", 
        "name": null, 
        "next": "/nomatch", 
        "required": true 
      } 
    } 
  ] 
} 

要准确回答您的问题,您需要了解 Python Tropo 库。我不熟悉 python 库,但 Tropo 的 Java 和 NodeJS 库似乎已经过时了......所以如果 Python 库过时了,那么你将不得不做更多的工作来构建和返回这个 JSON 对象。