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

Wyświetl plik

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