alexa

package module
v0.0.0-...-0236686 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

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

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

func SupportsDisplay

func SupportsDisplay(ctx *Context) bool

SupportsDisplay returns true if this skill was initiated from a device that supports video, false otherwise

func SupportsVideo

func SupportsVideo(ctx *Context) bool

SupportsVideo returns true if this skill was initiated from a device that supports video, false otherwise

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 AudioImage

type AudioImage struct {
	ContentDescription string             `json:"contentDescription,omitempty"`
	Sources            []AudioImageSource `json:"sources"`
}

AudioImage contains the data for images used within Audio Directives

type AudioImageSource

type AudioImageSource struct {
	URL          string  `json:"url"`
	Size         *string `json:"size,omitempty"`
	WidthPixels  *int    `json:"widthPixels,omitempty"`
	HeightPixels *int    `json:"heightPixels,omitempty"`
}

AudioImageSource contains the data for an image

type AudioItem

type AudioItem struct {
	Stream   Stream         `json:"stream"`
	Metadata *AudioMetadata `json:"metadata,omitempty"`
}

AudioItem contains an audio Stream definition for playback.

type AudioMetadata

type AudioMetadata struct {
	Title           string      `json:"title,omitempty"`
	Subtitle        string      `json:"subtitle,omitempty"`
	Art             *AudioImage `json:"art,omitempty"`
	BackgroundImage *AudioImage `json:"backgroundImage,omitempty"`
}

AudioMetadata contains instructions for providing audio metadata

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 ClearBehavior

type ClearBehavior string

ClearBehavior Describes the clear behavior

const (
	// ClearEnqueued ...
	ClearEnqueued ClearBehavior = "CLEAR_ENQUEUED"
	// ClearAll ...
	ClearAll ClearBehavior = "CLEAR_ALL"
)

type ClearQueueDirective

type ClearQueueDirective struct {
	Type          string        `json:"type"`
	ClearBehavior ClearBehavior `json:"clearBehavior"`
}

ClearQueueDirective Clears the audio playback queue.

type Context

type Context struct {
	System struct {
		Device struct {
			DeviceID            string `json:"deviceId"`
			SupportedInterfaces struct {
				AudioPlayer struct{}  `json:"AudioPlayer"`
				Display     *struct{} `json:"Display"`
				VideoApp    *struct{} `json:"VideoApp"`
			} `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"`
}

IntentSlot contains the data for one Slot

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 PlayAudioDirective

type PlayAudioDirective struct {
	Type         string       `json:"type"`
	PlayBehavior PlayBehavior `json:"playBehavior"`
	AudioItem    AudioItem    `json:"audioItem"`
}

PlayAudioDirective Sends Alexa a command to stream the audio file identified by the specified audioItem.

type PlayBehavior

type PlayBehavior string

PlayBehavior Describes playback behavior

const (
	// ReplaceAll ...
	ReplaceAll PlayBehavior = "REPLACE_ALL"
	// Enqueue ...
	Enqueue PlayBehavior = "ENQUEUE"
	// ReplaceEnqueued ...
	ReplaceEnqueued PlayBehavior = "REPLACE_ENQUEUED"
)

type RenderDocumentDirective

type RenderDocumentDirective struct {
	Type         string        `json:"type"`
	Token        string        `json:"token"`
	Document     interface{}   `json:"document"`
	DataSources  interface{}   `json:"datasources,omitempty"`
	Transformers []interface{} `json:"transformers,omitempty"`
}

RenderDocumentDirective contain directives for use with Amazon APL documents

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 {
	Type        string `json:"type"`
	Timestamp   string `json:"timestamp"`
	RequestID   string `json:"requestId"`
	Locale      string `json:"locale"`
	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) AddClearQueueAudioPlayerDirective

func (r *Response) AddClearQueueAudioPlayerDirective(clearBehavior ClearBehavior)

AddClearQueueAudioPlayerDirective adds an AudioPlayer directive to clear the audio queue.

func (*Response) AddDialogDirective

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

AddDialogDirective adds a Dialog directive to the Response.

func (*Response) AddPlayAudioPlayerDirective

func (r *Response) AddPlayAudioPlayerDirective(playBehavior PlayBehavior, streamToken, url string, offsetInMilliseconds int, metadata *AudioMetadata)

AddPlayAudioPlayerDirective adds an AudioPlayer directive to the Response.

func (*Response) AddRenderDocumentDirective

func (r *Response) AddRenderDocumentDirective(token string, document interface{}, datasources interface{}, transforms []interface{})

AddRenderDocumentDirective adds a RenderDocument directive to the Response.

func (*Response) AddStopAudioPlayerDirective

func (r *Response) AddStopAudioPlayerDirective()

AddStopAudioPlayerDirective adds an AudioPlayer directive to stop the Audio.

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]string `json:"string"`
	} `json:"attributes"`
	User struct {
		UserID      string            `json:"userId"`
		AccessToken string            `json:"accessToken"`
		Permissions map[string]string `json:"permissions,omitempty"`
	} `json:"user"`
	Application struct {
		ApplicationID string `json:"applicationId"`
	} `json:"application"`
}

Session contains the session data from the Alexa request.

type StopAudioDirective

type StopAudioDirective struct {
	Type string `json:"type"`
}

StopAudioDirective Stops the current audio playback.

type Stream

type Stream struct {
	Token                 string `json:"token"`
	URL                   string `json:"url"`
	ExpectedPreviousToken string `json:"expectedPreviousToken,omitempty"`
	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