-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
112 lines (94 loc) · 3.1 KB
/
api.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
package announcebot
import (
"net/http"
"github.com/gorilla/context"
"github.com/gorilla/mux"
"github.com/mattn/go-xmpp"
log "github.com/Sirupsen/logrus"
health "github.com/docker/go-healthcheck"
"github.com/tbruyelle/hipchat-go/hipchat"
)
func (bot *AnnounceBot) withBotContext(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
context.Set(r, "db", bot.db)
context.Set(r, "chat_api", bot.chatAPI)
context.Set(r, "config", &bot.Config)
h.ServeHTTP(w, r)
context.Clear(r)
}
}
func (bot *AnnounceBot) getRoutes() http.Handler {
r := mux.NewRouter()
r.HandleFunc("/subscribers", bot.withBotContext(bot.subscriptionListHandler)).Methods("GET")
r.HandleFunc("/subscriber/{user_id}", bot.withBotContext(bot.subscriptionHandler)).Methods("GET", "DELETE", "POST", "PUT")
r.HandleFunc("/announce", bot.withBotContext(bot.announceHandler)).Methods("POST")
r.HandleFunc("/test", bot.withBotContext(bot.testHandler)).Methods("POST")
r.HandleFunc("/debug/health", health.StatusHandler)
return r
}
func (bot *AnnounceBot) announceHandler(w http.ResponseWriter, r *http.Request) {
config := getConfiguration(r)
message, err := bot.MessageFactory()
if err != nil {
log.WithError(err).Error("No message generated for the announcement")
return
}
if config.AnnounceRoom != "-1" {
_, err := bot.chatAPI.Room.Notification(
config.AnnounceRoom,
&hipchat.NotificationRequest{
Message: message,
Notify: true,
MessageFormat: "text",
},
)
if err != nil {
log.WithError(err).Error("An error occurred while sending the announcement to the room")
} else {
log.WithField("room", config.AnnounceRoom).WithField("message", message).Info("Sent announcement to the room")
}
}
if config.HipchatUser != "" {
go bot.sendAnnouncementToUser(message)
}
w.WriteHeader(204)
}
func (bot *AnnounceBot) sendAnnouncementToUser(message string) {
subscribers, err := bot.getSubscribers()
if err != nil {
log.WithError(err).Error("An error occurred while retrieving subscribers")
return
}
talk, err := bot.getXMPPClient()
if err != nil {
log.WithError(err).WithField("message", message).Error("Error opening XMPP connection to send the announcement")
return
}
for _, userID := range subscribers {
userlog := log.WithField("user", userID).WithField("message", message)
user, _, err := bot.chatAPI.User.View(userID)
if err != nil {
userlog.WithError(err).Error("Could not find the user via the Hipchat API")
continue
}
_, err = talk.Send(xmpp.Chat{Remote: user.XMPPJid, Type: "chat", Text: message})
if err != nil {
userlog.WithError(err).Error("An error occurred while sending the announcement to a user")
continue
}
userlog.Info("Sent announcement to user")
}
}
func (bot *AnnounceBot) testHandler(w http.ResponseWriter, r *http.Request) {
_, err := bot.chatAPI.Room.Notification(
bot.Config.TestRoom,
&hipchat.NotificationRequest{
Message: "This is a test message!",
Notify: true,
},
)
if err != nil {
log.WithError(err).Error("An error occurred while sending the test message")
}
w.WriteHeader(204)
}