Skip to content

Commit

Permalink
Added speechAssets toString
Browse files Browse the repository at this point in the history
  • Loading branch information
michael.gloger committed Jun 24, 2016
1 parent dd11271 commit 3be37e6
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 43 deletions.
39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ app.intent('AsyncIntent', 'Search for something in database', (slots, attrs, don
});
```

### Generate Speech Assets

To minimize manual work needed while deploying your Alexa skills you can use our speechAssets generator. This helps you to create `intentSchema`, `sampleUtterances` and `customSlots` for your apps.

Speech assets consists of:
- **intentSchema** - array of intents with slots
- **utterances** - phrases that are used to invoke intents
- **customSlots** - custom slot types with samples

For more information see [interaction model reference](https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/alexa-skills-kit-interaction-model-reference)

```javascript
const speechAssets = app.speechAssets(); // object
console.log(speechAssets.toString()); // stringified version - f.e. copy paste from console
```

### Handling Amazon Requests

To handle Amazon requests you need to create HTTP server with POST route. See below example with [Hapi](http://hapijs.com/) server
Expand All @@ -192,26 +208,10 @@ server.route({
server.start((err) => {
if (err) throw err;
console.log('Server running at:', server.info.uri);
});
```

### Generate Speech Assets

To minimize manual work needed while deploying your Alexa skills you can use our speechAssets generator. This helps you to create `intentSchema`, `sampleUtterances` and `customSlots` for your apps.

Speech assets consists of:
- *intentSchema.json* - map of intents and their slots
- *customSlotTypes* - custom slots that are not part of biuld-in slots. It is recommended to use it when the value is from some set of values (e.g. Sign of Zodiac)
- *sampleUtterances* - phrases that are used to invoke intents

For more information see [interaction model reference](https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/alexa-skills-kit-interaction-model-reference)

```javascript
const assets = app.generateSpeechAssets();

console.log(assets.intentSchema);
console.log(assets.sampleUtterances);
console.log(assets.customSlots);
const speechAssets = app.speechAssets();
console.log(speechAssets.toString());
});
```

### Actions
Expand Down Expand Up @@ -329,6 +329,7 @@ app.handle(launchRequest, (response) => {
- Merge session attributes automatically
- Advanced error handling
- Logging
- Add more sophisticated request creator for unit testing

## Scripts

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
const alexia = require('..');
const app = alexia.createApp('GenerateSpeechAssetsExample');
const app = alexia.createApp('SpeechAssetsExample');

app.customSlot('Mood', ['fine', 'meh']);

Expand All @@ -12,10 +12,7 @@ app.intent('AwesomeIntent', 'You are awesome', (slots, attrs) => {
return 'Yes';
});

const assets = app.generateSpeechAssets();

console.log(assets.intentSchema);
console.log(assets.sampleUtterances);
console.log(assets.customSlotSamples);
const assets = app.speechAssets();
console.log(speechAssets.toString());

module.exports = app;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alexia",
"version": "1.0.0",
"version": "1.0.1",
"description": "A Framework for creating Amazon Echo (Alexa) skills using Node.js",
"main": "src/alexia.js",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions src/create-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ module.exports = (name, options) => {
/**
* Generates speech assets object: {schema, utterances, customSlots}
*/
app.generateSpeechAssets = () => {
return generateSpeechAssets(app);
app.speechAssets = () => {
return generateSpeechAssets(app);
};

return app;
Expand Down
49 changes: 40 additions & 9 deletions src/generate-speech-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,49 @@
const _ = require('lodash');

module.exports = (app) => {
return {
intentSchema: generateIntentSchema(app.intents),
sampleUtterances: generateSampleUtterances(app.intents),
customSlotSamples: generateCustomSlotSamples(app.customSlots)
};
let assets = {
intentSchema: genIntentSchema(app.intents),
utterances: genUtterances(app.intents),
customSlots: genCustomSlots(app.customSlots)
};

assets.toString = createStringifyAssets(assets);

return assets;
}

/**
* @param {object} assets
* @param {object} assets.intentSchema
* @param {object} assets.utterances
* @param {object} assets.customSlots
* @returns {function} returning stringified version of speech assetsuseful for printing in terminal
*/
const createStringifyAssets = (assets) => () => {
let customSlotsString = _.map(assets.customSlots, (samples, name) => {
return `${name}:\n${samples.join('\n')}\n`;
}).join('\n');

return createAsset('intentSchema', assets.intentSchema) +
createAsset('utterances', assets.utterances) +
createAsset('customSlots', customSlotsString);
};

/**
* Creates stringified part of speechAssets
* @param {string} type
* @param {object} data
* @returns {string}
*/
const createAsset = (type, data) => {
return `${type}:\n${data}\n\n`;
};

/**
* Generates intent schema JSON string
* @return {string} strigified intent schema object generated from intents
*/
const generateIntentSchema = (intents) => {
const genIntentSchema = (intents) => {
var intentSchema = {
intents: []
};
Expand All @@ -38,7 +69,7 @@ const generateIntentSchema = (intents) => {
* Generates sample utterances tied to intent name
* @return {string} interpretation of all sample utterances
*/
const generateSampleUtterances = (intents) => {
const genUtterances = (intents) => {
let sampleUtterances = [];

_.forOwn(intents, intent => {
Expand All @@ -56,11 +87,11 @@ const generateSampleUtterances = (intents) => {
* @return {object} where key = slot type and value is string interpretation of
* custom slot type samples
*/
const generateCustomSlotSamples = (customSlots) => {
const genCustomSlots = (customSlots) => {
var allCustomSlotSamples = {};

_.forOwn(customSlots, (customSlot) => {
allCustomSlotSamples[customSlot.name] = customSlot.samples.join('\n');
allCustomSlotSamples[customSlot.name] = customSlot.samples;
});

return allCustomSlotSamples;
Expand Down
19 changes: 13 additions & 6 deletions test/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const expect = require('chai').expect;
const app = require('./test-apps/basic-app');

describe('generateSpeechAssets', () => {
const assets = app.generateSpeechAssets();
const assets = app.speechAssets();

it('should generate intentSchema', () => {
expect(JSON.parse(assets.intentSchema)).to.deep.equal({
Expand All @@ -31,8 +31,8 @@ describe('generateSpeechAssets', () => {
});
});

it('should generate sampleUtterances', () => {
expect(assets.sampleUtterances.split('\n')).to.deep.equal([
it('should generate utterances', () => {
expect(assets.utterances.split('\n')).to.deep.equal([
'FirstIntent utterance',
'NamedIntent utteranceB',
'NamedIntent utteranceC',
Expand All @@ -50,10 +50,17 @@ describe('generateSpeechAssets', () => {
]);
});

it('should generate customSlotSamples', () => {
expect(assets.customSlotSamples).to.deep.equal({
Name: ['Borimir', 'Vlasto'].join('\n')
it('should generate customSlots', () => {
expect(assets.customSlots).to.deep.equal({
Name: ['Borimir', 'Vlasto']
});
});

it('should generate stringified speechAssets', () => {
const stringifiedAssets = assets.toString();

expect(typeof stringifiedAssets).to.equal('string');
expect(stringifiedAssets.split('\n\n').length).to.equal(4);
});

});

0 comments on commit 3be37e6

Please sign in to comment.