/
botmetrics.go
118 lines (96 loc) · 2.8 KB
/
botmetrics.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
package botmetrics
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"strconv"
"strings"
)
type BotmetricsClient struct {
ApiKey string
BotId string
}
var (
ErrClientNotConfigured = fmt.Errorf("Set the environment variables BOTMETRICS_API_KEY and BOTMETRICS_BOT_ID or call NewBotmetricsClient(\"api_key\", \"bot_id\")")
ErrIncorrectAPIResponse = fmt.Errorf("Incorrect Botmetrics API Response")
)
func NewBotmetricsClient(args ...string) (*BotmetricsClient, error) {
switch len(args) {
case 0:
if os.Getenv("BOTMETRICS_API_KEY") == "" || os.Getenv("BOTMETRICS_BOT_ID") == "" {
return nil, ErrClientNotConfigured
}
return &BotmetricsClient{ApiKey: os.Getenv("BOTMETRICS_API_KEY"), BotId: os.Getenv("BOTMETRICS_BOT_ID")}, nil
case 1:
if os.Getenv("BOTMETRICS_BOT_ID") == "" {
return nil, ErrClientNotConfigured
}
return &BotmetricsClient{ApiKey: args[0], BotId: os.Getenv("BOTMETRICS_API_ID")}, nil
case 2:
return &BotmetricsClient{ApiKey: args[0], BotId: args[1]}, nil
default:
return nil, ErrClientNotConfigured
}
}
func (b *BotmetricsClient) RegisterBot(token string, createdAt int64) bool {
params := url.Values{
"format": []string{"json"},
"instance[token]": []string{token},
}
host := os.Getenv("BOTMETRICS_API_HOST")
if host == "" {
host = "https://www.getbotmetrics.com"
}
url := fmt.Sprintf("%s/bots/%s/instances", host, b.BotId)
if createdAt > 0 {
params["instance[created_at]"] = []string{strconv.Itoa(int(createdAt))}
}
client := &http.Client{}
r, _ := http.NewRequest("POST", url, strings.NewReader(params.Encode()))
r.Header.Add("Authorization", b.ApiKey)
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Content-Length", strconv.Itoa(len(params.Encode())))
resp, err := client.Do(r)
if err != nil {
return false
} else {
defer resp.Body.Close()
if resp.StatusCode == 201 {
return true
}
}
return false
}
func (b *BotmetricsClient) Track(event string) (bool, error) {
var parsedEvent interface{}
err := json.Unmarshal([]byte(event), &parsedEvent)
if err != nil {
return false, err
}
params := url.Values{
"format": []string{"json"},
"event": []string{event},
}
host := os.Getenv("BOTMETRICS_API_HOST")
if host == "" {
host = "https://www.getbotmetrics.com"
}
url := fmt.Sprintf("%s/bots/%s/events", host, b.BotId)
client := &http.Client{}
r, _ := http.NewRequest("POST", url, strings.NewReader(params.Encode()))
r.Header.Add("Authorization", b.ApiKey)
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Content-Length", strconv.Itoa(len(params.Encode())))
resp, err := client.Do(r)
if err != nil {
return false, err
} else {
defer resp.Body.Close()
if resp.StatusCode == 202 {
return true, err
}
}
return false, ErrIncorrectAPIResponse
}