github.com/arienmalec/alexa-go@v0.0.0-20181025212142-975687393e90/README.md (about) 1 # Go Request/Response and Helpers for Alexa Skill Services 2 3 ### Install 4 5 ```console 6 go get github.com/arienmalec/alexa-go 7 ``` 8 9 ### Usage 10 11 #### Response 12 13 The `alexa.Response` struct implements the AWS Alexa Skill response, and contains a helper for simple speach responses. 14 15 The following is a minimal AWS Lambda implementing "Hello, World" as an Alexa skill in Go. 16 17 ```go 18 package main 19 20 import ( 21 "github.com/arienmalec/alexa-go" 22 "github.com/aws/aws-lambda-go/lambda" 23 ) 24 25 // Handler is the lambda hander 26 func Handler() (alexa.Response, error) { 27 return alexa.NewSimpleResponse("Saying Hello", "Hello, World"), nil 28 } 29 30 func main() { 31 lambda.Start(Handler) 32 } 33 ``` 34 35 ### Request 36 37 The `alexa.Request` struct implements the AWS Alexa Skill request, and contains some constants for locales and intents. 38 39 The following is a Lambda delivering localized content to users and handling multiple intents. 40 41 ```go 42 package main 43 44 import ( 45 "github.com/arienmalec/alexa-go" 46 "github.com/aws/aws-lambda-go/lambda" 47 ) 48 49 // DispatchIntents dispatches each intent to the right handler 50 func DispatchIntents(request alexa.Request) alexa.Response { 51 var response alexa.Response 52 switch request.Body.Intent.Name { 53 case "hello": 54 response = handleHello(request) 55 case alexa.HelpIntent: 56 response = handleHelp() 57 } 58 59 return response 60 } 61 62 func handleHello(request alexa.Request) alexa.Response { 63 title := "Saying Hello" 64 var text string 65 switch request.Body.Locale { 66 case alexa.LocaleAustralianEnglish: 67 text = "G'day mate!" 68 case alexa.LocaleGerman: 69 text = "Hallo Welt" 70 case alexa.LocaleJapanese: 71 text = "こんにちは世界" 72 default: 73 text = "Hello, World" 74 } 75 return alexa.NewSimpleResponse(title, text) 76 } 77 78 func handleHelp() alexa.Response { 79 return alexa.NewSimpleResponse("Help for Hello", "To receive a greeting, ask hello to say hello") 80 } 81 82 // Handler is the lambda hander 83 func Handler(request alexa.Request) (alexa.Response, error) { 84 return DispatchIntents(request), nil 85 } 86 87 func main() { 88 lambda.Start(Handler) 89 } 90 ``` 91 92 ### Credits 93 94 Request/Response struct layout influenced by `https://github.com/mikeflynn/go-alexa` which was written before Go was an AWS Lambda native language. 95