从 amazon.firstname alexa Skill dev

如何解决从 amazon.firstname alexa Skill dev

我正在以此为基础开发我的第一个 alexa 技能 https://medium.com/crowdbotics/how-to-build-a-custom-amazon-alexa-skill-step-by-step-my-favorite-chess-player-dcc0edae53fb

python 写的 lambda 函数一个预定义的带有名称的槽中使用来自 alexa 的输入,然后将对应的信息读回槽中的名称。这限制了您提供与预定义名称匹配的信息库。非常适合实际互动,但不是我所需要的。

我正在研究如何使用 amazon.firstname 槽来接收演讲者给出的任何名字并回读一个句子。第一步是返回一个固定的句子,但想将其更改为来自更大库的随机句子。

我已经将 alexa.firstname 定义为技能的插槽类型,但需要在这里更改一些代码

#------------------------------Part1--------------------------------
# In this part we define a list that contains the player names,and 
# a dictionary with player biographies
Player_LIST = ["john","steve","daddy","mummy"]

Player_BIOGRAPHY = {"john":"peeee-you,john smells like a stinky pair of week old socks that have been left out in the rain","steve":"Ughhhh,steve smells just like an elephant's bum that has been sweltering in the desert for a month","mummy":"Mummy smells beautiful,like the scent of freshly cut roses or sweet perfume","daddy":"Oh wow,the worst. He smells like a combination of burnt popcorn and rotting fish combined with sewer water and stinky armpits"}
#------------------------------Part2--------------------------------
# Here we define our Lambda function and configure what it does when 
# an event with a Launch,Intent and Session End Requests are sent. # The Lambda function responses to an event carrying a particular 
# Request are handled by functions such as on_launch(event) and 
# intent_scheme(event).
def lambda_handler(event,context):
    if event['session']['new']:
        on_start()
    if event['request']['type'] == "LaunchRequest":
        return on_launch(event)
    elif event['request']['type'] == "IntentRequest":
        return intent_scheme(event)
    elif event['request']['type'] == "SessionEndedRequest":
        return on_end()
#------------------------------Part3--------------------------------
# Here we define the Request handler functions
def on_start():
    print("Session Started.")

def on_launch(event):
    onlunch_MSG = "Hi,welcome to the Stinkbot Alexa Skill. My favourite players are: " + ','.join(map(str,Player_LIST)) + ". "\
    "If you would like to hear more about a particular player,you Could say for example: Does daddy smell?"
    reprompt_MSG = "Do you want to hear more about a particular player?"
    card_TEXT = "Pick a player."
    card_TITLE = "Choose player."
    return output_json_builder_with_reprompt_and_card(onlunch_MSG,card_TEXT,card_TITLE,reprompt_MSG,False)

def on_end():
    print("Session Ended.")
#-----------------------------Part3.1-------------------------------
# The intent_scheme(event) function handles the Intent Request. 
# Since we have a few different intents in our skill,we need to 
# configure what this function will do upon receiving a particular 
# intent. This can be done by introducing the functions which handle 
# each of the intents.
def intent_scheme(event):
    
    intent_name = event['request']['intent']['name']

    if intent_name == "playerBio": <<<<<<<<<<<<<<<<<<<<<<<
        return player_bio(event)        
    elif intent_name in ["AMAZON.NoIntent","AMAZON.StopIntent","AMAZON.CancelIntent"]:
        return stop_the_skill(event)
    elif intent_name == "AMAZON.HelpIntent":
        return assistance(event)
    elif intent_name == "AMAZON.FallbackIntent":
        return fallback_call(event)
#---------------------------Part3.1.1-------------------------------
# Here we define the intent handler functions
def player_bio(event):
    name=event['request']['intent']['slots']['player']['value']
    player_list_lower=[w.lower() for w in Player_LIST]
    if name.lower() in player_list_lower:
        reprompt_MSG = "Do you want to hear more about a particular player?"
        card_TEXT = "You've picked " + name.lower()
        card_TITLE = "You've picked " + name.lower()
        return output_json_builder_with_reprompt_and_card(Player_BIOGRAPHY[name.lower()],False)
    else:
        wrongname_MSG = "You haven't used the full name of a player. If you have forgotten which players you can pick say Help."
        reprompt_MSG = "Do you want to hear more about a particular player?"
        card_TEXT = "Use the full name."
        card_TITLE = "Wrong name."
        return output_json_builder_with_reprompt_and_card(wrongname_MSG,False)
        
def stop_the_skill(event):
    stop_MSG = "Thank you. Bye!"
    reprompt_MSG = ""
    card_TEXT = "Bye."
    card_TITLE = "Bye Bye."
    return output_json_builder_with_reprompt_and_card(stop_MSG,True)
    
def assistance(event):
    assistance_MSG = "You can choose among these players: " + ',Player_LIST)) + ". Be sure to use the full name when asking about the player."
    reprompt_MSG = "Do you want to hear more about a particular player?"
    card_TEXT = "You've asked for help."
    card_TITLE = "Help"
    return output_json_builder_with_reprompt_and_card(assistance_MSG,False)

def fallback_call(event):
    fallback_MSG = "I can't help you with that,try rephrasing the question or ask for help by saying HELP."
    reprompt_MSG = "Do you want to hear more about a particular player?"
    card_TEXT = "You've asked a wrong question."
    card_TITLE = "Wrong question."
    return output_json_builder_with_reprompt_and_card(fallback_MSG,False)
#------------------------------Part4--------------------------------
# The response of our Lambda function should be in a json format. 
# That is why in this part of the code we define the functions which 
# will build the response in the requested format. These functions
# are used by both the intent handlers and the request handlers to 
# build the output.
def plain_text_builder(text_body):
    text_dict = {}
    text_dict['type'] = 'PlainText'
    text_dict['text'] = text_body
    return text_dict

def reprompt_builder(repr_text):
    reprompt_dict = {}
    reprompt_dict['outputSpeech'] = plain_text_builder(repr_text)
    return reprompt_dict
    
def card_builder(c_text,c_title):
    card_dict = {}
    card_dict['type'] = "Simple"
    card_dict['title'] = c_title
    card_dict['content'] = c_text
    return card_dict    

def response_field_builder_with_reprompt_and_card(outputSpeach_text,card_text,card_title,reprompt_text,value):
    speech_dict = {}
    speech_dict['outputSpeech'] = plain_text_builder(outputSpeach_text)
    speech_dict['card'] = card_builder(card_text,card_title)
    speech_dict['reprompt'] = reprompt_builder(reprompt_text)
    speech_dict['shouldEndSession'] = value
    return speech_dict

def output_json_builder_with_reprompt_and_card(outputSpeach_text,value):
    response_dict = {}
    response_dict['version'] = '1.0'
    response_dict['response'] = response_field_builder_with_reprompt_and_card(outputSpeach_text,value)
    return response_dict

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?