Alexa 教程 2016
有关 Alexa 如何工作的重要信息。 - 正在进行中!
Alexa 基于类似于 Siri 和 Google Now 服务的问答方式。
要创建技能,我们需要创建一个 Alexa 技能来捕获请求,例如“Alexa,询问 HackGuides 现在几点了?”
然后,它将从 Alexa Skill API 转换为对 Amazon AWS Lambda 服务的方法调用,然后通过 Alexa Skill 发送回给用户。
让我们开始吧
要创建技能,您需要为developer.amazon.com创建::
现在您有一个帐户,您可以开始添加新技能:
技能信息
- 将名称更新为“Hack Guides SKill Demo”
- 使用“code hack”更新调用名称(这样更容易发音)
交互模型
返回 Alexa SKill Kit 中的“配置”
测试
{
"intents": [
{
"intent": "GetChoresIntent",
"slots": [
{
"name": "firstName",
"type" : "AMAZON.US_FIRST_NAME"
}
]
},
{
"intent": "AMAZON.HelpIntent"
},
{
"intent": "AMAZON.StopIntent"
},
{
"intent": "AMAZON.CancelIntent"
}
]
}
GetChoresIntent what chore is for {firstName}
GetChoresIntent {firstName} needs a chore
GetChoresIntent give me a chore for {firstName}
配置
- 在填写此部分之前,首先我们需要创建一个 Lambda 表达式
导航到此网站并创建必要的帐户Lambda Expression Website
选择蓝图
配置事件源
配置功能
您可能需要创建一个新角色,因此请单击“创建新角色”。
审查
在您的 Lambda 函数的右上角,您现在将拥有一个为 Alexa Skill 提供连接的 ARN。
// Route the incoming request based on type (LaunchRequest, IntentRequest,
// etc.) The JSON body of the request is provided in the event parameter.
exports.handler = function (event, context) {
try {
console.log("event.session.application.applicationId=" + event.session.application.applicationId);
/**
* Uncomment this if statement and populate with your skill's application ID to
* prevent someone else from configuring a skill that sends requests to this function.
*/
/*
if (event.session.application.applicationId !== "amzn1.echo-sdk-ams.app.[unique-value-here]") {
context.fail("Invalid Application ID");
}
*/
if (event.session.new) {
onSessionStarted({requestId: event.request.requestId}, event.session);
}
if (event.request.type === "LaunchRequest") {
onLaunch(event.request,
event.session,
function callback(sessionAttributes, speechletResponse) {
context.succeed(buildResponse(sessionAttributes, speechletResponse));
});
} else if (event.request.type === "IntentRequest") {
onIntent(event.request,
event.session,
function callback(sessionAttributes, speechletResponse) {
context.succeed(buildResponse(sessionAttributes, speechletResponse));
});
} else if (event.request.type === "SessionEndedRequest") {
onSessionEnded(event.request, event.session);
context.succeed();
}
} catch (e) {
context.fail("Exception: " + e);
}
};
/**
* Called when the session starts.
*/
function onSessionStarted(sessionStartedRequest, session) {
console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId +
", sessionId=" + session.sessionId);
}
/**
* Called when the user launches the skill without specifying what they want.
*/
function onLaunch(launchRequest, session, callback) {
console.log("onLaunch requestId=" + launchRequest.requestId +
", sessionId=" + session.sessionId);
// Dispatch to your skill's launch.
getWelcomeResponse(callback);
}
/**
* Called when the user specifies an intent for this skill.
*/
function onIntent(intentRequest, session, callback) {
console.log("onIntent requestId=" + intentRequest.requestId +
", sessionId=" + session.sessionId);
var intent = intentRequest.intent,
intentName = intentRequest.intent.name;
// Dispatch to your skill's intent handlers
if ("GetChoresIntent" === intentName) {
getChoreResponse(intent, session, callback);
} else if ("AMAZON.HelpIntent" === intentName) {
getWelcomeResponse(callback);
} else if ("AMAZON.StopIntent" === intentName || "AMAZON.CancelIntent" === intentName) {
handleSessionEndRequest(callback);
} else {
throw "Invalid intent";
}
}
/**
* Called when the user ends the session.
* Is not called when the skill returns shouldEndSession=true.
*/
function onSessionEnded(sessionEndedRequest, session) {
console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId +
", sessionId=" + session.sessionId);
// Add cleanup logic here
}
// --------------- Functions that control the skill's behavior -----------------------
function getWelcomeResponse(callback) {
// If we wanted to initialize the session to have some attributes we could add those here.
var sessionAttributes = {};
var cardTitle = "Welcome";
var speechOutput = "Welcome to the Hack Guides demo. " +
"Please ask for a chore by saying: give me a chore for name";
var repromptText = "Please ask for a chore by saying: give me a chore for name";
var shouldEndSession = false;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
function handleSessionEndRequest(callback) {
var cardTitle = "Session Ended";
var speechOutput = "Thank you for trying the Hack Guides demo. Have an Amazon day!";
// Setting this to true ends the session and exits the skill.
var shouldEndSession = true;
callback({}, buildSpeechletResponse(cardTitle, speechOutput, null, shouldEndSession));
}
/**
* Sets the color in the session and prepares the speech to reply to the user.
*/
function getChoreResponse(intent, session, callback) {
var cardTitle = intent.name;
var fistNameSlot = intent.slots.firstName;
var repromptText = "";
var sessionAttributes = {};
var shouldEndSession = false;
var speechOutput = "";
if (fistNameSlot) {
var miscResponses = [
'I think <break time="1s"/> {firstNameSlot} should work on Sweeping and moping!',
'I think {firstNameSlot} should work on <break time="1s"/> dishes!',
'{firstNameSlot}! should work on making their bed!',
'Maybe, nope definitely {firstNameSlot} should work on vacuuming the house',
'I think this time {firstNameSlot} should work on cleaning the toilets!',
<span class="hljs-st
免责声明:本内容来源于第三方作者授权、网友推荐或互联网整理,旨在为广大用户提供学习与参考之用。所有文本和图片版权归原创网站或作者本人所有,其观点并不代表本站立场。如有任何版权侵犯或转载不当之情况,请与我们取得联系,我们将尽快进行相关处理与修改。感谢您的理解与支持!
请先 登录后发表评论 ~