This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
fulfillment.go
74 lines (66 loc) · 2.38 KB
/
fulfillment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package dialogflow
import (
"bytes"
"encoding/json"
"fmt"
)
// Fulfillment is the response sent back to dialogflow in case of a successful
// webhook call
type Fulfillment struct {
FulfillmentText string `json:"fulfillmentText,omitempty"`
FulfillmentMessages Messages `json:"fulfillmentMessages,omitempty"`
Source string `json:"source,omitempty"`
Payload interface{} `json:"payload,omitempty"`
OutputContexts Contexts `json:"outputContexts,omitempty"`
FollowupEventInput FollowupEventInput `json:"followupEventInput,omitempty"`
}
// FollowupEventInput Optional. Makes the platform immediately invoke another sessions.detectIntent call internally with the specified event as input.
// https://dialogflow.com/docs/reference/api-v2/rest/v2beta1/projects.agent.sessions/detectIntent#EventInput
type FollowupEventInput struct {
Name string `json:"name"`
LanguageCode string `json:"languageCode,omitempty"`
Parameters interface{} `json:"parameters,omitempty"`
}
// Messages is a simple slice of Message
type Messages []Message
// RichMessage is an interface used in the Message type.
// It is used to send back payloads to dialogflow
type RichMessage interface {
GetKey() string
}
// Message is a struct holding a platform and a RichMessage.
// Used in the FulfillmentMessages of the response sent back to dialogflow
type Message struct {
Platform
RichMessage RichMessage
}
// MarshalJSON implements the Marshaller interface for the JSON type.
// Custom marshalling is necessary since there can only be one rich message
// per Message and the key associated to each type is dynamic
func (m *Message) MarshalJSON() ([]byte, error) {
var err error
var b []byte
buffer := bytes.NewBufferString("{")
if m.Platform != "" {
buffer.WriteString(fmt.Sprintf(`"platform": "%s"`, m.Platform))
}
if m.Platform != "" && m.RichMessage != nil {
buffer.WriteString(", ")
}
if m.RichMessage != nil {
if b, err = json.Marshal(m.RichMessage); err != nil {
return nil, err
}
buffer.WriteString(fmt.Sprintf(`"%s": %s`, m.RichMessage.GetKey(), string(b)))
}
buffer.WriteString("}")
return buffer.Bytes(), nil
}
// ForGoogle takes a rich message wraps it in a message with the appropriate
// platform set
func ForGoogle(r RichMessage) Message {
return Message{
Platform: ActionsOnGoogle,
RichMessage: r,
}
}