-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintegrations.go
151 lines (130 loc) · 4.32 KB
/
integrations.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
package rc
type IntegrationEvent string
const (
EvtSendMessage IntegrationEvent = "sendMessage"
EvtFileUploaded IntegrationEvent = "fileUploaded"
EvtRoomArchived IntegrationEvent = "roomArchived"
EvtRoomCreated IntegrationEvent = "roomCreated"
EvtRoomJoined IntegrationEvent = "roomJoined"
EvtRoomLeft IntegrationEvent = "roomLeft"
EvtUserCreated IntegrationEvent = "userCreated"
)
type Integration struct {
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Event IntegrationEvent `json:"event,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Username string `json:"username,omitempty"`
Urls []string `json:"urls,omitempty"`
ScriptEnabled bool `json:"scriptEnabled,omitempty"`
Channel string `json:"channel,omitempty"`
TriggerWords string `json:"trigger_words,omitempty"`
Alias string `json:"alias,omitempty"`
Avatar string `json:"avatar,omitempty"`
Emoji string `json:"emoji,omitempty"`
Token string `json:"token,omitempty"`
Script string `json:"script,omitempty"`
}
type IntegrationOption func(*Integration)
func IntegrationScript(script string) IntegrationOption {
return func(i *Integration) {
i.ScriptEnabled = true
i.Script = script
}
}
func IntegrationAvatarURL(u string) IntegrationOption {
return func(i *Integration) {
i.Avatar = u
}
}
func IntegrationAvatarEmoji(e string) IntegrationOption {
return func(i *Integration) {
i.Emoji = e
}
}
func IntegrationTriggers(t string) IntegrationOption {
return func(i *Integration) {
i.TriggerWords = t
}
}
func IntegrationToken(t string) IntegrationOption {
return func(i *Integration) {
i.Token = t
}
}
func NewIncomingIntegration(name, user, channel string, enabled bool, opts ...IntegrationOption) *Integration {
i := &Integration{
Type: "webhook-incoming",
Name: name,
Username: user,
Enabled: enabled,
Channel: channel,
ScriptEnabled: false,
}
for _, o := range opts {
o(i)
}
return i
}
func NewOutgoingIntegration(name, user, channel string, event IntegrationEvent, urls []string, enabled bool, opts ...IntegrationOption) *Integration {
i := &Integration{
Type: "webhook-outgoing",
Name: name,
Event: event,
Username: user,
Enabled: enabled,
Channel: channel,
ScriptEnabled: false,
Urls: urls,
}
for _, o := range opts {
o(i)
}
return i
}
type IntegrationResponse struct {
Integration IntegrationInfo `json:"integration"`
Success bool `json:"success"`
}
type IntegrationList struct {
Integrations []IntegrationInfo `json:"integrations,omitempty"`
Success bool `json:"success,omitempty"`
Offset int `json:"offset,omitempty"`
Items int `json:"items,omitempty"`
Total int `json:"total,omitempty"`
}
type IntegrationInfo struct {
Type string `json:"type"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
Username string `json:"username"`
Event string `json:"event"`
Urls []string `json:"urls"`
ScriptEnabled bool `json:"scriptEnabled"`
UserID string `json:"userId"`
Channel []interface{} `json:"channel"`
CreatedAt string `json:"_createdAt"`
CreatedBy IntegrationCreatedBy `json:"_createdBy"`
UpdatedAt string `json:"_updatedAt"`
ID string `json:"_id"`
}
type IntegrationCreatedBy struct {
Username string `json:"username"`
ID string `json:"_id"`
}
func (c *Client) CreateIntegration(i *Integration) (*IntegrationInfo, error) {
result := &IntegrationResponse{}
if err := c.c.postJSON("/integrations.create", i).JSON(result); err != nil {
return nil, err
}
info := result.Integration
return &info, nil
}
func (c *Client) GetIntegrations() ([]IntegrationInfo, error) {
is := &IntegrationList{}
if err := c.c.get("/integrations.list", nil).JSON(is); err != nil {
return nil, err
}
result := is.Integrations
return result, nil
}