-
Notifications
You must be signed in to change notification settings - Fork 1
/
hipchat.go
86 lines (70 loc) · 1.54 KB
/
hipchat.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
package main
import (
"fmt"
"github.com/tbruyelle/hipchat-go/hipchat"
"net/url"
"strings"
)
type Message struct {
Date string `json:"date,omitempty"`
Message string `json:"message"`
}
type Item struct {
Message Message `json:"message"`
}
type RoomMessage struct {
Event string `json:"event,omitempty"`
Item Item `json:"item"`
}
func getRoomID(rn string) (int, error) {
rooms, _, err := client.Room.List()
if err != nil {
return 0, err
}
for _, room := range rooms.Items {
if strings.EqualFold(room.Name, rn) {
return room.ID, nil
}
}
return 0, fmt.Errorf("room '%s' could not be found; you may not have the necessary permissions to view it", rn)
}
func webhookGC(rid int, k string) error {
webhooks, _, err := client.Room.ListWebhooks(rid, new(hipchat.ListWebhooksOptions))
if err != nil {
return err
}
for _, webhook := range webhooks.Webhooks {
if strings.EqualFold(webhook.Key, k) {
if _, err := client.Room.DeleteWebhook(rid, webhook.ID); err != nil {
return err
}
}
}
return nil
}
func webhookRegister(rn string, k string, u *url.URL) error {
if client == nil {
return fmt.Errorf("HipChat client is not initialised")
}
rid, err := getRoomID(rn)
if err != nil {
return err
}
roomID = rid
err = webhookGC(roomID, k)
if err != nil {
return err
}
req := hipchat.CreateWebhookRequest{
Name: "Hippie Bot",
Key: k,
Event: "room_message",
URL: u.String(),
}
wh, _, err := client.Room.CreateWebhook(roomID, &req)
if err != nil {
return err
}
webhookID = wh.ID
return nil
}