aboutsummaryrefslogblamecommitdiff
path: root/types.go
blob: f63fc86b2f867166a798bd083e10c9adf3de7da1 (plain) (tree)


















































































































                                                                                   
// Copyright (c) 2020 Alexander Kiryukhin <a.kiryukhin@mail.ru>

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package marusia

import "context"

type requestEnvelope struct {
	Meta    meta     `json:"meta"`
	Request *Request `json:"request"`
	Session *session `json:"session"`
	Version string   `json:"version"`
}

type responseEnvelope struct {
	Response *Response `json:"response"`
	Session  *session  `json:"session"`
	Version  string    `json:"version"`
}

// Text sets text to response
func (r *Response) SetText(text string) *Response {
	r.Text = text
	return r
}

// TTS sets text with pronounce
func (r *Response) SetTTS(tts string) *Response {
	r.TTS = tts
	return r
}

// EndSession if set dialog will be completed
func (r *Response) SetEndSession(endSession bool) *Response {
	r.EndSession = endSession
	return r
}

// AddButton to dialog
func (r *Response) AddButton(title string, payload Payload, URL string) *Response {
	r.Buttons = append(r.Buttons, newButton(title, payload, URL))
	return r
}

// Request represents incoming message from Marusia to skill
type Request struct {
	Command           string  `json:"command"`
	OriginalUtterance string  `json:"original_utterance"`
	Type              string  `json:"type"`
	Payload           Payload `json:"payload"`
	Nlu               struct {
		Tokens   []string      `json:"tokens"`
		Entities []interface{} `json:"entities"`
	} `json:"nlu"`
}

// Response represents outgoing message from skill to Marusia
type Response struct {
	Text       string    `json:"text"`
	TTS        string    `json:"tts"`
	Buttons    []*button `json:"buttons"`
	EndSession bool      `json:"end_session"`
}

func NewResponse(text string) *Response {
	return &Response{Text: text}
}

type meta struct {
	ClientID   string                 `json:"client_id"`
	Locale     string                 `json:"locale"`
	Timezone   string                 `json:"timezone"`
	Interfaces map[string]interface{} `json:"interfaces"`
}

type session struct {
	SessionID string `json:"session_id"`
	MessageID int    `json:"message_id"`
	UserID    string `json:"user_id"`
	SkillID   string `json:"skill_id,omitempty"`
	New       bool   `json:"new,omitempty"`
}

type button struct {
	Title   string  `json:"title"`
	Payload Payload `json:"payload"`
	URL     string  `json:"url"`
}

func newButton(title string, payload Payload, URL string) *button {
	return &button{Title: title, Payload: payload, URL: URL}
}

// Payload represents payload that sends by pressing interface button
type Payload map[string]interface{}

// MessageHandler represents handler for incoming requests
type MessageHandler func(context.Context, *Request) (*Response, error)