forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
chatbase.go
53 lines (43 loc) · 1.32 KB
/
chatbase.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
package chatbase
import (
"bytes"
"encoding/json"
"net/http"
"time"
"github.com/nyaruka/courier/utils"
)
// ChatbaseAPIURL is the URL chatbase API messages will be sent to
var chatbaseAPIURL = "https://chatbase.com/api/message"
// chatbaseLog is the payload for a chatbase request
type chatbaseLog struct {
Type string `json:"type"`
UserID string `json:"user_id"`
Platform string `json:"platform"`
Message string `json:"message"`
TimeStamp int64 `json:"time_stamp"`
APIKey string `json:"api_key"`
APIVersion string `json:"version,omitempty"`
}
// SendChatbaseMessage sends a chatbase message with the passed in api key and message details
func SendChatbaseMessage(apiKey string, apiVersion string, messageType string, userID string, platform string, message string, timestamp time.Time) error {
body := chatbaseLog{
Type: messageType,
UserID: userID,
Platform: platform,
Message: message,
TimeStamp: timestamp.UnixNano() / int64(time.Millisecond),
APIKey: apiKey,
APIVersion: apiVersion,
}
jsonBody, err := json.Marshal(body)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, chatbaseAPIURL, bytes.NewReader(jsonBody))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
_, err = utils.MakeHTTPRequest(req)
return err
}