简体   繁体   English

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

[英]Trying to create a simple local grammar in Tropo application written in Python

I'm creating an application in Tropo using Python and I was wondering if I could create a small grammar that is local.我正在使用 Python 在 Tropo 中创建一个应用程序,我想知道是否可以创建一个本地的小语法。 I've read about external grammars SRGS and GRXML but can I create one using a Python list right in the code.我已经阅读了外部语法SRGSGRXML但我可以在代码中使用 Python 列表创建一个。 Below is what I'm trying to do.下面是我正在尝试做的。

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

The above code compiles but it hangs up when it gets to this question.上面的代码可以编译,但是在遇到这个问题时挂断了。

If you want Tropo to ask the caller "What food would you like", and give them 3 attempts to answer with one of the phrases in 'food', try sending the following json response:如果您希望 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
      }
    }
  ]
}

To answer your question exactly, you would need to understand the Python Tropo library.要准确回答您的问题,您需要了解 Python Tropo 库。 I'm not familiar with the python one but Tropo's Java and NodeJS libraries seem to be out of date...so if the Python one is too then you will have to do some more work to build and return this JSON object.我对 python 不熟悉,但 Tropo 的 Java 和 NodeJS 库似乎已经过时了......所以如果 Python 也是如此,那么你将不得不做更多的工作来构建和返回这个 JSON 对象。

According to the example in the Tropo docs:根据 Tropo 文档中的示例:

result = ask("What's your favorite color? Choose from red, blue or green.", {
   "choices":"red, blue, green"})
say("You said " + result.value)
log("They said " + result.value)

choices is a string, not a list, so you'd want to do this instead: choices是一个字符串,而不是一个列表,所以你想要这样做:

food = "cheeseburger, hot dog, salad"

ask("What food would you like?",
    {'choices': food,
    'attempts':3,
    'onChoice': fill,
    'onBadChoice': nomatch,
    'onTimeout': noinput })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM