/
hipchat.clj
76 lines (66 loc) · 2.51 KB
/
hipchat.clj
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
(ns ^{:doc "Forwards events to HipChat"
:author "Hubert Iwaniuk"}
riemann.hipchat
(:require [clj-http.client :as client]
[clojure.string :refer [join]]))
(defn- message-colour [ev]
"Set the colour to be used in the
hipchat message."
(let [state (or (:state ev) (:state (first ev)))]
(get {"ok" "green"
"critical" "red"
"error" "red"}
state
"yellow")))
(defn ^:private chat-url [server room]
(str "https://" server "/v2/room/" room "/notification"))
(defn- format-message [ev]
"Formats a message, accepts a single
event or a sequence of events."
(join "\n\n"
(map
(fn [e]
(str
"Host: " (:host e)
" \nService: " (:service e)
" \nState: " (:state e)
" \nMetric: " (:metric e)
" \nDescription: " (:description e))) ev)))
(defn- format-event [{:keys [message] :as conf} event]
"Creates an event suitable for posting to hipchat."
(merge {:color (message-colour event) :from "riemann"}
(dissoc conf :server :room_id)
(when-not message
{:message (format-message (flatten [event]))})))
(defn- post
"POST to the HipChat API."
[token {:keys [server room_id] :as conf} event]
(client/post (str (chat-url server room_id) "?auth_token=" token)
{:form-params (format-event (assoc conf :message_format "text") event)
:socket-timeout 5000
:conn-timeout 5000
:accept :json
:throw-entire-message? true}))
(defn hipchat
"Creates a HipChat adapter. Takes your HipChat v2 authentication token,
and returns a function which posts a message to a HipChat.
You can any a personal or room-specific token, which can be obtained from
your profile page or a specific room.
More on api tokens at https://www.hipchat.com/docs/apiv2/auth
If you're using hosted HipChat, you can leave out :server (or set it to
'api.hipchat.com').
```clojure
(let [hc (hipchat {:server \"...\"
:token \"...\"
:room 12345
:notify 0})]
(changed-state hc))
```"
[{:keys [server token room notify]}]
(if (not (= 40 (count token)))
(throw (IllegalArgumentException. "This adapter now requires a v2 API key")))
(fn [e] (post token
{:server (or server "api.hipchat.com")
:room_id room
:notify notify}
e)))