kidshift-skills/lambda/index.js

238 lines
10 KiB
JavaScript
Raw Permalink Normal View History

2024-07-15 21:05:08 +00:00
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
2024-07-16 22:25:27 +00:00
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
2024-07-15 21:05:08 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
const Alexa = __importStar(require("ask-sdk-core"));
2024-07-16 00:55:50 +00:00
const AWS = __importStar(require("aws-sdk"));
const DynamoDBPersistantAttributesAdapter = __importStar(require("ask-sdk-dynamodb-persistence-adapter"));
2024-07-16 22:25:27 +00:00
const MetaService_1 = __importDefault(require("./service/MetaService"));
2024-07-16 23:25:15 +00:00
const AuthService_1 = __importDefault(require("./service/AuthService"));
2024-07-17 00:19:40 +00:00
const TaskService_1 = __importDefault(require("./service/TaskService"));
const AttributeUtils_1 = __importDefault(require("./AttributeUtils"));
2024-07-17 05:06:37 +00:00
const ChildService_1 = __importDefault(require("./service/ChildService"));
2024-07-17 07:12:39 +00:00
const const_1 = require("./const");
2024-07-18 03:41:38 +00:00
const TaskCompletedDerective_1 = __importDefault(require("./apl/TaskCompletedDerective"));
2024-07-15 18:48:13 +00:00
const LaunchRequestHandler = {
canHandle(handlerInput) {
2024-07-15 20:41:47 +00:00
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
2024-07-15 18:48:13 +00:00
},
handle(handlerInput) {
2024-07-15 20:49:04 +00:00
const speakOutput = 'Welcome, you can say Hello or Help. Which would you like to try?';
2024-07-15 18:48:13 +00:00
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const HelloWorldIntentHandler = {
canHandle(handlerInput) {
2024-07-15 20:41:47 +00:00
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
2024-07-16 04:48:04 +00:00
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
2024-07-15 18:48:13 +00:00
},
2024-07-16 00:47:51 +00:00
async handle(handlerInput) {
2024-07-16 04:48:04 +00:00
const speakOutput = 'Hello World!';
2024-07-16 00:44:20 +00:00
return handlerInput.responseBuilder
2024-07-16 00:55:50 +00:00
.speak(speakOutput)
2024-07-16 00:44:20 +00:00
.getResponse();
}
};
2024-07-16 01:29:54 +00:00
const KidShiftAuthIntentHandler = {
canHandle(handlerInput) {
2024-07-16 03:48:24 +00:00
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
2024-07-16 21:43:08 +00:00
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'KidShiftAuthIntent';
2024-07-16 06:29:31 +00:00
},
async handle(handlerInput) {
2024-07-16 21:51:46 +00:00
const loginCode = Alexa.getSlotValue(handlerInput.requestEnvelope, 'loginCode');
2024-07-16 23:25:15 +00:00
const tokenResponse = await AuthService_1.default.login(loginCode);
if (tokenResponse) {
2024-07-17 00:28:39 +00:00
const attributeUtils = new AttributeUtils_1.default(handlerInput);
await attributeUtils.setToken(tokenResponse.accessToken);
await attributeUtils.saveAttributes();
2024-07-16 23:25:15 +00:00
return handlerInput.responseBuilder
2024-07-17 07:12:39 +00:00
.speak(const_1.MESSAGES.LOGGED_IN)
2024-07-16 23:25:15 +00:00
.getResponse();
}
else {
return handlerInput.responseBuilder
.speak('Login failed')
.getResponse();
}
2024-07-16 06:29:31 +00:00
}
};
2024-07-17 00:19:40 +00:00
const KidShiftTaskListIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'KidShiftTaskListIntent';
},
async handle(handlerInput) {
const attributeUtils = new AttributeUtils_1.default(handlerInput);
TaskService_1.default.setToken(await attributeUtils.getToken());
const taskList = await TaskService_1.default.getTasks();
return handlerInput.responseBuilder
.speak(taskList.list.map((task) => task.name).join(', '))
.getResponse();
}
};
const KidShiftTaskCompleteIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'KidShiftTaskCompleteIntent';
},
async handle(handlerInput) {
2024-07-17 05:27:24 +00:00
const attributeUtils = new AttributeUtils_1.default(handlerInput);
TaskService_1.default.setToken(await attributeUtils.getToken());
2024-07-17 05:06:37 +00:00
const taskList = await TaskService_1.default.getTasks();
const childList = await ChildService_1.default.getChildList();
const taskName = Alexa.getSlotValue(handlerInput.requestEnvelope, 'taskName');
const childName = Alexa.getSlotValue(handlerInput.requestEnvelope, 'childName');
const task = taskList.list.find((task) => task.name === taskName);
if (!task) {
return handlerInput.responseBuilder
2024-07-17 07:12:39 +00:00
.speak(const_1.MESSAGES.TASK_NOT_FOUND)
2024-07-17 05:06:37 +00:00
.getResponse();
}
const child = childList.list.find((child) => child.name === childName);
if (!child) {
return handlerInput.responseBuilder
2024-07-17 07:12:39 +00:00
.speak(const_1.MESSAGES.CHILD_NOT_FOUND)
2024-07-17 05:06:37 +00:00
.getResponse();
}
return TaskService_1.default.completeTask(task.id, child.id).then(() => {
2024-07-18 03:41:38 +00:00
const dataSources = TaskCompletedDerective_1.default.createDataSources(task.name, " + " + task.reward + " 円");
const directivePayload = TaskCompletedDerective_1.default.createDirectivePayload(dataSources);
2024-07-17 05:06:37 +00:00
return handlerInput.responseBuilder
2024-07-17 07:12:39 +00:00
.speak(const_1.MESSAGES.TASK_COMPLETED)
2024-07-17 07:39:24 +00:00
.addDirective(directivePayload)
2024-07-17 05:06:37 +00:00
.getResponse();
}).catch(() => {
return handlerInput.responseBuilder
2024-07-17 07:12:39 +00:00
.speak(const_1.MESSAGES.ERROR_OCCURRED)
2024-07-17 05:06:37 +00:00
.getResponse();
});
2024-07-17 00:19:40 +00:00
}
};
2024-07-16 22:25:27 +00:00
const KidShiftMetaIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
2024-07-16 22:54:23 +00:00
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'KidShiftMetaIntent';
2024-07-16 22:25:27 +00:00
},
async handle(handlerInput) {
return MetaService_1.default.getMeta().then((resp) => {
return handlerInput.responseBuilder
2024-07-16 23:08:38 +00:00
.speak(JSON.stringify(resp))
2024-07-16 22:25:27 +00:00
.getResponse();
}).catch((err) => {
return handlerInput.responseBuilder
.speak('Error occured')
.getResponse();
});
}
};
2024-07-15 18:48:13 +00:00
const HelpIntentHandler = {
canHandle(handlerInput) {
2024-07-15 20:41:47 +00:00
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
2024-07-15 18:48:13 +00:00
},
handle(handlerInput) {
2024-07-15 20:49:04 +00:00
const speakOutput = 'You can say hello to me! How can I help?';
2024-07-15 18:48:13 +00:00
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
2024-07-15 20:41:47 +00:00
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
2024-07-15 18:48:13 +00:00
},
handle(handlerInput) {
2024-07-15 20:49:04 +00:00
const speakOutput = 'Goodbye!';
2024-07-15 18:48:13 +00:00
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
const FallbackIntentHandler = {
canHandle(handlerInput) {
2024-07-15 20:41:47 +00:00
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.FallbackIntent';
2024-07-15 18:48:13 +00:00
},
handle(handlerInput) {
2024-07-15 20:49:04 +00:00
const speakOutput = 'Sorry, I don\'t know about that. Please try again.';
2024-07-15 18:48:13 +00:00
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
2024-07-15 20:41:47 +00:00
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
2024-07-15 18:48:13 +00:00
},
handle(handlerInput) {
console.log(`~~~~ Session ended: ${JSON.stringify(handlerInput.requestEnvelope)}`);
2024-07-15 21:05:08 +00:00
return handlerInput.responseBuilder.getResponse();
2024-07-15 18:48:13 +00:00
}
};
const IntentReflectorHandler = {
canHandle(handlerInput) {
2024-07-15 20:41:47 +00:00
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
2024-07-15 18:48:13 +00:00
},
handle(handlerInput) {
2024-07-15 20:41:47 +00:00
const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
2024-07-15 20:49:04 +00:00
const speakOutput = `You just triggered ${intentName}`;
2024-07-15 18:48:13 +00:00
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
2024-07-15 20:49:04 +00:00
const speakOutput = 'Sorry, I had trouble doing what you asked. Please try again.';
console.log(`~~~~ Error handled: ${JSON.stringify(error)}`);
2024-07-15 18:48:13 +00:00
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
2024-07-15 20:41:47 +00:00
exports.handler = Alexa.SkillBuilders.custom()
2024-07-17 00:19:40 +00:00
.addRequestHandlers(LaunchRequestHandler, HelloWorldIntentHandler, KidShiftAuthIntentHandler, KidShiftTaskListIntentHandler, KidShiftTaskCompleteIntentHandler, KidShiftMetaIntentHandler, HelpIntentHandler, CancelAndStopIntentHandler, FallbackIntentHandler, SessionEndedRequestHandler, IntentReflectorHandler)
2024-07-15 21:05:08 +00:00
.addErrorHandlers(ErrorHandler)
2024-07-16 00:55:50 +00:00
.withPersistenceAdapter(new DynamoDBPersistantAttributesAdapter.DynamoDbPersistenceAdapter({
tableName: process.env.DYNAMODB_PERSISTENCE_TABLE_NAME || 'ask-sdk-table',
createTable: false,
dynamoDBClient: new AWS.DynamoDB({ apiVersion: 'latest', region: process.env.DYNAMODB_PERSISTENCE_REGION })
}))
.withCustomUserAgent('sample/hello-world/v1.2')
2024-07-15 21:05:08 +00:00
.lambda();
//# sourceMappingURL=index.js.map