如何在python中获取alexa技能槽值

时间:2018-04-08 03:37:44

标签: python alexa alexa-skills-kit alexa-skill alexa-voice-service

我正在尝试获取插槽值并稍后访问它,但我无法获得该值。你能帮我吗

这是我坚持的路线。如何从插槽中获取响应并将其保存到变量中。

textoutput = intent['slots'].['slotsname'].['value']

代码:

import logging

from random import randint
from flask import Flask, render_template
from flask_ask import Ask, statement, question, session

app = Flask(__name__)
ask = Ask(app, "/")

logging.getLogger("flask_ask").setLevel(logging.DEBUG)

@ask.intent("MAYBE")

def next_round():
    textoutput = intent['slots'].['slotsname'].['value']
    textoutput1 = 'working'
    textoutput2 = 'not working'
    if textoutput ='ftw':
        schedule_msg4 = render_template('CONNECTION2', schedule4=textoutput1)

    else:
        schedule_msg4 = render_template('CONNECTION2', schedule4=textoutput2)

    return question(schedule_msg4)

if __name__ == '__main__':
    app.run(debug=True)

2 个答案:

答案 0 :(得分:0)

要选择Python列表中的元素,只需使用listName['key'] 对于嵌套列表,请使用另一组括号:listName['key1']['key2']

所以不要将每个[].分开 所以你的第一行应该是:

textoutput = intent['slots']['slotKey']['value']

答案 1 :(得分:0)

如果您已定义MAYBE意图包含名为answer的广告位,则请求将如下所示。 money是您要在代码中使用的值:

"request": {
  "intent": {
    "name": "MAYBE",
    "slots": {
      "answer": {
        "name": "answer",
        "value": "money"
      }
    }
  }
  ...
}

然后,你需要调整你的函数定义:

@ask.intent("MAYBE")    
def next_round(answer):    
    textoutput = "You said: {}".format(answer)    
    return statement(textoutput) # "You said: money"

Flask-ask将负责从请求中提取值。 查看documentation