Skip to content

Instantly share code, notes, and snippets.

@stnc
Last active July 11, 2023 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stnc/cfa72f5c3eb44ab3cf10450a07dbcb9c to your computer and use it in GitHub Desktop.
Save stnc/cfa72f5c3eb44ab3cf10450a07dbcb9c to your computer and use it in GitHub Desktop.
golang pointer example 2
package main
import (
"encoding/json"
"fmt"
"log"
)
type EchoResponse struct {
Version string `json:"version"`
SessionAttributes map[string]interface{} `json:"sessionAttributes,omitempty"`
Response EchoRespBody `json:"response"`
}
// EchoRespBody contains the body of the response to be sent back to the Alexa service.
// This includes things like the text that should be spoken or any cards that should
// be shown in the Alexa companion app.
type EchoRespBody struct {
OutputSpeech *EchoRespPayload `json:"outputSpeech,omitempty"`
Card *EchoRespPayload `json:"card,omitempty"`
ShouldEndSession bool `json:"shouldEndSession"`
}
// EchoRespPayload contains the interesting parts of the Echo response including text to be spoken,
// card attributes, and images.
type EchoRespPayload struct {
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Text string `json:"text,omitempty"`
SSML string `json:"ssml,omitempty"`
Content string `json:"content,omitempty"`
}
// OutputSpeech will replace any existing text that should be spoken with this new value. If the output
// needs to be constructed in steps or special speech tags need to be used, see the `SSMLTextBuilder`.
func (r *EchoResponse) OutputSpeech(text string) *EchoResponse {
r.Response.OutputSpeech = &EchoRespPayload{
Type: "PlainText",
Text: text,
}
return r
}
// Card will add a card to the Alexa app's response with the provided title and content strings.
func (r *EchoResponse) Card(title string, content string) *EchoResponse {
return r.SimpleCard(title, content)
}
// SimpleCard will indicate that a card should be included in the Alexa companion app as part of the response.
// The card will be shown with the provided title and content.
func (r *EchoResponse) SimpleCard(title string, content string) *EchoResponse {
r.Response.Card = &EchoRespPayload{
Type: "Simple",
Title: title,
Content: content,
}
return r
}
func main() {
var echoResp EchoResponse
echoResp.OutputSpeech("Hello world from my new Echo test app!").Card("Hello World", "This is a test card.")
empJSON, err := json.MarshalIndent(echoResp, "", " ")
if err != nil {
log.Fatalf(err.Error())
}
fmt.Printf("MarshalIndent funnction output\n %s\n", string(empJSON))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment