Thomas Maier 2020-01-05 15:09:36 +01:00
rodzic 8ec6f82ae6
commit 8024037666
2 zmienionych plików z 32 dodań i 33 usunięć

Wyświetl plik

@ -7,12 +7,12 @@ import (
) )
type Response struct { type Response struct {
Message string Message string
Code int Code int
} }
func errorResponse() (Response) { func errorResponse() Response {
return Response{"", http.StatusInternalServerError} return Response{"", http.StatusInternalServerError}
} }
// interface for parser functions (grafana, prometheus, ...) // interface for parser functions (grafana, prometheus, ...)
@ -33,7 +33,7 @@ func (h *messageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// send message to xmpp client // send message to xmpp client
h.messages <- m h.messages <- m
w.WriteHeader(response.Code) w.WriteHeader(response.Code)
w.Write( []byte(response.Message) ) w.Write([]byte(response.Message))
} }
// returns new handler with a given parser function // returns new handler with a given parser function
@ -84,26 +84,26 @@ func grafanaParserFunc(r *http.Request) (string, error, Response) {
SLACK PARSER SLACK PARSER
*************/ *************/
type SlackMessage struct { type SlackMessage struct {
Channel string `json:"channel"` Channel string `json:"channel"`
IconEmoji string `json:"icon_emoji"` IconEmoji string `json:"icon_emoji"`
Username string `json:"username"` Username string `json:"username"`
Text string `json:"text"` Text string `json:"text"`
Attachments []SlackAttachment `json:"attachments"` Attachments []SlackAttachment `json:"attachments"`
} }
type SlackAttachment struct { type SlackAttachment struct {
Color string `json:"color"` Color string `json:"color"`
Title string `json:"title"` Title string `json:"title"`
TitleLink string `json:"title_link"` TitleLink string `json:"title_link"`
Text string `json:"text"` Text string `json:"text"`
} }
func nonemptyAppendNewline(message string) (string) { func nonemptyAppendNewline(message string) string {
if len(message) == 0 { if len(message) == 0 {
return message return message
} }
return message+"\n" return message + "\n"
} }
func slackParserFunc(r *http.Request) (string, error, Response) { func slackParserFunc(r *http.Request) (string, error, Response) {
@ -123,19 +123,18 @@ func slackParserFunc(r *http.Request) (string, error, Response) {
} }
// contruct alert message // contruct alert message
message := "" message := ""
hasText := (alert.Text != "") hasText := (alert.Text != "")
if hasText { if hasText {
message = alert.Text message = alert.Text
} }
for _, attachment := range alert.Attachments {
message = nonemptyAppendNewline(message)
message += attachment.Title+"\n"
message += attachment.TitleLink+"\n\n"
message += attachment.Text
}
for _, attachment := range alert.Attachments {
message = nonemptyAppendNewline(message)
message += attachment.Title + "\n"
message += attachment.TitleLink + "\n\n"
message += attachment.Text
}
return message, nil, Response{"ok", http.StatusOK} return message, nil, Response{"ok", http.StatusOK}
} }

Wyświetl plik

@ -114,7 +114,7 @@ func main() {
// create reply with identical contents // create reply with identical contents
reply := MessageBody{ reply := MessageBody{
Message: stanza.Message{ Message: stanza.Message{
To: msg.From.Bare(), To: msg.From.Bare(),
From: address, From: address,
}, },
Body: msg.Body, Body: msg.Body,
@ -139,7 +139,7 @@ func main() {
// try to send message, ignore errors // try to send message, ignore errors
_ = xmppSession.Encode(MessageBody{ _ = xmppSession.Encode(MessageBody{
Message: stanza.Message{ Message: stanza.Message{
To: recipient, To: recipient,
From: address, From: address,
}, },
Body: m, Body: m,