Skip to content

Commit

Permalink
Merge f106a2d into b83b8da
Browse files Browse the repository at this point in the history
  • Loading branch information
armonge committed Oct 11, 2018
2 parents b83b8da + f106a2d commit a5ce5e8
Show file tree
Hide file tree
Showing 24 changed files with 1,090 additions and 1,295 deletions.
20 changes: 0 additions & 20 deletions docs/controllers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,6 @@ Or they can be a function that gets a :ref:`voxaEvent <voxa-event>` object.
Your state should respond with a :ref:`transition <transition>`. The transition is a plain object that can take ``directives``, ``to`` and ``flow`` keys.

The ``entry`` controller
--------------------------

The ``entry`` controller is special in that it's the default state to go to at the beginning of your session and if your state returns no response.

For example in the next snipped there's a ``waiting`` state that expects an ``AMAZON.NextIntent`` or an ``AMAZON.PreviousIntent``, in the case the users says something unexpected like an ``AMAZON.HelpIntent`` the state returns undefined, the State Machine framework handles this situations by redirecting to the ``entry`` state

.. code-block:: javascript
app.onState('waiting', (voxaEvent) => {
if (voxaEvent.intent.name === 'AMAZON.NextIntent') {
voxaEvent.model.index += 1;
return { ask: 'Ingredients.Describe', to: 'waiting' }
} else if (voxaEvent.intent.name === 'AMAZON.PreviousIntent') {
voxaEvent.model.index -= 1;
return { ask: 'Ingredients.Describe', to: 'waiting' }
}
});
The ``onIntent`` helper
-----------------------

Expand Down
23 changes: 14 additions & 9 deletions hello-world/hello-world.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

require("source-map-support").install();
let voxa;
try {
voxa = require("voxa");
Expand Down Expand Up @@ -51,15 +50,21 @@ app.onBeforeReplySent(request => {
}
});

app.onState("likesVoxa?", request => {
if (request.intent.name === "YesIntent") {
return { tell: "doesLikeVoxa" };
}
app.onState(
"likesVoxa?",
{
tell: "doesLikeVoxa"
},
"YesIntent"
);

if (request.intent.name === "NoIntent") {
return { ask: "doesNotLikeVoxa" };
}
});
app.onState(
"likesVoxa?",
{
tell: "doesNotLikeVoxa"
},
"NoIntent"
);

const alexaSkill = new AlexaPlatform(app);

Expand Down
12 changes: 8 additions & 4 deletions src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import * as _ from "lodash";
import { IBag, IVoxaEvent } from "./VoxaEvent";

export class Model {
[key: string]: any
[key: string]: any;

public static deserialize(data: IBag, voxaEvent: IVoxaEvent): Promise<Model>|Model {
public static deserialize(
data: IBag,
voxaEvent: IVoxaEvent,
): Promise<Model> | Model {
return new this(data);
}

Expand All @@ -14,12 +17,13 @@ export class Model {
_.assign(this, data);
}

public serialize(): any|Promise<any> {
public async serialize(): Promise<any> {
return this;
}
}

export interface IModel {
new (data?: any): Model;
deserialize(data: IBag, event: IVoxaEvent): Model|Promise<Model>;
deserialize(data: IBag, event: IVoxaEvent): Model | Promise<Model>;
serialize(): Promise<any>;
}
59 changes: 59 additions & 0 deletions src/StateMachine/State.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2018 Rain Agency <contact@rain.agency>
* Author: Rain Agency <contact@rain.agency>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import * as _ from "lodash";
import { ITransition } from "../StateMachine";
import { IVoxaIntentEvent } from "../VoxaEvent";

export type IStateHandler = (event: IVoxaIntentEvent) => Promise<ITransition>;

export class State {
public intents: string[] = [];
private handler: IStateHandler;

constructor(
public name: string,
handler: IStateHandler | ITransition,
intents: string | string[] = [],
public platform: string = "core",
) {
if (_.isFunction(handler)) {
this.handler = handler;
} else {
this.handler = this.getSimpleTransitionHandler(handler);
}

if (_.isString(intents)) {
this.intents = [intents];
} else {
this.intents = intents;
}
}

public async handle(voxaEvent: IVoxaIntentEvent): Promise<ITransition> {
return this.handler(voxaEvent);
}

protected getSimpleTransitionHandler(transition: ITransition): IStateHandler {
return async (voxaEvent: IVoxaIntentEvent) => _.cloneDeep(transition);
}
}
Loading

0 comments on commit a5ce5e8

Please sign in to comment.