intentful
Version:
Create Custom Skills with less headache
222 lines (221 loc) • 9.2 kB
JavaScript
"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Skill = void 0;
const Alexa = __importStar(require("ask-sdk"));
const ask_sdk_1 = require("ask-sdk");
const aws_sdk_1 = require("aws-sdk");
const fs = __importStar(require("fs"));
class Skill {
constructor(props) {
this.locales = {};
this.aplDocuments = {};
this.aplaDocuments = {};
this.props = props;
}
toJSON() {
return this.model();
}
addLocale(locale) {
if (this.locales[locale.localeValue]) {
throw new Error(`${locale.localeValue} already added to skill`);
}
this.locales[locale.localeValue] = locale;
return this;
}
addAPLDocument(aplDocument) {
if (this.aplDocuments[aplDocument.props.documentName]) {
throw new Error(`APL Document already added ${aplDocument.props.documentName}`);
}
this.aplDocuments[aplDocument.props.documentName] = aplDocument;
return this;
}
addAPLADocument(aplaDocument) {
if (this.aplDocuments[aplaDocument.props.documentName]) {
throw new Error(`APLA Document already added ${aplaDocument.props.documentName}`);
}
this.aplaDocuments[aplaDocument.props.documentName] = aplaDocument;
return this;
}
addAlexaHostedDynamoDBPersistenceAdapter(builder) {
if (this.props.dynamoDBInfo) {
builder.withPersistenceAdapter(new ask_sdk_1.DynamoDbPersistenceAdapter({
tableName: process.env.DYNAMODB_PERSISTENCE_TABLE_NAME,
createTable: true,
dynamoDBClient: new aws_sdk_1.DynamoDB({ apiVersion: 'latest', region: process.env.DYNAMODB_PERSISTENCE_REGION })
}));
}
}
addDynamoDBPersistenceAdapter(builder) {
if (this.props.dynamoDBInfo) {
builder.withPersistenceAdapter(new ask_sdk_1.DynamoDbPersistenceAdapter(this.props.dynamoDBInfo));
}
}
addRequestInterceptor(builder) {
if (this.props.requestInterceptor) {
builder.addRequestInterceptors({
process: this.props.requestInterceptor
});
}
}
addResponseInterceptor(builder) {
if (this.props.responseInterceptor) {
builder.addResponseInterceptors({
process: this.props.responseInterceptor
});
}
}
getErrorHandlers() {
return this.getLocaleStrings()
.map((ls) => this.locales[ls])
.map((locale) => locale.getErrorHandler());
}
builder() {
const builder = Alexa.SkillBuilders.custom()
.addRequestHandlers(...this.getRequestHandlers())
.addErrorHandlers(...this.getErrorHandlers())
.withCustomUserAgent(this.props.customUserAgent ? this.props.customUserAgent : 'JamiabboSkillBuilder');
this.addRequestInterceptor(builder);
this.addResponseInterceptor(builder);
return builder;
}
handler() {
return this.builder().lambda();
}
writeSkillPackage(rootFolder) {
const baseSkillPackagePath = (rootFolder ? rootFolder : '.') + '/skill-package/';
Object.keys(this.locales)
.map((locale) => this.locales[locale])
.forEach((locale) => {
locale.writeToPackageFile(baseSkillPackagePath);
});
Object.keys(this.aplDocuments)
.map((documentName) => this.aplDocuments[documentName])
.forEach((aplDocument) => aplDocument.writeToFile(baseSkillPackagePath));
Object.keys(this.aplaDocuments)
.map((documentName) => this.aplaDocuments[documentName])
.forEach((aplaDocument) => aplaDocument.writeToFile(baseSkillPackagePath));
this.writeToPackageFile(baseSkillPackagePath);
}
getRequestHandlers() {
const localeList = this.getLocaleStrings().map((locale) => this.locales[locale]);
const aplDocumentKeys = Object.keys(this.aplDocuments).map((documentName) => this.aplDocuments[documentName]);
const requestHandlers = localeList
.flatMap((locale) => locale.getRequestHandlers())
.concat(aplDocumentKeys.flatMap((aplDocument) => aplDocument.getRequestHandlers()));
return requestHandlers;
}
getLocaleStrings() {
return Object.keys(this.locales);
}
model() {
var _a, _b;
return {
manifest: {
manifestVersion: '1.0',
apis: {
custom: {
dialogManagement: {
dialogManagers: [
{
type: 'AMAZON.Conversations'
}
]
},
endpoint: (_a = this.props.custom) === null || _a === void 0 ? void 0 : _a.endpoint,
regions: (_b = this.props.custom) === null || _b === void 0 ? void 0 : _b.regions,
interfaces: [
{
type: 'ALEXA_PRESENTATION_APL',
supportedViewports: [
{
mode: 'HUB',
shape: 'ROUND',
minWidth: 100,
maxWidth: 599,
minHeight: 100,
maxHeight: 599
},
{
mode: 'HUB',
shape: 'RECTANGLE',
minHeight: 600,
maxHeight: 959,
minWidth: 960,
maxWidth: 1279
},
{
mode: 'HUB',
shape: 'RECTANGLE',
minHeight: 600,
maxHeight: 1279,
minWidth: 1280,
maxWidth: 1920
},
{
mode: 'TV',
shape: 'RECTANGLE',
minHeight: 540,
maxHeight: 540,
minWidth: 960,
maxWidth: 960
}
]
}
]
}
},
publishingInformation: {
locales: [
{},
...this.getLocaleStrings().map((localeString) => {
return {
[localeString]: {
name: this.props.name
}
};
})
].reduce((left, right) => {
return {
...left,
...right
};
}),
name: this.props.name
}
}
};
}
writeToPackageFile(baseSkillPackagePath) {
const packageFolder = baseSkillPackagePath ? baseSkillPackagePath : './';
if (!fs.existsSync(packageFolder)) {
fs.mkdirSync(packageFolder, { recursive: true });
}
const model = this.model();
// write interaction models
fs.writeFileSync(`${packageFolder}/skill.json`, JSON.stringify(model, null, 2));
}
}
exports.Skill = Skill;