From 7626953f4fabb23fa53343e411ea691a19d330e6 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Wed, 20 Nov 2019 14:42:59 +0800 Subject: [PATCH] update at 2019-11-20 14:42:59 by ehlxr --- go.mod | 1 + main.go | 53 +++++++++++++++++------------------------------------ version | 2 +- 3 files changed, 19 insertions(+), 37 deletions(-) diff --git a/go.mod b/go.mod index 8a13f9c..004650d 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/ehlxr/ddgo go 1.13 require ( + github.com/JetBlink/dingtalk-notify-go-sdk v0.0.0-20191112085213-0dc836cea13e // indirect github.com/jessevdk/go-flags v1.4.1-0.20181221193153-c0795c8afcf4 golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056 // indirect unknwon.dev/clog/v2 v2.0.0-beta.5 diff --git a/main.go b/main.go index 60f2a75..9256869 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,12 @@ package main import ( - "bytes" "context" "encoding/base64" - "encoding/json" "fmt" + dt "github.com/JetBlink/dingtalk-notify-go-sdk" "github.com/jessevdk/go-flags" "io" - "io/ioutil" "net/http" "os" "os/signal" @@ -33,10 +31,15 @@ GoVersion: %s bannerBase64 = "DQogX19fXyAgX19fXyAgICBfX18gIF9fX19fIA0KKCAgXyBcKCAgXyBcICAvIF9fKSggIF8gICkNCiApKF8pICkpKF8pICkoIChfLS4gKShfKSggDQooX19fXy8oX19fXy8gIFxfX18vKF9fX19fKQ0K" opts struct { - Addr string `short:"a" long:"addr" default:":80" env:"ADDR" description:"Addr to listen on for HTTP server"` - WebHookUrl string `short:"u" long:"webhook-url" env:"URL" description:"Webhook url of dingding" required:"true"` - Version bool `short:"v" long:"version" description:"Show version info"` + Addr string `short:"a" long:"addr" default:":80" env:"ADDR" description:"Addr to listen on for HTTP server"` + Token string `short:"t" long:"token" env:"TOKEN" description:"Dingtalk robot access token" required:"true"` + Secret string `short:"s" long:"secret" env:"SECRET" description:"Dingtalk robot secret"` + AtMobiles []string `short:"m" long:"at-mobiles" env:"AT_MOBILES" env-delim:"," description:"The mobile of the person will be @"` + IsAtAll bool `short:"e" long:"at-all" env:"AT_ALL" description:"Whether @ everyone"` + Version bool `short:"v" long:"version" description:"Show version info"` } + + robot *dt.Robot ) func init() { @@ -46,6 +49,8 @@ func init() { func main() { parseArg() + robot = dt.NewRobot(opts.Token, opts.Secret) + mux := http.NewServeMux() mux.HandleFunc("/", requestHandle) @@ -134,40 +139,16 @@ func requestHandle(w http.ResponseWriter, r *http.Request) { _, _ = io.WriteString(w, "read content from request form nil") return } - info := dingToInfo(content) - _, _ = w.Write(info) -} - -func dingToInfo(msg string) []byte { - content, data := make(map[string]string), make(map[string]interface{}) - - content["content"] = msg - data["msgtype"] = "text" - data["text"] = content - b, _ := json.Marshal(data) - - log.Info("send to %s data <%s>", - opts.WebHookUrl, - b) - - resp, err := http.Post(opts.WebHookUrl, - "application/json", - bytes.NewBuffer(b)) + err = robot.SendTextMessage(content, opts.AtMobiles, opts.IsAtAll) if err != nil { - log.Error("send request to %s %+v", - opts.Addr, - err) - + log.Error("%+v", err) + _, _ = fmt.Fprintln(w, err) + return } - defer resp.Body.Close() - body, _ := ioutil.ReadAll(resp.Body) - log.Info("send to %s data <%s> result is %s", - opts.WebHookUrl, - b, - body) - return body + log.Info("send message <%s> success", content) + _, _ = io.WriteString(w, "send message success") } // printVersion Print out version information diff --git a/version b/version index 95e94cd..90ab6e9 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.0.1 \ No newline at end of file +v0.0.2 \ No newline at end of file