From a2ebd7b29735585a83b4913506606d6f22d3c780 Mon Sep 17 00:00:00 2001
From: Erick Hitter <git-contrib@ethitter.com>
Date: Sun, 5 Aug 2018 18:27:55 -0700
Subject: [PATCH] Rethink how webhooks are sent

---
 .gitlab-ci.yml      |  1 +
 eth-log-alerting.go | 55 +++++++++++++++++++++------------------------
 2 files changed, 26 insertions(+), 30 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 47ae277..f7a15a4 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -10,6 +10,7 @@ image: golang:latest
 before_script:
     - ln -s /builds /go/src/git.ethitter.com
     - cd /go/src/git.ethitter.com/debian/eth-log-alerting
+    - go version
 
 stages:
     - test
diff --git a/eth-log-alerting.go b/eth-log-alerting.go
index 89e05ad..dac58a5 100644
--- a/eth-log-alerting.go
+++ b/eth-log-alerting.go
@@ -12,8 +12,8 @@ import (
 	"regexp"
 	"syscall"
 
-	"github.com/42wim/matterbridge/matterhook"
 	"github.com/asaskevich/govalidator"
+	"github.com/ashwanthkumar/slack-go-webhook"
 	"github.com/hpcloud/tail"
 )
 
@@ -33,15 +33,6 @@ type logConfig struct {
 	SearchRegex string `json:"search"`
 }
 
-type attachment struct {
-	Fallback string `json:"fallback,omitempty"`
-	Pretext  string `json:"pretext,omitempty"`
-	Text     string `json:"text"`
-	Color    string `json:"color,omitempty"`
-}
-
-type attachments []attachment
-
 var (
 	configPath string
 	logConfigs []logConfig
@@ -122,44 +113,48 @@ func tailLog(logCfg logConfig) {
 		return
 	}
 
-	mh := matterhook.New(logCfg.WebhookURL, matterhook.Config{DisableServer: true})
-
-	parseLinesAndSend(t, mh, logCfg)
+	parseLinesAndSend(t, logCfg)
 
 	t.Stop()
 	t.Cleanup()
 }
 
-func parseLinesAndSend(t *tail.Tail, mh *matterhook.Client, logCfg logConfig) {
+func parseLinesAndSend(t *tail.Tail, logCfg logConfig) {
 	for line := range t.Lines {
 		if line.Err != nil {
 			continue
 		}
 
 		if len(logCfg.SearchRegex) == 0 {
-			go sendLine(line, mh, logCfg)
+			go sendLine(line, logCfg)
 		} else if matched, _ := regexp.MatchString(logCfg.SearchRegex, line.Text); matched {
-			go sendLine(line, mh, logCfg)
+			go sendLine(line, logCfg)
 		}
 	}
 }
 
-func sendLine(line *tail.Line, mh *matterhook.Client, logCfg logConfig) {
-	atts := attachments{
-		attachment{
-			Fallback: fmt.Sprintf("New entry in %s", logCfg.LogPath),
-			Pretext:  fmt.Sprintf("In `%s` at `%s`:", logCfg.LogPath, line.Time),
-			Text:     fmt.Sprintf("    %s", line.Text),
-			Color:    logCfg.Color,
-		},
-	}
+func sendLine(line *tail.Line, logCfg logConfig) {
+	text := fmt.Sprintf("    %s", line.Text)
+
+	att := slack.Attachment{}
+	att.AddField(slack.Field{Title: "Fallback", Value: fmt.Sprintf("New entry in %s", logCfg.LogPath)})
+	att.AddField(slack.Field{Title: "Pretext", Value: fmt.Sprintf("In `%s` at `%s`:", logCfg.LogPath, line.Time)})
+	att.AddField(slack.Field{Title: "Text", Value: text})
+	att.AddField(slack.Field{Title: "Color", Value: logCfg.Color})
 
-	mh.Send(matterhook.OMessage{
+	payload := slack.Payload{
+		Text:        text,
+		Username:    logCfg.Username,
 		Channel:     logCfg.Channel,
-		UserName:    logCfg.Username,
-		Attachments: atts,
-		IconURL:     logCfg.IconURL,
-	})
+		IconEmoji:   logCfg.IconURL,
+		Attachments: []slack.Attachment{att},
+	}
+
+	err := slack.Send(logCfg.WebhookURL, "", payload)
+	if len(err) > 0 {
+		fmt.Printf("error: %s\n", err)
+	}
+
 }
 
 func setUpLogger() {
-- 
GitLab