From da30e9987f5282b5303a77c42dd16c97ac4d3bfb Mon Sep 17 00:00:00 2001 From: Yun Date: Fri, 9 Aug 2019 12:36:35 +0900 Subject: [PATCH] append webhook feature --- cmd/config.go | 3 +++ utils/app.go | 3 +++ utils/websocket.go | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/cmd/config.go b/cmd/config.go index 4fb8ad5..1713a36 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -27,6 +27,9 @@ var configCmd = &cobra.Command{ TriggerInterval: "5", FeeAmount: "1000000uluna", + + WebHookURL: "", + WebHookDataKey: "text", } if _, err := os.Stat(g.KeyDir); os.IsNotExist(err) { diff --git a/utils/app.go b/utils/app.go index 1b85929..3494244 100644 --- a/utils/app.go +++ b/utils/app.go @@ -39,6 +39,9 @@ type SantaApp struct { TriggerInterval string `json:"trigger_interval" yaml:"trigger_interval"` FeeAmount string `json:"fee_amount" yaml:"fee_amount"` + WebHookURL string `json:"webhook_url" yaml:"webhook_url"` + WebHookDataKey string `json:"webhook_data_key" yaml:"webhook_data_key"` + Version string `yaml:"version,omitempty"` Commit string `yaml:"commit,omitempty"` Branch string `yaml:"branch,omitempty"` diff --git a/utils/websocket.go b/utils/websocket.go index e2a73bf..7ef6057 100644 --- a/utils/websocket.go +++ b/utils/websocket.go @@ -1,8 +1,11 @@ package utils import ( + "bytes" "encoding/json" + "fmt" "log" + "net/http" "net/url" "os" "strconv" @@ -80,6 +83,21 @@ func (app SantaApp) ListenNewBLock(isTest bool) { txHash, err := app.SendTx(blockEvent.Block.ChainID) if err != nil { log.Printf("[Fail] to send tx: %s", err.Error()) + + if app.WebHookURL != "" && app.WebHookDataKey != "" { + notiBody, err := json.Marshal(map[string]string{ + app.WebHookDataKey: fmt.Sprintf("[Fail] sending tx: %s", err.Error()), + }) + + if err != nil { + continue + } + + // send notification to slack + http.Post(app.WebHookURL, "application/json", bytes.NewBuffer(notiBody)) + continue + } + } log.Printf("[Success] Height: %d,\tTxHash: %s\n", blockEvent.Block.Height, txHash)