This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage.go
183 lines (161 loc) · 5.05 KB
/
message.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package chatbase
import (
"context"
"encoding/json"
"io"
)
// MessageType describes the source of a message
type MessageType string
// Types of messages used to identify the origin of
// a message in Chatbase
const (
UserType MessageType = "user"
AgentType MessageType = "agent"
)
// A set of strings that can be used for specifying a message's platform.
// These platforms will be recognized by Chatbase, but any other non-zero
// custom string value like "Workplace" can be used as well
const (
PlatformFacebook = "Facebook"
PlatformSMS = "SMS"
PlatformWeb = "Web"
PlatformAndroid = "Android"
PlatformIOS = "iOS"
PlatformActions = "Actions"
PlatformAlexa = "Alexa"
PlatformCortana = "Cortana"
PlatformKik = "Kik"
PlatformSkype = "Skype"
PlatformTwitter = "Twitter"
PlatformViber = "Viber"
PlatformTelegram = "Telegram"
PlatformSlack = "Slack"
PlatformWhatsApp = "WhatsApp"
PlatformWeChat = "WeChat"
PlatformLine = "Line"
PlatformKakao = "Kakao"
)
var (
messagesEndpoint = "https://chatbase.com/api/messages"
messageEndpoint = "https://chatbase.com/api/message"
)
// Message contains data about a generic chat message
type Message struct {
APIKey string `json:"api_key"`
Type MessageType `json:"type"`
UserID string `json:"user_id"`
TimeStamp int64 `json:"time_stamp"`
Platform string `json:"platform"`
Message string `json:"message,omitempty"`
Intent string `json:"intent,omitempty"`
NotHandled bool `json:"not_handled,omitempty"`
Feedback bool `json:"feedback,omitempty"`
Version string `json:"version,omitempty"`
SessionID string `json:"session_id,omitempty"`
}
// SetMessage adds an optional "message" value to a message
func (m *Message) SetMessage(msg string) *Message {
m.Message = msg
return m
}
// SetIntent adds an optional "intent" value to a message
func (m *Message) SetIntent(i string) *Message {
m.Intent = i
return m
}
// SetNotHandled adds an optional "not handled" value to a message
func (m *Message) SetNotHandled(n bool) *Message {
m.NotHandled = n
return m
}
// SetFeedback adds an optional "feedback" value to a message
func (m *Message) SetFeedback(f bool) *Message {
m.Feedback = f
return m
}
// SetVersion adds an optional "version" value to a message
func (m *Message) SetVersion(v string) *Message {
m.Version = v
return m
}
// SetSessionID adds an optional "session id" value to a message
func (m *Message) SetSessionID(s string) *Message {
m.SessionID = s
return m
}
// SetTimeStamp overrides the message's "timestamp" value
func (m *Message) SetTimeStamp(t int64) *Message {
m.TimeStamp = t
return m
}
// Submit tries to deliver the message to Chatbase
func (m *Message) Submit() (*MessageResponse, error) {
return newMessageResponse(func() (io.ReadCloser, error) {
return apiPost(messageEndpoint, m)
})
}
// SubmitWithContext tries to deliver the message to Chatbase
// while considering the given context's deadline
func (m *Message) SubmitWithContext(ctx context.Context) (*MessageResponse, error) {
data, err := resultWithContext(ctx, func() (interface{}, error) {
return m.Submit()
})
if err != nil {
return nil, err
}
if res, ok := data.(*MessageResponse); ok {
return res, nil
}
return nil, errBadData
}
// MessageResponse describes a Chatbase response to the submission of
// a single message. It is also used to represent the result of an item
// of a collection of messages that have been submitted.
type MessageResponse struct {
MessageID MessageID `json:"message_id"`
Status Status `json:"status"`
Reason string `json:"reason,omitempty"`
}
// Messages is a collection of Message
type Messages []Message
// MarshalJSON ensures the list of messages is wrapped in a
// top-level object before being serialized into JSON
func (m Messages) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"messages": []Message(m),
})
}
// Submit tries to deliver the set of messages to Chatbase
func (m *Messages) Submit() (*MessagesResponse, error) {
return newMessagesResponse(func() (io.ReadCloser, error) {
return apiPost(messagesEndpoint, m)
})
}
// SubmitWithContext tries to deliver the set of messages to Chatbase
// while considering the given context's deadline
func (m *Messages) SubmitWithContext(ctx context.Context) (*MessagesResponse, error) {
data, err := resultWithContext(ctx, func() (interface{}, error) {
return m.Submit()
})
if err != nil {
return nil, err
}
if res, ok := data.(*MessagesResponse); ok {
return res, nil
}
return nil, errBadData
}
// Append adds messages to the the collection
func (m *Messages) Append(addition ...*Message) *Messages {
for _, a := range addition {
*m = append(*m, *a)
}
return m
}
// MessagesResponse describes a Chatbase response to submitting a set of messages
type MessagesResponse struct {
AllSucceeded bool `json:"all_succeeded"`
Status Status `json:"status"`
Responses []MessageResponse `json:"responses"`
Reason string `json:"reason,omitempty"`
}