github.com/456vv/valexa@v1.0.2-0.20200706152242-1fb922d71ce5/EchoResponse.go (about)

     1  package valexa
     2  
     3  import (
     4  	"encoding/json"
     5  )
     6  
     7  
     8  /*
     9  https://developer.amazon.com/zh/docs/custom-skills/request-and-response-json-reference.html
    10  
    11  HTTP/1.1 200 OK
    12  Content-Type: application/json;charset=UTF-8
    13  Content-Length:
    14  
    15  {
    16    "version": "string",
    17    "sessionAttributes": {
    18      "key": "value"
    19    },
    20    "response": {
    21      "outputSpeech": {
    22        "type": "PlainText",
    23        "text": "Plain text string to speak",
    24        "ssml": "<speak>SSML text string to speak</speak>",
    25        "playBehavior": "REPLACE_ENQUEUED"      
    26      },
    27      "card": {
    28        "type": "Standard",
    29        "title": "Title of the card",
    30        "content": "Content of a simple card",
    31        "text": "Text content for a standard card",
    32        "image": {
    33          "smallImageUrl": "https://url-to-small-card-image...",
    34          "largeImageUrl": "https://url-to-large-card-image..."
    35        }
    36      },
    37      "reprompt": {
    38        "outputSpeech": {
    39          "type": "PlainText",
    40          "text": "Plain text string to speak",
    41          "ssml": "<speak>SSML text string to speak</speak>",
    42          "playBehavior": "REPLACE_ENQUEUED"             
    43        }
    44      },
    45      "directives": [
    46        {
    47          "type": "InterfaceName.Directive"
    48          (...properties depend on the directive type)
    49        }
    50      ],
    51      "shouldEndSession": true
    52    }
    53  }
    54  
    55  Response Examples:
    56  {
    57    "version": "1.0",
    58    "sessionAttributes": {
    59      "supportedHoroscopePeriods": {
    60        "daily": true,
    61        "weekly": false,
    62        "monthly": false
    63      }
    64    },
    65    "response": {
    66      "outputSpeech": {
    67        "type": "PlainText",
    68        "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?"
    69      },
    70      "card": {
    71        "type": "Simple",
    72        "title": "Horoscope",
    73        "content": "Today will provide you a new learning opportunity.  Stick with it and the possibilities will be endless."
    74      },
    75      "reprompt": {
    76        "outputSpeech": {
    77          "type": "PlainText",
    78          "text": "Can I help you with anything else?"
    79        }
    80      },
    81      "shouldEndSession": false
    82    }
    83  }
    84  
    85  Response to IntentRequest or Launch Request with Directives Example:
    86  {
    87    "version": "1.0",
    88    "sessionAttributes": {},
    89    "response": {
    90      "outputSpeech": {
    91        "type": "PlainText",
    92        "text": "Playing the requested song."
    93      },
    94      "card": {
    95        "type": "Simple",
    96        "title": "Play Audio",
    97        "content": "Playing the requested song."
    98      },
    99      "reprompt": {
   100        "outputSpeech": {
   101          "type": "PlainText",
   102          "text": null
   103        }
   104      },
   105      "directives": [
   106        {
   107          "type": "AudioPlayer.Play",
   108          "playBehavior": "ENQUEUE",
   109          "audioItem": {
   110            "stream": {
   111              "token": "this-is-the-audio-token",
   112              "url": "https://my-audio-hosting-site.com/audio/sample-song.mp3",
   113              "offsetInMilliseconds": 0
   114            }
   115          }
   116        }
   117      ],
   118      "shouldEndSession": true
   119    }
   120  }
   121  
   122  Response to AudioPlayer or PlaybackController Example (Directives Only):
   123  {
   124    "version": "1.0",
   125    "response": {
   126      "directives": [
   127        {
   128          "type": "AudioPlayer.Play",
   129          "playBehavior": "REPLACE_ALL",
   130          "audioItem": {
   131            "stream": {
   132              "token": "track2-long-audio",
   133              "url": "https://my-audio-hosting-site.com/audio/sample-song-2.mp3",
   134              "offsetInMilliseconds": 0
   135            }
   136          }
   137        }
   138      ]
   139    }
   140  }
   141  
   142  
   143  */
   144  type EchoResponseResponseCardImage struct {
   145  	LargeImageURL string 												`json:"largeImageUrl,omitempty"`
   146  	SmallImageURL string 												`json:"smallImageUrl,omitempty"`
   147  }
   148  
   149  type EchoResponseResponseCard struct {
   150  	Content string 														`json:"content,omitempty"`
   151  	Image   EchoResponseResponseCardImage								`json:"image,omitempty"`
   152  	Text  	string 														`json:"text,omitempty"`
   153  	Title 	string 														`json:"title,omitempty"`
   154  	Type  	string 														`json:"type,omitempty"`
   155  }
   156  
   157  type EchoResponseResponseDirectivesAudioItemStream struct {
   158  	ExpectedPreviousToken string 										`json:"expectedPreviousToken"`
   159  	OffsetInMilliseconds  int    										`json:"offsetInMilliseconds"`
   160  	Token                 string 										`json:"token"`
   161  	URL                   string 										`json:"url"`
   162  }
   163  
   164  type EchoResponseResponseDirectivesAudioItem struct {
   165  	Stream  EchoResponseResponseDirectivesAudioItemStream				`json:"stream"`
   166  }
   167  
   168  type EchoResponseResponseDirectives struct {
   169  	AudioItem  		EchoResponseResponseDirectivesAudioItem				`json:"audioItem"`
   170  	PlayBehavior 	string 												`json:"playBehavior"`
   171  	Type         	string 												`json:"type"`
   172  }
   173  
   174  type EchoResponseResponseOutputSpeech struct {
   175  	Ssml string 														`json:"ssml,omitempty"`
   176  	Text string 														`json:"text,omitempty"`
   177  	Type string 														`json:"type,omitempty"`
   178  }
   179  
   180  type EchoResponseResponseRepromptOutputSpeech struct {
   181  	Ssml string 														`json:"ssml,omitempty"`
   182  	Text string 														`json:"text,omitempty"`
   183  	Type string 														`json:"type,omitempty"`
   184  }
   185  
   186  type EchoResponseResponseReprompt struct {
   187  	OutputSpeech EchoResponseResponseRepromptOutputSpeech 				`json:"outputSpeech,omitempty"`
   188  }
   189  
   190  type EchoResponseResponse struct {
   191  	Card  				*EchoResponseResponseCard						`json:"card,omitempty"`
   192  	Directives 			[]EchoResponseResponseDirectives 				`json:"directives,omitempty"`
   193  	OutputSpeech  		*EchoResponseResponseOutputSpeech 				`json:"outputSpeech,omitempty"`
   194  	Reprompt  			*EchoResponseResponseReprompt					`json:"reprompt,omitempty"`
   195  	ShouldEndSession 	bool 											`json:"shouldEndSession"`
   196  }
   197  
   198  type EchoResponse struct {
   199  	Response  			EchoResponseResponse							`json:"response"`
   200  	SessionAttributes 	map[string]interface{}							`json:"sessionAttributes,omitempty"`
   201  	Version 			string 											`json:"version"`
   202  }
   203  
   204  //默认的响应对象
   205  func NewEchoResponse() *EchoResponse {
   206  	er := &EchoResponse{
   207  		Version: "1.0",
   208  		Response: EchoResponseResponse{
   209  			ShouldEndSession: false,
   210  		},
   211  		SessionAttributes: make(map[string]interface{}),
   212  	}
   213  
   214  	return er
   215  }
   216  
   217  //设置是否结束本次会活,默认是结束会话
   218  //	ok		true结束会话,fales不结束会话
   219  func (T *EchoResponse) SetEndSession(ok bool) *EchoResponse {
   220  	T.Response.ShouldEndSession = ok
   221  	return T
   222  }
   223  
   224  //设置语音输出
   225  //	text			语音文本
   226  //	*EchoResponse	响应对象
   227  func (T *EchoResponse) OutputSpeech(text string) *EchoResponse {
   228  	T.Response.OutputSpeech = &EchoResponseResponseOutputSpeech{
   229  		Type	: "PlainText",
   230  		Text	: text,
   231  	}
   232  	return T
   233  }
   234  
   235  //设置语音输出
   236  //更多浏览这里: https://developer.amazon.com/docs/custom-skills/speech-synthesis-markup-language-ssml-reference.html
   237  //	text			语音文本(支持语气表达,等等)
   238  //	*EchoResponse	响应对象
   239  func (T *EchoResponse) OutputSpeechSSML(text string) *EchoResponse {
   240  	T.Response.OutputSpeech = &EchoResponseResponseOutputSpeech{
   241  		Type	: "SSML",
   242  		Ssml	: text,
   243  	}
   244  	return T
   245  }
   246  
   247  //设置在屏幕上显示的卡片
   248  //	title			标题
   249  //	content			内容
   250  //	*EchoResponse	响应对象
   251  func (T *EchoResponse) SimpleCard(title string, content string) *EchoResponse {
   252  	T.Response.Card = &EchoResponseResponseCard{
   253  		Type	: "Simple",
   254  		Title	: title,
   255  		Content	: content,
   256  	}
   257  	return T
   258  }
   259  
   260  //设置在屏幕上显示的卡片,支持图片
   261  //	title			标题
   262  //	text			内容
   263  //	smallImg		小图片 720w x 480h
   264  //	largeImg		中图片 1200w x 800h
   265  //	*EchoResponse	响应对象
   266  func (T *EchoResponse) StandardCard(title string, text string, smallImg string, largeImg string) *EchoResponse {
   267  	T.Response.Card = &EchoResponseResponseCard{
   268  		Type	: "Standard",
   269  		Title	: title,
   270  		Text	: text,
   271  	}
   272  	if smallImg != "" {
   273  		T.Response.Card.Image.SmallImageURL = smallImg
   274  	}
   275  	if largeImg != "" {
   276  		T.Response.Card.Image.LargeImageURL = largeImg
   277  	}
   278  	return T
   279  }
   280  
   281  //设置在屏幕上显示的卡片,仅用于用户认证
   282  //	*EchoResponse	响应对象
   283  func (T *EchoResponse) LinkAccountCard() *EchoResponse {
   284  	T.Response.Card = &EchoResponseResponseCard{
   285  		Type: "LinkAccount",
   286  	}
   287  	return T
   288  }
   289  
   290  //设置回复确认输出
   291  //	text			内容
   292  //	*EchoResponse	响应对象
   293  func (T *EchoResponse) RepromptText(text string) *EchoResponse {
   294  	T.Response.Reprompt = &EchoResponseResponseReprompt{
   295  		OutputSpeech: EchoResponseResponseRepromptOutputSpeech{
   296  			Type: "PlainText",
   297  			Text: text,
   298  		},
   299  	}
   300  	return T
   301  }
   302  
   303  //设置回复确认输出
   304  //更多浏览这里: https://developer.amazon.com/docs/custom-skills/speech-synthesis-markup-language-ssml-reference.html
   305  //	text			语音文本(支持语气表达,等等)
   306  //	*EchoResponse	响应对象
   307  func (T *EchoResponse) RepromptSSML(text string) *EchoResponse {
   308  	T.Response.Reprompt = &EchoResponseResponseReprompt{
   309  		OutputSpeech: EchoResponseResponseRepromptOutputSpeech{
   310  			Type: "SSML",
   311  			Ssml: text,
   312  		},
   313  	}
   314  	return T
   315  }
   316  
   317  
   318  func (T *EchoResponse) String() string {
   319  	jsonStr, err := json.Marshal(T)
   320  	if err != nil {
   321  		return "{}"
   322  	}
   323  	return string(jsonStr)
   324  }
   325