Skip to content
This repository has been archived by the owner on Oct 1, 2022. It is now read-only.

Commit

Permalink
support intents in separated classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Dilly committed Jan 12, 2017
1 parent ed23d55 commit 188ea1f
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 14 deletions.
4 changes: 3 additions & 1 deletion dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ description "Amazon Alexa Skill Kit API in D"
authors "Stephan Dilly"
copyright "Copyright © 2017, Stephan Dilly"
license "MIT"
dependency "vibe-d:data" version="~>0.7.30"
dependency "vibe-d" version="~>0.7.30"
targetType "library"
targetPath "lib"
57 changes: 45 additions & 12 deletions source/ask/alexaskill.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import vibe.d;

import ask.types;
import ask.locale;
import ask.baseintent;

/// annotation to mark an intent callback, use name to specify the exact intent name as specified in the intent schema
struct CustomIntent
Expand All @@ -13,10 +14,12 @@ struct CustomIntent
}

///
abstract class AlexaSkill(T)
abstract class AlexaSkill(T) : ITextManager
{
///
private AlexaText[] localeText;
///
private BaseIntent[] intents;

/++
+ constructor that requires the loca table as input
Expand All @@ -33,7 +36,7 @@ abstract class AlexaSkill(T)
}

///
int execute(AlexaEvent event, AlexaContext context, Duration timeout = 2.seconds)
int runInEventLoop(AlexaEvent event, AlexaContext context, Duration timeout = 2.seconds)
{
import std.stdio:writeln,stderr;

Expand All @@ -42,14 +45,7 @@ abstract class AlexaSkill(T)

stderr.writefln("execute request: %s",event.request.type);

AlexaResult result;

if(event.request.type == AlexaRequest.Type.LaunchRequest)
result = onLaunch(event, context);
else if(event.request.type == AlexaRequest.Type.IntentRequest)
result = onIntent(event, context);
else if(event.request.type == AlexaRequest.Type.SessionEndedRequest)
onSessionEnd(event, context);
auto result = executeEvent(event, context);

writeln(serializeToJson(result).toPrettyString());
});
Expand All @@ -63,6 +59,28 @@ abstract class AlexaSkill(T)
return runEventLoop();
}

///
AlexaResult executeEvent(AlexaEvent event, AlexaContext context)
{
AlexaResult result;

if(event.request.type == AlexaRequest.Type.LaunchRequest)
result = onLaunch(event, context);
else if(event.request.type == AlexaRequest.Type.IntentRequest)
result = onIntent(event, context);
else if(event.request.type == AlexaRequest.Type.SessionEndedRequest)
onSessionEnd(event, context);

return result;
}

/// see_also: `BaseIntent`
void addIntent(BaseIntent intent)
{
intents ~= intent;
intent.texts = this;
}

/// returns the
string getText(int _key) const pure nothrow
{
Expand Down Expand Up @@ -95,8 +113,23 @@ abstract class AlexaSkill(T)
}
}

return tryRegisteredIntents(event, context);
}

///
private AlexaResult tryRegisteredIntents(AlexaEvent event, AlexaContext context)
{
import std.stdio:stderr;
stderr.writefln("onIntent did not match: %s",event);

const eventIntent = event.request.intent.name;

foreach(baseIntent; intents)
{
if(baseIntent.name == eventIntent)
return baseIntent.onIntent(event,context);
}

stderr.writefln("onIntent did not match: %s",eventIntent);
return AlexaResult();
}

Expand All @@ -105,4 +138,4 @@ abstract class AlexaSkill(T)
{
throw new Exception("not implemented");
}
}
}
3 changes: 2 additions & 1 deletion source/ask/ask.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module ask.ask;

public import ask.alexaskill;
public import ask.types;
public import ask.locale;
public import ask.locale;
public import ask.baseintent;
59 changes: 59 additions & 0 deletions source/ask/baseintent.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module ask.baseintent;

import ask.alexaskill;
import ask.locale;
import ask.types;

///
abstract class BaseIntent
{
///
immutable string name;
///
ITextManager texts;

///
this()
{
import std.array:split;

TypeInfo_Class info = cast(TypeInfo_Class)typeid(this);
auto fullnameParts = info.name.split(".");
this.name = fullnameParts[$-1];
}

///
string getText(int _key) const pure nothrow
{
return texts.getText(_key);
}

///
AlexaResult onIntent(AlexaEvent, AlexaContext);
}

///
unittest
{
class TestIntent : BaseIntent{
override AlexaResult onIntent(AlexaEvent, AlexaContext){
AlexaResult res;
res._version = "v3";
return res;
}
}

class TestSkill : AlexaSkill!TestSkill{
this(){
super([]);
addIntent(new TestIntent);
}
}

auto skill = new TestSkill();
AlexaEvent ev;
ev.request.type = AlexaRequest.Type.IntentRequest;
ev.request.intent.name = "TestIntent";
auto res = skill.executeEvent(ev,AlexaContext());
assert(res._version == "v3");
}
7 changes: 7 additions & 0 deletions source/ask/locale.d
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
module ask.locale;

///
interface ITextManager
{
///
string getText(int _key) const pure nothrow;
}

///
struct AlexaText
{
Expand Down

0 comments on commit 188ea1f

Please sign in to comment.