update at 2019-11-20 14:42:59 by ehlxr

This commit is contained in:
ehlxr 2019-11-20 14:42:59 +08:00
parent eda6aaeb20
commit 7626953f4f
3 changed files with 19 additions and 37 deletions

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/ehlxr/ddgo
go 1.13 go 1.13
require ( 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 github.com/jessevdk/go-flags v1.4.1-0.20181221193153-c0795c8afcf4
golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056 // indirect golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056 // indirect
unknwon.dev/clog/v2 v2.0.0-beta.5 unknwon.dev/clog/v2 v2.0.0-beta.5

53
main.go
View File

@ -1,14 +1,12 @@
package main package main
import ( import (
"bytes"
"context" "context"
"encoding/base64" "encoding/base64"
"encoding/json"
"fmt" "fmt"
dt "github.com/JetBlink/dingtalk-notify-go-sdk"
"github.com/jessevdk/go-flags" "github.com/jessevdk/go-flags"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
@ -33,10 +31,15 @@ GoVersion: %s
bannerBase64 = "DQogX19fXyAgX19fXyAgICBfX18gIF9fX19fIA0KKCAgXyBcKCAgXyBcICAvIF9fKSggIF8gICkNCiApKF8pICkpKF8pICkoIChfLS4gKShfKSggDQooX19fXy8oX19fXy8gIFxfX18vKF9fX19fKQ0K" bannerBase64 = "DQogX19fXyAgX19fXyAgICBfX18gIF9fX19fIA0KKCAgXyBcKCAgXyBcICAvIF9fKSggIF8gICkNCiApKF8pICkpKF8pICkoIChfLS4gKShfKSggDQooX19fXy8oX19fXy8gIFxfX18vKF9fX19fKQ0K"
opts struct { opts struct {
Addr string `short:"a" long:"addr" default:":80" env:"ADDR" description:"Addr to listen on for HTTP server"` 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"` Token string `short:"t" long:"token" env:"TOKEN" description:"Dingtalk robot access token" required:"true"`
Version bool `short:"v" long:"version" description:"Show version info"` 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() { func init() {
@ -46,6 +49,8 @@ func init() {
func main() { func main() {
parseArg() parseArg()
robot = dt.NewRobot(opts.Token, opts.Secret)
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", requestHandle) mux.HandleFunc("/", requestHandle)
@ -134,40 +139,16 @@ func requestHandle(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, "read content from request form nil") _, _ = io.WriteString(w, "read content from request form nil")
return return
} }
info := dingToInfo(content)
_, _ = w.Write(info) err = robot.SendTextMessage(content, opts.AtMobiles, opts.IsAtAll)
}
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))
if err != nil { if err != nil {
log.Error("send request to %s %+v", log.Error("%+v", err)
opts.Addr, _, _ = fmt.Fprintln(w, err)
err) return
} }
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body) log.Info("send message <%s> success", content)
log.Info("send to %s data <%s> result is %s", _, _ = io.WriteString(w, "send message success")
opts.WebHookUrl,
b,
body)
return body
} }
// printVersion Print out version information // printVersion Print out version information

View File

@ -1 +1 @@
v0.0.1 v0.0.2