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
/
update.go
88 lines (78 loc) · 2.13 KB
/
update.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
package chatbase
import (
"context"
"io"
)
var (
updateEndpoint = "https://chatbase.com/api/message/update"
)
// Update contains data about an existing message that should be updated
type Update struct {
APIKey string `json:"-"`
MessageID MessageID `json:"-"`
Intent string `json:"intent,omitempty"`
NotHandled string `json:"not_handled,omitempty"`
Feedback string `json:"feedback,omitempty"`
Version string `json:"version,omitempty"`
}
// SetIntent adds an optional "intent" value to an update
func (u *Update) SetIntent(i string) *Update {
u.Intent = i
return u
}
// SetNotHandled adds an optional "not handled" value to an update
func (u *Update) SetNotHandled(n bool) *Update {
u.NotHandled = ""
if n {
u.NotHandled = "true"
}
return u
}
// SetFeedback adds an optional "feedback" value to an update
func (u *Update) SetFeedback(f string) *Update {
u.Feedback = f
return u
}
// SetVersion adds an optional "version" value to an update
func (u *Update) SetVersion(v string) *Update {
u.Version = v
return u
}
// Submit tries to deliver the update to Chatbase
func (u *Update) Submit() (*UpdateResponse, error) {
return newUpdateResponse(func() (io.ReadCloser, error) {
ep, epErr := augmentURL(updateEndpoint, map[string]string{
"api_key": u.APIKey,
"message_id": u.MessageID.String(),
})
if epErr != nil {
return nil, epErr
}
body, bodyErr := apiPut(ep, u)
if bodyErr != nil {
return nil, bodyErr
}
return body, nil
})
}
// SubmitWithContext tries to deliver the update to Chatbase while
// considering the given context's deadline
func (u *Update) SubmitWithContext(ctx context.Context) (*UpdateResponse, error) {
data, err := resultWithContext(ctx, func() (interface{}, error) {
return u.Submit()
})
if err != nil {
return nil, err
}
if res, ok := data.(*UpdateResponse); ok {
return res, nil
}
return nil, errBadData
}
// UpdateResponse describes a Chatbase response to an update submission
type UpdateResponse struct {
Error []string `json:"error"`
Updated []string `json:"updated"`
Status Status `json:"status"`
Reason string `json:"string,omitempty"`
}