alexa

package module
v0.0.0-...-52c9a4b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2020 License: Apache-2.0 Imports: 7 Imported by: 10

README

ericdaugherty/alexa-skills-kit-golang

alexa-skills-kit-golang is a lightweight port of the Amazon alexa-skills-kit-java SDK and Samples.

License godoc

Usage

This explanation assumes familiarity with with AWS Documentation. Please review Developing an Alexa Skill as a Lambda Function before proceeding. This SDK addresses some of the steps documented here for you, but you should be familiar with the entire process.

The samples directory provides example usage.

The Alexa struct is the initial interface point with the SDK. Alexa must be initialized first. The struct is defined as:

type Alexa struct {
    ApplicationID       string
    RequestHandler      RequestHandler
    IgnoreApplicationID bool
    IgnoreTimestamp     bool
}

The ApplicationID must match the ApplicationID defined in the Alexa Skills

The RequestHandler is an interface that must be implemented, and is called to handle requests.

IgnoreApplicationID and IgnoreTimestamp should be used during debugging to test with hard-coded requests.

Requests from Alexa should be passed into the Alexa.ProcessRequest method.

func (alexa *Alexa) ProcessRequest(context context.Context, requestEnv *RequestEnvelope) (*ResponseEnvelope, error)

This method takes the incoming request and validates it, and the calls the appropriate callback methods on the RequestHandler interface implementation.

The ResponseEnvelope is returned and can be converted to JSON to be passed back to the Alexa skill.

RequestHandler interface is defined as:

type RequestHandler interface {
	OnSessionStarted(context.Context, *Request, *Session, *Context, *Response) error
	OnLaunch(context.Context, *Request, *Session, *Context, *Response) error
	OnIntent(context.Context, *Request, *Session, *Context, *Response) error
	OnSessionEnded(context.Context, *Request, *Session, *Context, *Response) error
}

For a summary of these methods, please see the Handling Requests Sent By Alexa documentation.

You can directly manipulate the Response struct, but it is not initialized by default and use of the connivence methods is recommended.

These methods include:

func (r *Response) SetSimpleCard(title string, content string)
func (r *Response) SetStandardCard(title string, text string, smallImageURL string, largeImageURL string)
func (r *Response) SetLinkAccountCard()
func (r *Response) SetOutputText(text string)
func (r *Response) SetOutputSSML(ssml string)
func (r *Response) SetRepromptText(text string)
func (r *Response) SetRepromptSSML(ssml string)

And more. These methods handle initializing any required struts within the Response struct as well as setting all required fields.

samples

HelloWorld

Limitations

This version does not support use as a standalone web server as it does not implement any of the HTTPS validation. It was developed to be used as an AWS Lambda function using AWS Labda Go support.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRequestEnvelopeNil = errors.New("request envelope was nil")

ErrRequestEnvelopeNil reports that the request envelope was nil there might be edge case which causes panic if for whatever reason this object is empty

Functions

This section is empty.

Types

type Alexa

type Alexa struct {
	ApplicationID       string
	RequestHandler      RequestHandler
	IgnoreApplicationID bool
	IgnoreTimestamp     bool
}

Alexa defines the primary interface to use to create an Alexa request handler.

func (*Alexa) ProcessRequest

func (alexa *Alexa) ProcessRequest(ctx context.Context, requestEnv *RequestEnvelope) (*ResponseEnvelope, error)

ProcessRequest handles a request passed from Alexa

func (*Alexa) SetTimestampTolerance

func (alexa *Alexa) SetTimestampTolerance(seconds int)

SetTimestampTolerance sets the maximum number of seconds to allow between the current time and the request Timestamp. Default value is 150 seconds.

type AudioItem

type AudioItem struct {
	Stream Stream `json:"stream,omitempty"`
}

AudioItem contains an audio Stream definition for playback.

type AudioPlayerDirective

type AudioPlayerDirective struct {
	Type         string     `json:"type"`
	PlayBehavior string     `json:"playBehavior,omitempty"`
	AudioItem    *AudioItem `json:"audioItem,omitempty"`
}

AudioPlayerDirective contains device level instructions on how to handle the response.

type Card

type Card struct {
	Type    string `json:"type"`
	Title   string `json:"title,omitempty"`
	Content string `json:"content,omitempty"`
	Text    string `json:"text,omitempty"`
	Image   *Image `json:"image,omitempty"`
}

Card contains the data displayed to the user by the Alexa app.

type Context

type Context struct {
	System struct {
		Device struct {
			DeviceID            string `json:"deviceId"`
			SupportedInterfaces struct {
				AudioPlayer struct {
				} `json:"AudioPlayer"`
			} `json:"supportedInterfaces"`
		} `json:"device"`
		Application struct {
			ApplicationID string `json:"applicationId"`
		} `json:"application"`
		User struct {
			UserID      string `json:"userId"`
			AccessToken string `json:"accessToken"`
			Permissions struct {
				ConsentToken string `json:"consentToken"`
			} `json:"permissions"`
		} `json:"user"`
		APIEndpoint    string `json:"apiEndpoint"`
		APIAccessToken string `json:"apiAccessToken"`
	} `json:"System"`
	AudioPlayer struct {
		PlayerActivity       string `json:"playerActivity"`
		Token                string `json:"token"`
		OffsetInMilliseconds int    `json:"offsetInMilliseconds"`
	} `json:"AudioPlayer"`
}

Context contains the context data from the Alexa Request.

type DialogDirective

type DialogDirective struct {
	Type          string  `json:"type"`
	SlotToElicit  string  `json:"slotToElicit,omitempty"`
	SlotToConfirm string  `json:"slotToConfirm,omitempty"`
	UpdatedIntent *Intent `json:"updatedIntent,omitempty"`
}

DialogDirective contains directives for use in Dialog prompts.

type Image

type Image struct {
	SmallImageURL string `json:"smallImageUrl,omitempty"`
	LargeImageURL string `json:"largeImageUrl,omitempty"`
}

Image provides URL(s) to the image to display in resposne to the request.

type Intent

type Intent struct {
	Name               string                `json:"name"`
	ConfirmationStatus string                `json:"confirmationStatus,omitempty"`
	Slots              map[string]IntentSlot `json:"slots"`
}

Intent contains the data about the Alexa Intent requested.

type IntentSlot

type IntentSlot struct {
	Name               string       `json:"name"`
	ConfirmationStatus string       `json:"confirmationStatus,omitempty"`
	Value              string       `json:"value"`
	Resolutions        *Resolutions `json:"resolutions,omitempty"`

	// SlotValue is a BETA field and may be removed by Amazon without warning.
	// See https://developer.amazon.com/en-US/docs/alexa/custom-skills/collect-multiple-values-in-a-slot.html.
	SlotValue *IntentSlotValue `json:"slotValue,omitempty"`
}

IntentSlot contains the data for one Slot

type IntentSlotValue

type IntentSlotValue struct {
	Type        string             `json:"type"`
	Values      []*IntentSlotValue `json:"values"`
	Value       string             `json:"value"`
	Resolutions *Resolutions       `json:"resolutions,omitempty"`
}

IntentSlotValue contains the value or values of a slot. When Type == "Simple", Value and Resolutions are populated. When Type == "List", Values is populated.

type OutputSpeech

type OutputSpeech struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
	SSML string `json:"ssml,omitempty"`
}

OutputSpeech contains the data the defines what Alexa should say to the user.

type Reprompt

type Reprompt struct {
	OutputSpeech *OutputSpeech `json:"outputSpeech,omitempty"`
}

Reprompt contains data about whether Alexa should prompt the user for more data.

type Request

type Request struct {
	Locale      string `json:"locale"`
	Timestamp   string `json:"timestamp"`
	Type        string `json:"type"`
	RequestID   string `json:"requestId"`
	DialogState string `json:"dialogState"`
	Intent      Intent `json:"intent"`
	Name        string `json:"name"`
}

Request contains the data in the request within the main request.

type RequestEnvelope

type RequestEnvelope struct {
	Version string   `json:"version"`
	Session *Session `json:"session"`
	Request *Request `json:"request"`
	Context *Context `json:"context"`
}

RequestEnvelope contains the data passed from Alexa to the request handler.

type RequestHandler

type RequestHandler interface {
	OnSessionStarted(context.Context, *Request, *Session, *Context, *Response) error
	OnLaunch(context.Context, *Request, *Session, *Context, *Response) error
	OnIntent(context.Context, *Request, *Session, *Context, *Response) error
	OnSessionEnded(context.Context, *Request, *Session, *Context, *Response) error
}

RequestHandler defines the interface that must be implemented to handle Alexa Requests

type Resolutions

type Resolutions struct {
	ResolutionsPerAuthority []struct {
		Authority string `json:"authority"`
		Status    struct {
			Code string `json:"code"`
		} `json:"status"`
		Values []struct {
			Value struct {
				Name string `json:"name"`
				ID   string `json:"id"`
			} `json:"value"`
		} `json:"values"`
	} `json:"resolutionsPerAuthority"`
}

Resolutions contain the (optional) ID of a slot

type Response

type Response struct {
	OutputSpeech     *OutputSpeech `json:"outputSpeech,omitempty"`
	Card             *Card         `json:"card,omitempty"`
	Reprompt         *Reprompt     `json:"reprompt,omitempty"`
	Directives       []interface{} `json:"directives,omitempty"`
	ShouldSessionEnd bool          `json:"shouldEndSession"`
}

Response contains the body of the response.

func (*Response) AddAudioPlayer

func (r *Response) AddAudioPlayer(playerType, playBehavior, streamToken, url string, offsetInMilliseconds int)

AddAudioPlayer adds an AudioPlayer directive to the Response.

func (*Response) AddDialogDirective

func (r *Response) AddDialogDirective(dialogType, slotToElicit, slotToConfirm string, intent *Intent)

AddDialogDirective adds a Dialog directive to the Response.

func (*Response) SetLinkAccountCard

func (r *Response) SetLinkAccountCard()

SetLinkAccountCard creates a new LinkAccount card.

func (*Response) SetOutputSSML

func (r *Response) SetOutputSSML(ssml string)

SetOutputSSML sets the OutputSpeech type to ssml and sets the value specified.

func (*Response) SetOutputText

func (r *Response) SetOutputText(text string)

SetOutputText sets the OutputSpeech type to text and sets the value specified.

func (*Response) SetRepromptSSML

func (r *Response) SetRepromptSSML(ssml string)

SetRepromptSSML created a Reprompt if needed and sets the OutputSpeech type to ssml and sets the value specified.

func (*Response) SetRepromptText

func (r *Response) SetRepromptText(text string)

SetRepromptText created a Reprompt if needed and sets the OutputSpeech type to text and sets the value specified.

func (*Response) SetSimpleCard

func (r *Response) SetSimpleCard(title string, content string)

SetSimpleCard creates a new simple card with the specified content.

func (*Response) SetStandardCard

func (r *Response) SetStandardCard(title string, text string, smallImageURL string, largeImageURL string)

SetStandardCard creates a new standard card with the specified content.

type ResponseEnvelope

type ResponseEnvelope struct {
	Version           string                 `json:"version"`
	SessionAttributes map[string]interface{} `json:"sessionAttributes,omitempty"`
	Response          *Response              `json:"response"`
}

ResponseEnvelope contains the Response and additional attributes.

type Session

type Session struct {
	New        bool   `json:"new"`
	SessionID  string `json:"sessionId"`
	Attributes struct {
		String map[string]interface{} `json:"string"`
	} `json:"attributes"`
	User struct {
		UserID      string `json:"userId"`
		AccessToken string `json:"accessToken"`
	} `json:"user"`
	Application struct {
		ApplicationID string `json:"applicationId"`
	} `json:"application"`
}

Session contains the session data from the Alexa request.

type Stream

type Stream struct {
	Token                string `json:"token"`
	URL                  string `json:"url"`
	OffsetInMilliseconds int    `json:"offsetInMilliseconds"`
}

Stream contains instructions on playing an audio stream.

Directories

Path Synopsis
samples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL