2

I want to create a conversational alexa skill using Flask-Ask and python 3. But when I intentionally omit the required slot. Alexa doesn't prompt me anything. It goes to FallBackIntent directly. I've set the prompt and corresponding answer for it in the web console and I've added necessary code to check if dialogState!= 'COMPLETED' and return delegate(). But it just go to FallBackIntent directly.

Below is a screenshot of my settings for the slot on the web console

This is a screenshot of my web console setting about the slot

Below is my entire code(I use ngrok to map to my localhost):

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)

Could anyone tell me what is wrong with my code?

Thank you!

0 Answers0