valexa

package module
v1.0.2-0...-1fb922d Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2020 License: Apache-2.0 Imports: 20 Imported by: 0

README

valexa Build Status

golang valexa,亚马逊 Echo Alexa 自定义版本

列表:

type Alexa struct{                                                                              // Alexa服务
    AppIdAttr       string                                                                          // 程序的名称,如:appid
}
    func (T *Alexa) SetEchoApp(alexaAppId string, app *EchoApplication)                             // 设置echoApp
    func (T *Alexa) ServeHTTP(w http.ResponseWriter, r *http.Request)                               // 服务器启用
func NewEchoResponse() *EchoResponse                                                            // Echo响应
    func (T *EchoResponse) SetEndSession(ok bool)                                                   // 结束对话
    func (T *EchoResponse) OutputSpeech(text string) *EchoResponse                                  // 输出语音
    func (T *EchoResponse) OutputSpeechSSML(text string) *EchoResponse                              // 输出语音(有语气)
    func (T *EchoResponse) SimpleCard(title string, content string) *EchoResponse                   // 纯文本卡片
    func (T *EchoResponse) StandardCard(title string, text string, smallImg string, largeImg string) *EchoResponse      // 带图文本卡片
    func (T *EchoResponse) LinkAccountCard() *EchoResponse                                          // 登录卡片
    func (T *EchoResponse) RepromptText(text string) *EchoResponse                                  // 回应
    func (T *EchoResponse) RepromptSSML(text string) *EchoResponse                                  // 回应(有语气)
    func (T *EchoResponse) String() string                                                          // 字符串
type EchoRequest struct {                                                                       // Echo请求
    Context EchoRequestContext                                                                      // 上下文
    Request EchoRequestRequest                                                                      // 请求
    Session EchoRequestSession                                                                      // 会话
    Version string                                                                                  // 版本
}
    func (T *EchoRequest) VerifyTimestamp(second int) bool                                              // 验证请求时间
    func (T *EchoRequest) GetLocale() string															// 语言类型
    func (T *EchoRequest) GetApplicationID() string                                                     // 程序ID
    func (T *EchoRequest) GetSessionID() string                                                         // 会话ID
    func (T *EchoRequest) GetUserID() string                                                            // 用户ID
    func (T *EchoRequest) GetAccessToken() string                                                       // 用户Token
    func (T *EchoRequest) GetRequestType() string                                                       // 请求类型
    func (T *EchoRequest) GetIntentName() string                                                        // 意图名称
    func (T *EchoRequest) GetSlots() (slots map[string]*EchoRequestRequestIntentSlot, err error)        // 所有类型意图对象(含空值)
	func (T *EchoRequest) GetValueSlots() (slots map[string]*EchoRequestRequestIntentSlot, err error)	// 所有类型意图对象(不含空值)
    func (T *EchoRequest) GetSlotNames() (names []string)                                               // 所有意图名称
    func (T *EchoRequest) GetSlotValue(slotName string) (val string, err error)                         // 意图值
    func (T *EchoRequest) GetSlot(slotName string) (slot *EchoRequestRequestIntentSlot, err error)      // 意图对象
type EchoIntent struct{                                                                             // Echo意图
    Request     *EchoRequest                                                                            // Echo请求
    Response    *EchoResponse                                                                           // Echo响应
    App         *EchoApplication                                                                        // Echo配置
}
func (T *EchoIntent) ServeHTTP(w http.ResponseWriter, r *http.Request)
type EchoApplication struct {                                                                       // Echo程序的配置
    Version                 []string                                                                    // 支持的版本号
    IsDevelop               bool                                                                      	// 调试
    ValidReqTimestamp       int                                                                         // 有效时间,秒为单位
    CertFolder              string                                                                      // 证书保存目录
    HandleFunc              http.HandlerFunc                                                            // 原生处理函数
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DepthField

func DepthField(s interface{}, index ...interface{}) (field interface{}, err error)

DepthField 快速深入读取字段

s interface{}        Struct
ndex ... interface{} 字段
field interface{}    字段
err error            错误
例:
type A struct {
 B
}
type B struct {
 C
 F map[string]string
 G []string
}
type C struct {
 D int
}
func main(){
 a := A{}
    fidld, err := DepthField(a, "B", "C", "D")
    fmt.Println(fidld, err)
    //0 <nil>
   }

func ForMethod

func ForMethod(x interface{}) string

ForMethod 遍历方法

x interface{}     类型
string            字符串

func ForType

func ForType(x interface{}) string

ForType 遍历字段

x interface{}     类型
string            字符串

func InDirect

func InDirect(v reflect.Value) reflect.Value

InDirect 指针到内存

v reflect.Value        映射引用为真实内存地址
reflect.Value          真实内存地址

func TypeSelect

func TypeSelect(v reflect.Value) interface{}

TypeSelect 类型选择

v reflect.Value        映射一种未知类型的变量
interface{}            读出v的值

Types

type Alexa

type Alexa struct {
	//如:https://www.xxx.com/?appid=amzn1.ask.skill.594232d8-4095-499b-9ba3-11701107834s&type=1
	//这个 appid 是程序属性名称,其中的值是 amzn1.ask.skill.594232d8-4095-499b-9ba3-11701107834s
	//这个 type 是程序类型属性名称,暂时仅支持 Echo,值也就是 1。其它值不支持
	//在Alexa后台,Configuration->Global Fields->Endpoint->Service Endpoint Type->选择HTTPS
	//Default 填入 https://www.xxx.com/?appid=amzn1.ask.skill.594232d8-4095-499b-9ba3-11701107834s&type=1
	AppIdAttr string //程序的名称,如:appid
	// contains filtered or unexported fields
}

func (*Alexa) ServeHTTP

func (T *Alexa) ServeHTTP(w http.ResponseWriter, r *http.Request)

服务处理

w http.ResponseWriter	http响应对象
r *http.Request			http请求对象

func (*Alexa) SetEchoApp

func (T *Alexa) SetEchoApp(alexaAppId string, app *EchoApplication)

设置Echo程序

alexaAppId string		Alexa程序ID名
app *EchoApplication	Echo对象

type EchoApplication

type EchoApplication struct {
	Version           []string         // 支持的版本号
	IsDevelop         bool             // 调试
	ValidReqTimestamp int              // 有效时间,秒为单位
	CertFolder        string           // 证书保存目录
	HandleFunc        http.HandlerFunc // 原生处理函数
}

Echo程序的配置

type EchoIntent

type EchoIntent struct {
	Request  *EchoRequest     //Echo请求
	Response *EchoResponse    //Echo响应
	App      *EchoApplication //Echo配置
}

Echo意图处理

func (*EchoIntent) ServeHTTP

func (T *EchoIntent) ServeHTTP(w http.ResponseWriter, r *http.Request)

服务处理

w http.ResponseWriter	http响应对象
r *http.Request			http请求对象

type EchoRequest

type EchoRequest struct {
	Context EchoRequestContext `json:"context"`
	Request EchoRequestRequest `json:"request"`
	Session EchoRequestSession `json:"session"`
	Version string             `json:"version"`
}

func (*EchoRequest) GetAccessToken

func (T *EchoRequest) GetAccessToken() string

读取用户的令牌

string	令牌

func (*EchoRequest) GetApplicationID

func (T *EchoRequest) GetApplicationID() string

读取程序ID

string	程序ID

func (*EchoRequest) GetIntentName

func (T *EchoRequest) GetIntentName() string

读取意图名称,如标注符:{Map}

string	意图名称

func (*EchoRequest) GetLocale

func (T *EchoRequest) GetLocale() string

读取语言类型

string	语言类型

func (*EchoRequest) GetRequestType

func (T *EchoRequest) GetRequestType() string

读取意图类型,有效:LaunchRequest, IntentRequest, SessionEndedRequest

string	意图类型

func (*EchoRequest) GetSessionID

func (T *EchoRequest) GetSessionID() string

读取本次会话的ID。如果用户结束会话,再发起会话,这个ID将是一个全新ID。

string	会话ID

func (*EchoRequest) GetSlot

func (T *EchoRequest) GetSlot(slotName string) (slot *EchoRequestRequestIntentSlot, err error)

读取Solt对象

slotName	跟踪名称
slot		跟踪的对象
err			错误,如果这个名称不存在,将会返回错误

func (*EchoRequest) GetSlotNames

func (T *EchoRequest) GetSlotNames() (names []string)

读取出所有Solt名称

names	所有跟踪名称

func (*EchoRequest) GetSlotValue

func (T *EchoRequest) GetSlotValue(slotName string) (val string, err error)

读取Solt值

slotName	跟踪名称
val			跟踪的值
err			错误,如果这个名称不存在,将会返回错误

func (*EchoRequest) GetSlots

func (T *EchoRequest) GetSlots() (slots map[string]*EchoRequestRequestIntentSlot, err error)

读取出所有Solt对象,所有值,包含空值

slots	所有跟踪名称和值
err		错误,如果不是意图类型请求,调用这个方法,将会返回错误

func (*EchoRequest) GetUserID

func (T *EchoRequest) GetUserID() string

读取用户的ID

string	用户ID

func (*EchoRequest) GetValueSlots

func (T *EchoRequest) GetValueSlots() (slots map[string]*EchoRequestRequestIntentSlot, err error)

读取出所有Solt对象,非空值的

slots	所有跟踪名称和值
err		错误,如果不是意图类型请求,调用这个方法,将会返回错误

func (*EchoRequest) VerifyTimestamp

func (T *EchoRequest) VerifyTimestamp(second int) bool

验证请求是否有效

second int	允许过时多久?
bool		true有效,false无效

type EchoRequestContext

type EchoRequestContext struct {
	AudioPlayer EchoRequestContextAudioPlayer `json:"AudioPlayer"`
	System      EchoRequestContextSystem      `json:"System"`
}

Alexa服务和设备的当前状态的信息。用于在会话(的上下文中发送的请求LaunchRequest和IntentRequest)时, context对象将复制user和application其也可在信息session对象。 https://developer.amazon.com/docs/custom-skills/request-and-response-json-reference.html#context-object

type EchoRequestContextAudioPlayer

type EchoRequestContextAudioPlayer struct {
	//发送请求时的轨道偏移量(以毫秒为单位)。
	//如果曲目在开头,这是0。AudioPlayer当您的技能是最近在设备上播放音频的技能时,这只会包含在对象中。
	OffsetInMilliseconds int `json:"offsetInMilliseconds"`

	//音频播放的最后已知状态。
	//IDLE:没有什么,没有入队的项目。
	//PAUSED:流已暂停。
	//PLAYING:流正在播放。
	//BUFFER_UNDERRUN:缓冲区不足
	//FINISHED:流完了。STOPPED:流被中断。
	PlayerActivity string `json:"playerActivity"`

	//音频流的不透明标记。发送Play指令时提供此标记。
	//AudioPlayer当您的技能是最近在设备上播放音频的技能时,这只会包含在对象中。
	Token string `json:"token"`
}

提供AudioPlayer接口当前状态的对象 https://developer.amazon.com/docs/custom-skills/request-and-response-json-reference.html#audioplayer-object

type EchoRequestContextSystem

type EchoRequestContextSystem struct {
	APIAccessToken string                              `json:"apiAccessToken"`
	APIEndpoint    string                              `json:"apiEndpoint"`
	Application    EchoRequestContextSystemApplication `json:"application"`
	Device         EchoRequestContextSystemDevice      `json:"device"`
	User           EchoRequestContextSystemUser        `json:"user"`
}

type EchoRequestContextSystemApplication

type EchoRequestContextSystemApplication struct {
	ApplicationID string `json:"applicationId"`
}

type EchoRequestContextSystemDevice

type EchoRequestContextSystemDevice struct {
	DeviceID            string                                            `json:"deviceId"`
	SupportedInterfaces EchoRequestContextSystemDeviceSupportedInterfaces `json:"supportedInterfaces"`
}

type EchoRequestContextSystemDeviceSupportedInterfaces

type EchoRequestContextSystemDeviceSupportedInterfaces struct {
	AudioPlayer struct{} `json:"AudioPlayer"`
}

type EchoRequestContextSystemUser

type EchoRequestContextSystemUser struct {
	AccessToken string                                  `json:"accessToken"`
	Permissions EchoRequestContextSystemUserPermissions `json:"permissions"`
	UserID      string                                  `json:"userId"`
}

type EchoRequestContextSystemUserPermissions

type EchoRequestContextSystemUserPermissions struct {
	ConsentToken string `json:"consentToken"`
}

type EchoRequestRequest

type EchoRequestRequest struct {
	DialogState string                   `json:"dialogState"`
	Error       EchoRequestRequestError  `json:"error"`
	Intent      EchoRequestRequestIntent `json:"intent"`
	Locale      string                   `json:"locale"`
	Reason      string                   `json:"reason"`
	RequestID   string                   `json:"requestId"`
	Timestamp   string                   `json:"timestamp"`
	Type        string                   `json:"type"`
}

type EchoRequestRequestError

type EchoRequestRequestError struct {
	Message string `json:"message"`
	Type    string `json:"type"`
}

type EchoRequestRequestIntent

type EchoRequestRequestIntent struct {
	ConfirmationStatus string                                   `json:"confirmationStatus"`
	Name               string                                   `json:"name"`
	Slots              map[string]*EchoRequestRequestIntentSlot `json:"slots"`
}

type EchoRequestRequestIntentSlot

type EchoRequestRequestIntentSlot struct {
	ConfirmationStatus string   `json:"confirmationStatus"`
	Resolutions        struct{} `json:"resolutions"`
	Name               string   `json:"name"`
	Value              string   `json:"value"`
}

type EchoRequestSession

type EchoRequestSession struct {
	Application EchoRequestSessionApplication `json:"application"`
	Attributes  struct{}                      `json:"attributes"`
	New         bool                          `json:"new"`
	SessionID   string                        `json:"sessionId"`
	User        EchoRequestSessionUser        `json:"user"`
}

type EchoRequestSessionApplication

type EchoRequestSessionApplication struct {
	ApplicationID string `json:"applicationId"`
}

type EchoRequestSessionUser

type EchoRequestSessionUser struct {
	AccessToken string                            `json:"accessToken"`
	Permissions EchoRequestSessionUserPermissions `json:"permissions"`
	UserID      string                            `json:"userId"`
}

type EchoRequestSessionUserPermissions

type EchoRequestSessionUserPermissions struct {
	ConsentToken string `json:"consentToken"`
}

type EchoResponse

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

func NewEchoResponse

func NewEchoResponse() *EchoResponse

默认的响应对象

func (*EchoResponse) LinkAccountCard

func (T *EchoResponse) LinkAccountCard() *EchoResponse

设置在屏幕上显示的卡片,仅用于用户认证

*EchoResponse	响应对象

func (*EchoResponse) OutputSpeech

func (T *EchoResponse) OutputSpeech(text string) *EchoResponse

设置语音输出

text			语音文本
*EchoResponse	响应对象

func (*EchoResponse) OutputSpeechSSML

func (T *EchoResponse) OutputSpeechSSML(text string) *EchoResponse

设置语音输出 更多浏览这里: https://developer.amazon.com/docs/custom-skills/speech-synthesis-markup-language-ssml-reference.html

text			语音文本(支持语气表达,等等)
*EchoResponse	响应对象

func (*EchoResponse) RepromptSSML

func (T *EchoResponse) RepromptSSML(text string) *EchoResponse

设置回复确认输出 更多浏览这里: https://developer.amazon.com/docs/custom-skills/speech-synthesis-markup-language-ssml-reference.html

text			语音文本(支持语气表达,等等)
*EchoResponse	响应对象

func (*EchoResponse) RepromptText

func (T *EchoResponse) RepromptText(text string) *EchoResponse

设置回复确认输出

text			内容
*EchoResponse	响应对象

func (*EchoResponse) SetEndSession

func (T *EchoResponse) SetEndSession(ok bool) *EchoResponse

设置是否结束本次会活,默认是结束会话

ok		true结束会话,fales不结束会话

func (*EchoResponse) SimpleCard

func (T *EchoResponse) SimpleCard(title string, content string) *EchoResponse

设置在屏幕上显示的卡片

title			标题
content			内容
*EchoResponse	响应对象

func (*EchoResponse) StandardCard

func (T *EchoResponse) StandardCard(title string, text string, smallImg string, largeImg string) *EchoResponse

设置在屏幕上显示的卡片,支持图片

title			标题
text			内容
smallImg		小图片 720w x 480h
largeImg		中图片 1200w x 800h
*EchoResponse	响应对象

func (*EchoResponse) String

func (T *EchoResponse) String() string

type EchoResponseResponse

type EchoResponseResponse struct {
	Card             *EchoResponseResponseCard         `json:"card,omitempty"`
	Directives       []EchoResponseResponseDirectives  `json:"directives,omitempty"`
	OutputSpeech     *EchoResponseResponseOutputSpeech `json:"outputSpeech,omitempty"`
	Reprompt         *EchoResponseResponseReprompt     `json:"reprompt,omitempty"`
	ShouldEndSession bool                              `json:"shouldEndSession"`
}

type EchoResponseResponseCard

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

type EchoResponseResponseCardImage

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

https://developer.amazon.com/zh/docs/custom-skills/request-and-response-json-reference.html

HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Content-Length:

{
  "version": "string",
  "sessionAttributes": {
    "key": "value"
  },
  "response": {
    "outputSpeech": {
      "type": "PlainText",
      "text": "Plain text string to speak",
      "ssml": "<speak>SSML text string to speak</speak>",
      "playBehavior": "REPLACE_ENQUEUED"
    },
    "card": {
      "type": "Standard",
      "title": "Title of the card",
      "content": "Content of a simple card",
      "text": "Text content for a standard card",
      "image": {
        "smallImageUrl": "https://url-to-small-card-image...",
        "largeImageUrl": "https://url-to-large-card-image..."
      }
    },
    "reprompt": {
      "outputSpeech": {
        "type": "PlainText",
        "text": "Plain text string to speak",
        "ssml": "<speak>SSML text string to speak</speak>",
        "playBehavior": "REPLACE_ENQUEUED"
      }
    },
    "directives": [
      {
        "type": "InterfaceName.Directive"
        (...properties depend on the directive type)
      }
    ],
    "shouldEndSession": true
  }
}

Response Examples:

{
  "version": "1.0",
  "sessionAttributes": {
    "supportedHoroscopePeriods": {
      "daily": true,
      "weekly": false,
      "monthly": false
    }
  },
  "response": {
    "outputSpeech": {
      "type": "PlainText",
      "text": "Today will provide you a new learning opportunity.  Stick with it and the possibilities will be endless. Can I help you with anything else?"
    },
    "card": {
      "type": "Simple",
      "title": "Horoscope",
      "content": "Today will provide you a new learning opportunity.  Stick with it and the possibilities will be endless."
    },
    "reprompt": {
      "outputSpeech": {
        "type": "PlainText",
        "text": "Can I help you with anything else?"
      }
    },
    "shouldEndSession": false
  }
}

Response to IntentRequest or Launch Request with Directives Example:

{
  "version": "1.0",
  "sessionAttributes": {},
  "response": {
    "outputSpeech": {
      "type": "PlainText",
      "text": "Playing the requested song."
    },
    "card": {
      "type": "Simple",
      "title": "Play Audio",
      "content": "Playing the requested song."
    },
    "reprompt": {
      "outputSpeech": {
        "type": "PlainText",
        "text": null
      }
    },
    "directives": [
      {
        "type": "AudioPlayer.Play",
        "playBehavior": "ENQUEUE",
        "audioItem": {
          "stream": {
            "token": "this-is-the-audio-token",
            "url": "https://my-audio-hosting-site.com/audio/sample-song.mp3",
            "offsetInMilliseconds": 0
          }
        }
      }
    ],
    "shouldEndSession": true
  }
}

Response to AudioPlayer or PlaybackController Example (Directives Only):

{
  "version": "1.0",
  "response": {
    "directives": [
      {
        "type": "AudioPlayer.Play",
        "playBehavior": "REPLACE_ALL",
        "audioItem": {
          "stream": {
            "token": "track2-long-audio",
            "url": "https://my-audio-hosting-site.com/audio/sample-song-2.mp3",
            "offsetInMilliseconds": 0
          }
        }
      }
    ]
  }
}

type EchoResponseResponseDirectives

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

type EchoResponseResponseDirectivesAudioItem

type EchoResponseResponseDirectivesAudioItem struct {
	Stream EchoResponseResponseDirectivesAudioItemStream `json:"stream"`
}

type EchoResponseResponseDirectivesAudioItemStream

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

type EchoResponseResponseOutputSpeech

type EchoResponseResponseOutputSpeech struct {
	Ssml string `json:"ssml,omitempty"`
	Text string `json:"text,omitempty"`
	Type string `json:"type,omitempty"`
}

type EchoResponseResponseReprompt

type EchoResponseResponseReprompt struct {
	OutputSpeech EchoResponseResponseRepromptOutputSpeech `json:"outputSpeech,omitempty"`
}

type EchoResponseResponseRepromptOutputSpeech

type EchoResponseResponseRepromptOutputSpeech struct {
	Ssml string `json:"ssml,omitempty"`
	Text string `json:"text,omitempty"`
	Type string `json:"type,omitempty"`
}

Jump to

Keyboard shortcuts

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