首页 > 解决方案 > 关于使用 Flask-Ask 和 python 创建对话式 alexa 技能的困惑:当缺少所需的插槽时,它会转到 FallBackIntent

问题描述

我想使用 Flask-Ask 和 python 3 创建一个对话式 alexa 技能。但是当我故意省略所需的插槽时。Alexa没有提示我任何东西。它直接进入 FallBackIntent。我已经在 Web 控制台中为其设置了提示和相应的答案,并且我添加了必要的代码来检查是否 dialogState!= 'COMPLETED' 并返回委托()。但它只是直接转到 FallBackIntent。

下面是我在 Web 控制台上设置插槽的屏幕截图

这是我关于插槽的 Web 控制台设置的屏幕截图

下面是我的整个代码(我使用 ngrok 映射到我的本地主机):

from flask import Flask
from flask_ask import Ask, statement, question, session, convert_errors, delegate
import random
import logging


app = Flask(__name__)
ask = Ask(app, "/ask_demo") 
app.logger.setLevel(logging.DEBUG)
logger = app.logger
facts = ['A year on Mercury is just 88 days long.',
  'Despite being farther from the Sun, Venus experiences higher temperatures than Mercury.',
  'Venus rotates counter-clockwise, possibly because of a collision in the past with an asteroid.',
  'On Mars, the Sun appears about half the size as it does on Earth.',
  'Earth is the only planet not named after a god.',
  'Jupiter has the shortest day of all the planets.',
  'The Milky Way galaxy will collide with the Andromeda Galaxy in about 5 billion years.',
  'The Sun contains 99.86% of the mass in the Solar System.',
  'The Sun is an almost perfect sphere.',
  'A total solar eclipse can happen once every 1 to 2 years. This makes them a rare event.',
  'Saturn radiates two and a half times more energy into space than it receives from the sun.',
  'The temperature inside the Sun can reach 15 million degrees Celsius.',
  'The Moon is moving approximately 3.8 cm away from our planet every year.',]

fact_index_by_planets =dict()
fact_index_by_planets['mercury'] = [0]
fact_index_by_planets['venus'] = [1,2]
fact_index_by_planets['mars'] = [3]
fact_index_by_planets['earth'] = [4]
fact_index_by_planets['jupiter'] = [5]
fact_index_by_planets['sun'] = [7,8,9,11]
fact_index_by_planets['saturn'] = [10]
fact_index_by_planets['moon'] = [12]

sample_questions = 'What planet do you ask for, you can say a fact about Mercury, Venus, Mars, Earth, Jupiter, Sun, ' \
                   'Saturn or Moon?'

def get_dialog_state():
    return session['dialogState']

def get_fact(planet):

    planet = planet.lower()
    indices = fact_index_by_planets[planet]
    index = random.choice(indices)
    return facts[index]


# @app.route('/',methods=['GET'])
# def homepage():
#     return "hi this is the homepage!"

@ask.launch
def start_skill():
    welcome_message = 'Hello there, Welcome! {}'.format(sample_questions) 
    logger.debug('Launch')
    return question(welcome_message)

@ask.intent("FactIntent")
def tell_fact(planet):
    dialog_state = get_dialog_state()
    if dialog_state != 'COMPLETED':
        return delegate()
    logger.debug("The planet asked for is: {}".format(planet))
    if convert_errors and 'planet' in convert_errors:
        return question("Can you please repeat the planet name, you can say Mercury, Venus, Mars, Earth, Jupiter, Sun, Saturn or Moon?")
    fact_msg = get_fact(planet)
    return question(fact_msg+"Anything else?")

@ask.intent("AMAZON.StopIntent")
def stop(str):
    logger.debug('Stop')

    return statement('Thank you for using me! See you')

@ask.intent('AMAZON.FallbackIntent')
def fall_back():
    logger.debug('Not Understood')
    return question("Sorry! I don't understand Could you repeat that?")

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

谁能告诉我我的代码有什么问题?

谢谢!

标签: pythonpython-3.xalexa-skillflask-ask

解决方案


推荐阅读