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

2024-09-25 00:31:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python在Tropo中创建一个应用程序,我想知道是否可以创建一个本地的小语法。我读过关于外部语法SRGSGRXML的文章,但是我可以在代码中使用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 })

上面的代码可以编译,但当它遇到这个问题时就挂断了。你知道吗


Tags: 代码应用程序列表food文章语法askchoices
2条回答

如果您想让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
      }
    }
  ]
}

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

根据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是一个字符串,而不是一个列表,因此您可以这样做:

food = "cheeseburger, hot dog, salad"

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

相关问题 更多 >