This repository has been archived by the owner on Apr 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
80 lines (66 loc) · 1.78 KB
/
main.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
package main
import (
"github.com/andybons/hipchat"
"encoding/json"
"fmt"
"net/http"
"os"
)
type Notification struct {
Message string
MessageId string
Signature string
SignatureVersion string
SigningCertURL string
SubscribeURL string
Subject string
Timestamp string
TopicArn string
Type string
UnsubscribeURL string
}
type HipChatSender struct{
AuthToken string
}
func (h HipChatSender)SendMessage(room_id, message string) error {
c := hipchat.Client{AuthToken: h.AuthToken}
c.BaseURL = "https://api.hipchat.com/v1"
req := hipchat.MessageRequest{
RoomId: room_id,
From: "Amazon SNS",
Message: message,
Color: hipchat.ColorYellow,
MessageFormat: hipchat.FormatText,
Notify: true,
}
return c.PostMessage(req)
}
func (h HipChatSender) ServeHTTP(w http.ResponseWriter, r *http.Request) {
room_id := r.URL.Path[1:]
var n Notification
dec := json.NewDecoder(r.Body)
err := dec.Decode(&n)
if (err != nil) {
http.Error(w, "Invalid JSON.", http.StatusBadRequest)
return
}
fmt.Printf("Received notification room_id:%v notification:%+v\n", room_id, n)
if s := n.SubscribeURL; len(s) != 0 {
fmt.Printf("SubscribeURL detected: %v\n", s)
if _, err := http.Get(s); err != nil {
fmt.Printf("Subscribe error: %v\n", err)
}
}
if len(n.Message) != 0 && len(n.Subject) != 0 {
err := h.SendMessage(room_id, fmt.Sprintf("%v: %v", n.Subject, n.Message))
if err != nil {
fmt.Printf("HipChat error: %v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func main() {
fmt.Println("Starting aws-sns-hipchat server.")
h := HipChatSender{AuthToken: os.Getenv("HIPCHAT_AUTH_TOKEN")}
http.ListenAndServe(":"+os.Getenv("PORT"), h)
}