-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconversation_guestchat.go
295 lines (272 loc) · 9.76 KB
/
conversation_guestchat.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package gcloudcx
import (
"context"
"net/http"
"strings"
"time"
"github.com/gildas/go-core"
"github.com/gildas/go-errors"
"github.com/gildas/go-logger"
"github.com/gildas/go-request"
"github.com/google/uuid"
"github.com/gorilla/websocket"
)
// ConversationGuestChat describes a Guest Chat
type ConversationGuestChat struct {
ID uuid.UUID `json:"id"`
SelfURI URI `json:"selfUri,omitempty"`
Target *RoutingTarget `json:"-"`
Guest *ChatMember `json:"member,omitempty"`
Members map[uuid.UUID]*ChatMember `json:"-"`
JWT string `json:"jwt,omitempty"`
EventStream string `json:"eventStreamUri,omitempty"`
Socket *websocket.Conn `json:"-"`
TopicReceived chan NotificationTopic `json:"-"`
LogHeartbeat bool `json:"logHeartbeat"`
client *Client `json:"-"`
logger *logger.Logger `json:"-"`
}
// Initialize initializes the object
//
// accepted parameters: *gcloudcx.Client, *logger.Logger
//
// implements Initializable
func (conversation *ConversationGuestChat) Initialize(parameters ...interface{}) {
for _, raw := range parameters {
switch parameter := raw.(type) {
case uuid.UUID:
conversation.ID = parameter
case *Client:
conversation.client = parameter
case *logger.Logger:
conversation.logger = parameter.Child("conversation", "conversation", "id", conversation.ID, "media", "guestchat")
}
}
if conversation.logger == nil {
conversation.logger = logger.Create("gcloudcx", &logger.NilStream{})
}
}
// GetID gets the identifier of this
//
// implements Identifiable
func (conversation ConversationGuestChat) GetID() uuid.UUID {
return conversation.ID
}
// GetURI gets the URI of this
//
// implements Addressable
func (conversation ConversationGuestChat) GetURI(ids ...uuid.UUID) URI {
if len(ids) > 0 {
return NewURI("/api/v2/conversations/%s", ids[0])
}
if conversation.ID != uuid.Nil {
return NewURI("/api/v2/conversations/%s", conversation.ID)
}
return URI("/api/v2/conversations/")
}
// String gets a string version
//
// implements the fmt.Stringer interface
func (conversation ConversationGuestChat) String() string {
return conversation.ID.String()
}
// Connect connects a Guest Chat to its websocket and starts its message loop
//
// If the websocket was already connected, nothing happens
// If the environment variable PURECLOUD_LOG_HEARTBEAT is set to true, the Heartbeat topic will be logged
func (conversation *ConversationGuestChat) Connect(context context.Context) (err error) {
if conversation.Socket != nil {
return
}
conversation.Socket, _, err = websocket.DefaultDialer.Dial(conversation.EventStream, nil)
if err != nil {
_ = conversation.Close(context)
// return errors.NotConnectedError.With("Conversation")
return errors.NotConnected.Wrap(err)
}
go conversation.messageLoop()
return
}
// Start starts a Conversation Guest Chat
func (conversation *ConversationGuestChat) Start(ctx context.Context, guest *ChatMember, target *RoutingTarget) error {
if conversation == nil || conversation.client == nil || conversation.logger == nil {
return errors.NotInitialized.With("Conversation")
}
log := conversation.logger
client := conversation.client
if guest == nil {
return errors.ArgumentMissing.With("Guest")
}
if target == nil {
return errors.ArgumentMissing.With("Target")
}
if client.Organization == nil {
return errors.ArgumentMissing.With("Organization")
}
if len(client.DeploymentID) == 0 {
return errors.ArgumentMissing.With("DeploymentID")
}
if err := client.Post(ctx, "/webchat/guest/conversations",
struct {
OrganizationID string `json:"organizationId"`
DeploymentID string `json:"deploymentId"`
RoutingTarget *RoutingTarget `json:"routingTarget"`
Guest *ChatMember `json:"memberInfo"`
}{
OrganizationID: conversation.client.Organization.ID.String(),
DeploymentID: conversation.client.DeploymentID.String(),
RoutingTarget: target,
Guest: guest,
},
&conversation,
); err != nil {
return err
}
conversation.logger = log
conversation.client = client
conversation.Guest.DisplayName = guest.DisplayName
conversation.Guest.AvatarURL = guest.AvatarURL
conversation.Guest.Role = guest.Role
conversation.Guest.State = guest.State
conversation.Guest.Custom = guest.Custom
conversation.Members = map[uuid.UUID]*ChatMember{}
conversation.Members[conversation.Guest.ID] = conversation.Guest
conversation.TopicReceived = make(chan NotificationTopic)
conversation.LogHeartbeat = core.GetEnvAsBool("PURECLOUD_LOG_HEARTBEAT", false)
return nil
}
// Close disconnects the websocket and the guest
func (conversation *ConversationGuestChat) Close(context context.Context) (err error) {
log := conversation.logger.Scope("close")
if conversation.Socket != nil {
log.Debugf("Disconnecting websocket")
if err = conversation.Socket.Close(); err != nil {
log.Errorf("Failed while close websocket", err)
return errors.WithMessage(err, "Failed while closing websocket")
}
log.Infof("Disconnected websocket")
}
if conversation.Guest != nil {
log.Debugf("Disconnecting Guest Member")
if err = conversation.client.Delete(context, NewURI("/webchat/guest/conversations/%s/members/%s", conversation.ID, conversation.Guest.ID), nil); err != nil {
log.Errorf("Failed while disconnecting Guest Member", err)
return err
}
log.Infof("Disconnected Guest Member")
}
return
}
func (conversation *ConversationGuestChat) messageLoop() {
log := conversation.logger.Scope("receive")
for {
// get a message body and decode it. (ReadJSON is nice, but in case of unknown message, I cannot get the original string)
var err error
var body []byte
if _, body, err = conversation.Socket.ReadMessage(); err != nil {
if strings.Contains(err.Error(), "use of closed network connection") {
log.Infof("Websocket was closed, stopping receive handler")
return
}
log.Errorf("Failed to read incoming message", err)
continue
}
// we have to use a custom version since topics are the same between agent chats and guest chats
// TODO: Maybe, we should create a Conversation interface...
topic, err := UnmarshalNotificationTopic(body)
if err != nil {
log.Warnf("%s, Body size: %d, Content: %s", err.Error(), len(body), string(body))
continue
}
switch topic.(type) {
case MetadataTopic:
if conversation.LogHeartbeat {
log.Tracef("Request %d bytes: %s", len(body), string(body))
}
default:
log.Tracef("Request %d bytes: %s", len(body), string(body))
}
conversation.TopicReceived <- topic
}
}
// GetMember fetches the given member of this Conversation (caches the member)
func (conversation *ConversationGuestChat) GetMember(context context.Context, identifiable Identifiable) (*ChatMember, error) {
if member, ok := conversation.Members[identifiable.GetID()]; ok {
return member, nil
}
member := &ChatMember{}
err := conversation.client.SendRequest(
context,
NewURI("/webchat/guest/conversations/%s/members/%s", conversation.ID, identifiable.GetID()),
&request.Options{
Authorization: "bearer " + conversation.JWT,
},
&member,
)
if err != nil {
return nil, err
}
conversation.logger.Scope("getmember").Debugf("Response: %+v", member)
conversation.Members[member.ID] = member
return member, nil
}
// SendTyping sends a typing indicator to Gcloud as the chat guest
func (conversation *ConversationGuestChat) SendTyping(context context.Context) (err error) {
response := &struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Conversation Conversation `json:"conversation,omitempty"`
Sender *ChatMember `json:"sender,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty"`
}{}
if err = conversation.client.SendRequest(
context,
NewURI("/webchat/guest/conversations/%s/members/%s/typing", conversation.ID, conversation.Guest.ID),
&request.Options{
Method: http.MethodPost,
Authorization: "bearer " + conversation.JWT,
},
&response,
); err == nil {
conversation.client.Logger.Record("scope", "sendtyping").Infof("Sent successfully. Response: %+v", response)
}
return
}
// SendMessage sends a message as the chat guest
func (conversation *ConversationGuestChat) SendMessage(context context.Context, text string) (err error) {
return conversation.sendBody(context, "standard", text)
}
// SendNotice sends a notice as the chat guest
func (conversation *ConversationGuestChat) SendNotice(context context.Context, text string) (err error) {
return conversation.sendBody(context, "notice", text)
}
// sendBody sends a body message as the chat guest
func (conversation *ConversationGuestChat) sendBody(context context.Context, bodyType, body string) (err error) {
response := &struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Conversation Conversation `json:"conversation,omitempty"`
Sender *ChatMember `json:"sender,omitempty"`
Body string `json:"body,omitempty"`
BodyType string `json:"bodyType,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty"`
SelfURI URI `json:"selfUri,omitempty"`
}{}
if err = conversation.client.SendRequest(
context,
NewURI("/webchat/guest/conversations/%s/members/%s/messages", conversation.ID, conversation.Guest.ID),
&request.Options{
Authorization: "bearer " + conversation.JWT,
Payload: struct {
BodyType string `json:"bodyType"`
Body string `json:"body"`
}{
BodyType: bodyType,
Body: body,
},
},
&response,
); err == nil {
conversation.client.Logger.Record("scope", "sendbody").Infof("Sent successfully. Response: %+v", response)
}
return
}