Share
Embed
[Englisch] Alexa, Say Hello to Chefkoch
Alexa skill development with Golang, Apex and AWS Lambda.
Golexa - A little Go library to easily handle Alexa custom skill requests
https://github.com/b00giZm/golexa
Follow me:
https://twitter.com/b00gizm
https://github.com/b00gizm
Pascal Cremer
November 17, 2016
Transcript
53 slides
-
"ALEXA, SAY HELLO TO CHEFKOCH!" Alexa Skill Development with Golang,
Apex and AWS Lambda -
"…bad news: We have less than 6 weeks to deliver
a custom Alexa skill." -
Golang: A statically compiled, C- like programming language with an
easy to learn syntax, huge benefits on performance and an awesome concurrency model. -
func Greeting(name string) string { return "Hello " + name
} function greeting($name) { return 'Hello '.$name; } GOLANG PHP -
type Person struct{ FirstName, LastName string } func (p
*Person) FullName() string { return p.FirstName + " " + p.LastName } fuckFace := &Person{"Donald", "Trump"} class Person { private $firstName; private $lastName; public function __construct($firstName, $lastName) { $this->firstName = $firstName; $this->lastName = $lastName; } public function getFullName() { return $this->firstName." ".$this->lastName; } } $fuckFace = new Person('Donald', 'Trump'); GOLANG PHP -
func ScoreForPlayer(name string) (int, bool) { ... } playerName :=
"Alice" score, ok := ScoreForPlayer(playerName) if (!ok) { panic("No score for player " + playerName) } fmt.Println(playerName + "'s score is " + strconv.Itoa(score)) function getScoreForPlayer($playerName) { ... } $playerName = "Alice"; $score = getScoreForPlayer($playerName); if (!$score) { die("No score for player ".$playerName); } echo $playerName."'s score is ".$score."\n"; GOLANG PHP -
type Person struct{ FirstName string `json:"first_name"` LastName string `json:"last_name"` }
func (p *Person) FullName() string { return p.FirstName + " " + p.LastName } jsonString := []byte{`{ "first_name": "Donald", "last_name": "Trump" }`} var fuckFace Person _ := json.Unmarshal(jsonString, &fuckFace) fmt.Println(fuckFace.FullName()) // "Donald Trump" GO JSON AWESOMENESS -
type Person struct{ FirstName string `json:"first_name"` LastName string `json:"last_name"` }
func (p *Person) FullName() string { return p.FirstName + " " + p.LastName } jsonString := []byte{`{ "first_name": "Donald", "last_name": "Trump" }`} var fuckFace Person _ := json.Unmarshal(jsonString, &fuckFace) fmt.Println(fuckFace.FullName()) // "Donald Trump" GO JSON AWESOMENESS -
type Person struct{ FirstName string `json:"first_name"` LastName string `json:"last_name"` }
func (p *Person) FullName() string { return p.FirstName + " " + p.LastName } jsonString := []byte{`{ "first_name": "Donald", "last_name": "Trump" }`} var fuckFace Person _ := json.Unmarshal(jsonString, &fuckFace) fmt.Println(fuckFace.FullName()) // "Donald Trump" GO JSON AWESOMENESS -
type Person struct{ FirstName string `json:"first_name"` LastName string `json:"last_name"` }
func (p *Person) FullName() string { return p.FirstName + " " + p.LastName } jsonString := []byte{`{ "first_name": "Donald", "last_name": "Trump" }`} var fuckFace Person _ := json.Unmarshal(jsonString, &fuckFace) fmt.Println(fuckFace.FullName()) // "Donald Trump" GO JSON AWESOMENESS -
func FetchTopRecipes(categoryID int, c chan RecipeList) { result := ...
c <- result } c := make(chan RecipeList) go FetchTopRecipes(123, c) go FetchTopRecipes(321, c) results := make([]RecipeList, 2) append(results, <-c) append(results, <-c) GO CONCURRENCY AWESOMENESS -
func FetchTopRecipes(categoryID int, c chan RecipeList) { result := ...
c <- result } c := make(chan RecipeList) go FetchTopRecipes(123, c) go FetchTopRecipes(321, c) results := make([]RecipeList, 2) append(results, <-c) append(results, <-c) GO CONCURRENCY AWESOMENESS -
AWS Lambda: An event driven service for running stateless lambda
functions for data processing on Amazon's cloud infrastructure. -
Alexa, frage Chefkoch nach Pasta Rezepten. Chefkoch Skill Utterances RecipeOfCategory
{Category} Rezepte RecipeOfCategory zeig mir {Category} Rezepte RecipeOfCategory ich hätte gerne {Category} Rezepte RecipeOfCategory gib mir Rezepte aus der Kategorie {Category} ... { ... "request": { ... "type": "IntentRequest", "locale": "de_DE", ... "intent": { "name": "RecipeOfCategory", "slots": { "string": { "name": "Category", "value": "Pasta" } } } } } { ... "response": { "outputSpeech": { "type": "PlainText", "text": "Ich habe dir...", }, "shouldEndSession": false } } Ich habe dir fünf der besten Pasta-Rezepte rausgesucht.... ❤ ❤ -
Apex: A simple tool for managing and deploying AWS lambda
functions. On top of that, you can write your lambda function in basically any language you want with the help of a Node.js shim. -
0101001010 1010110101 0100101001 1010101... 0101001010 1010110101 0100101001 1010101... child_process STDIN
STDOUT go build... { "request": { "type": "IntentRequest", "locale": "de_DE", "intent": { ... } } } apex deploy -
var child = require('child_process'); var byline = require('./byline'); var
proc = child.spawn('./main', { stdio: ['pipe', 'pipe', process.stderr] }); var out = byline(proc.stdout); out.on('data', function(line) { if (process.env.DEBUG_SHIM) console.log('[shim] parsing: %j', line); var msg = JSON.parse(line); ctx.done(msg.error, msg.value) }); exports.handle = function(event, context) { ctx = context; proc.stdin.write(JSON.stringify({ "event": event, "context": context })+'\n'); }; NODE.JS SHIM -
var child = require('child_process'); var byline = require('./byline'); var
proc = child.spawn('./main', { stdio: ['pipe', 'pipe', process.stderr] }); var out = byline(proc.stdout); out.on('data', function(line) { if (process.env.DEBUG_SHIM) console.log('[shim] parsing: %j', line); var msg = JSON.parse(line); ctx.done(msg.error, msg.value) }); exports.handle = function(event, context) { ctx = context; proc.stdin.write(JSON.stringify({ "event": event, "context": context })+'\n'); }; NODE.JS SHIM -
var child = require('child_process'); var byline = require('./byline'); var
proc = child.spawn('./main', { stdio: ['pipe', 'pipe', process.stderr] }); var out = byline(proc.stdout); out.on('data', function(line) { if (process.env.DEBUG_SHIM) console.log('[shim] parsing: %j', line); var msg = JSON.parse(line); ctx.done(msg.error, msg.value) }); exports.handle = function(event, context) { ctx = context; proc.stdin.write(JSON.stringify({ "event": event, "context": context })+'\n'); }; NODE.JS SHIM -
var child = require('child_process'); var byline = require('./byline'); var
proc = child.spawn('./main', { stdio: ['pipe', 'pipe', process.stderr] }); var out = byline(proc.stdout); out.on('data', function(line) { if (process.env.DEBUG_SHIM) console.log('[shim] parsing: %j', line); var msg = JSON.parse(line); ctx.done(msg.error, msg.value) }); exports.handle = function(event, context) { ctx = context; proc.stdin.write(JSON.stringify({ "event": event, "context": context })+'\n'); }; NODE.JS SHIM -
var child = require('child_process'); var byline = require('./byline'); var
proc = child.spawn('./main', { stdio: ['pipe', 'pipe', process.stderr] }); var out = byline(proc.stdout); out.on('data', function(line) { if (process.env.DEBUG_SHIM) console.log('[shim] parsing: %j', line); var msg = JSON.parse(line); ctx.done(msg.error, msg.value) }); exports.handle = function(event, context) { ctx = context; proc.stdin.write(JSON.stringify({ "event": event, "context": context })+'\n'); }; NODE.JS SHIM -
package main import ( "encoding/json" "github.com/apex/go-apex" )
func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { }) } -
package main import ( "encoding/json" "github.com/apex/go-apex" )
func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { }) } -
package main import ( "encoding/json" "github.com/apex/go-apex" )
func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { }) } -
package main import ( "encoding/json" "github.com/apex/go-apex" g "github.com/b00giZm/golexa"
) func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { app := g.Default() return app.Process(event) }) } -
package main import ( "encoding/json" "github.com/apex/go-apex" g "github.com/b00giZm/golexa"
) func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { app := g.Default() app.OnIntent(func(a *g.Alexa, intent *g.Intent, req *g.Request, session *g.Session) *g.Response) { }) return app.Process(event) }) } -
package main import ( "encoding/json" "github.com/apex/go-apex" g "github.com/b00giZm/golexa"
) func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { app := g.Default() app.OnIntent(func(a *g.Alexa, intent *g.Intent, req *g.Request, session *g.Session) *g.Response) { if intent.Name != "GetRecipeOfTheDay" { return nil } }) return app.Process(event) }) } -
package main import ( "chefkoch.de/chefkoch-skill/lib/api" "encoding/json" "github.com/apex/go-apex" g
"github.com/b00giZm/golexa" ) func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { apiClient := api.DefaultClient() app := g.Default() app.OnIntent(func(a *g.Alexa, intent *g.Intent, req *g.Request, session *g.Session) *g.Response) { if intent.Name != "GetRecipeOfTheDay" { return nil } recipe, _ := apiClient.FetchRecipeOfTheDay() }) return app.Process(event) }) } -
package main import ( "chefkoch.de/chefkoch-skill/lib/api" "encoding/json" "github.com/apex/go-apex" g
"github.com/b00giZm/golexa" ) func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { apiClient := api.DefaultClient() app := g.Default() app.OnIntent(func(a *g.Alexa, intent *g.Intent, req *g.Request, session *g.Session) *g.Response) { if intent.Name != "GetRecipeOfTheDay" { return nil } recipe, err := apiClient.FetchRecipeOfTheDay() if err != nil { // Handle error } }) return app.Process(event) }) } -
package main import ( "chefkoch.de/chefkoch-skill/lib/api" "encoding/json" "github.com/apex/go-apex" g
"github.com/b00giZm/golexa" ) func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { apiClient := api.DefaultClient() app := g.Default() app.OnIntent(func(a *g.Alexa, intent *g.Intent, req *g.Request, session *g.Session) *g.Response) { if intent.Name != "GetRecipeOfTheDay" { return nil } recipe, _ := apiClient.FetchRecipeOfTheDay() }) return app.Process(event) }) } -
package main import ( "chefkoch.de/chefkoch-skill/lib/api" "chefkoch.de/chefkoch-skill/lib/helpers" "encoding/json" "github.com/apex/go-apex" g
"github.com/b00giZm/golexa" ) func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { apiClient := api.DefaultClient() templateParser := helpers.DefaultTemplateParser() app := g.Default() app.OnIntent(func(a *g.Alexa, intent *g.Intent, req *g.Request, session *g.Session) *g.Response) { if intent.Name != "GetRecipeOfTheDay" { return nil } recipe, _ := apiClient.FetchRecipeOfTheDay() str, _ := templateParser.Parse("GetRecipeOfTheDay", recipe) }) return app.Process(event) }) } -
package main import ( "chefkoch.de/chefkoch-skill/lib/api" "chefkoch.de/chefkoch-skill/lib/helpers" "encoding/json" "github.com/apex/go-apex" g
"github.com/b00giZm/golexa" ) func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { apiClient := api.DefaultClient() templateParser := helpers.DefaultTemplateParser() app := g.Default() app.OnIntent(func(a *g.Alexa, intent *g.Intent, req *g.Request, session *g.Session) *g.Response) { if intent.Name != "GetRecipeOfTheDay" { return nil } recipe, _ := apiClient.FetchRecipeOfTheDay() str, _ := templateParser.Parse("GetRecipeOfTheDay", recipe) return a.Response() }) return app.Process(event) }) } -
package main import ( "chefkoch.de/chefkoch-skill/lib/api" "chefkoch.de/chefkoch-skill/lib/helpers" "encoding/json" "github.com/apex/go-apex" g
"github.com/b00giZm/golexa" ) func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { apiClient := api.DefaultClient() templateParser := helpers.DefaultTemplateParser() app := g.Default() app.OnIntent(func(a *g.Alexa, intent *g.Intent, req *g.Request, session *g.Session) *g.Response) { if intent.Name != "GetRecipeOfTheDay" { return nil } recipe, _ := apiClient.FetchRecipeOfTheDay() str, _ := templateParser.Parse("GetRecipeOfTheDay", recipe) return a.Response().AddPlainTextSpeech(str) }) return app.Process(event) }) } -
package main import ( "chefkoch.de/chefkoch-skill/lib/api" "chefkoch.de/chefkoch-skill/lib/helpers" "encoding/json" "github.com/apex/go-apex" g
"github.com/b00giZm/golexa" ) func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { apiClient := api.DefaultClient() templateParser := helpers.DefaultTemplateParser() app := g.Default() app.OnIntent(func(a *g.Alexa, intent *g.Intent, req *g.Request, session *g.Session) *g.Response) { if intent.Name != "GetRecipeOfTheDay" { return nil } recipe, _ := apiClient.FetchRecipeOfTheDay() str, _ := templateParser.Parse("GetRecipeOfTheDay", recipe) return a.Response().AddPlainTextSpeech(str).AddSessionAttributes(g.SessionAttributes{ "currentRecipe": recipe, }) }) return app.Process(event) }) } -
package main import ( "chefkoch.de/chefkoch-skill/lib/api" "chefkoch.de/chefkoch-skill/lib/helpers" "encoding/json" "github.com/apex/go-apex" g
"github.com/b00giZm/golexa" ) func main() { apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { apiClient := api.DefaultClient() templateParser := helpers.DefaultTemplateParser() app := g.Default() app.OnIntent(func(a *g.Alexa, intent *g.Intent, req *g.Request, session *g.Session) *g.Response) { if intent.Name != "GetRecipeOfTheDay" { return nil } recipe, _ := apiClient.FetchRecipeOfTheDay() str, _ := templateParser.Parse("GetRecipeOfTheDay", recipe) return a.Response().AddPlainTextSpeech(str).AddSessionAttributes(g.SessionAttributes{ "currentRecipe": recipe, }).KeepSessionAlive() }) return app.Process(event) }) } -
type RecipeOfTheDayHandler struct { BaseHandler } func (h *RecipeOfTheDayHandler) HandleIntent(a
*g.Alexa, intent *g.Intent, req *g.Request, session *g.Session) *g.Response { ... } ... alexaHandlers := &[]golexa.Handler{ NewWelcomeHandler(client, parser, ctx), NewLogHandler(), NewRecipeOfTheDayHandler(client, parser, ctx), NewRecipesOfCategoryHandler(client, parser, ctx, categories), NewIngredientsHandler(client, parser, ctx, categories), NewSendRecipeHandler(client, parser, ctx, categories), ... NewAmazonIntentsHandler(client, parser, ctx, categories), NewGoodByeHandler(client, parser, ctx), } app := g.Init(alexaHandlers) ... apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) { return app.Process(event) }) -
Skill development (+ unit tests) took less than a week
Very stable builds (benefits of a compiled language) Deployments with Apex in less than 10 seconds Huge amount of boilerplate (missing language features like Generics etc.) Debugging with Apex can be a PITA (must use STDERR for logging as STDOUT is taken) -
◻ Evaluation/Implementation of initial user feedback (e.g. ingredients search) ◻
Differentiation between new and returning users ◻ Account-Linking with Chefkoch.de (Business Value!) ◻ Migration to our own infrastructure (?) -
THANK YOU! …ANY QUESTIONS? Echo Product Images, AWS Lambda &
Alexa SkillKit Logos (c) by Amazon