update at 2020-11-12 11:15:12 by ehlxr

This commit is contained in:
ehlxr 2020-11-12 11:15:12 +08:00
parent c743bc6abb
commit f575f3244c

103
main.go
View File

@ -2,6 +2,8 @@ package main
import ( import (
"context" "context"
"encoding/json"
"errors"
"fmt" "fmt"
dtn "github.com/JetBlink/dingtalk-notify-go-sdk" dtn "github.com/JetBlink/dingtalk-notify-go-sdk"
"github.com/ehlxr/ddgo/pkg" "github.com/ehlxr/ddgo/pkg"
@ -34,7 +36,14 @@ func main() {
func start() { func start() {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", requestHandle) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.Header.Get("Content-Type") {
case "application/json":
handlePostJson(w, r)
case "application/x-www-form-urlencoded":
handlePostForm(w, r)
}
})
server := &http.Server{ server := &http.Server{
Addr: pkg.Opts.Addr, Addr: pkg.Opts.Addr,
@ -80,7 +89,7 @@ func initLog() {
} }
} }
func requestHandle(w http.ResponseWriter, r *http.Request) { func handlePostForm(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
log.Error("parse request form %+v", log.Error("parse request form %+v",
@ -90,47 +99,85 @@ func requestHandle(w http.ResponseWriter, r *http.Request) {
return return
} }
content := r.Form.Get("content") if err = sendMessage(&Message{
if content == "" { r.Form.Get("content"),
log.Error("read content from request form nil") r.Form.Get("at"),
_, _ = io.WriteString(w, "read content from request form nil") r.Form.Get("app"),
r.Form.Get("dt"),
r.Form.Get("ds"),
}); err != nil {
http.Error(w, err.Error(), 400)
log.Error(err.Error())
return return
} }
_, _ = io.WriteString(w, "success")
return
}
func handlePostJson(w http.ResponseWriter, r *http.Request) {
if r.Body == nil {
http.Error(w, "Please sendMessage a request body", 400)
log.Error("Please sendMessage a request body")
return
}
m := &Message{}
err := json.NewDecoder(r.Body).Decode(m)
if err != nil {
http.Error(w, err.Error(), 400)
log.Error(err.Error())
return
}
if err = sendMessage(m); err != nil {
http.Error(w, err.Error(), 400)
log.Error(err.Error())
return
}
_, _ = io.WriteString(w, "success")
return
}
type Message struct {
Content string `json:"content" desc:"消息内容"`
At string `json:"at" desc:"被@人的手机号"`
App string `json:"app" desc:"发送消息应用名称(添加到消息之前)"`
Dt string `json:"dt" desc:"钉钉机器人 token"`
Ds string `json:"ds" desc:"钉钉机器人签名 secret"`
}
func sendMessage(m *Message) error {
if m.Content == "" {
return errors.New("content must not be null")
}
ats := pkg.Opts.Robot.AtMobiles ats := pkg.Opts.Robot.AtMobiles
at := r.Form.Get("at") if m.At != "" {
if at != "" { ats = append(ats, strings.Split(m.At, ",")...)
ats = append(ats, strings.Split(at, ",")...)
} }
app := r.Form.Get("app") if m.App != "" {
if app != "" { m.Content = fmt.Sprintf("%s\n%s", m.App, m.Content)
content = fmt.Sprintf("%s\n%s", app, content)
} }
dtRobot := dingTalk dtRobot := dingTalk
dt := r.Form.Get("dt")
ds := r.Form.Get("ds") if m.Ds != "" && m.Dt != "" {
if ds != "" && dt != "" { dtRobot = dtn.NewRobot(m.Dt, m.Ds)
dtRobot = dtn.NewRobot(dt, ds)
} }
if limiter.IsAvailable() { if limiter.IsAvailable() {
err = dtRobot.SendTextMessage(content, ats, pkg.Opts.Robot.IsAtAll) err := dtRobot.SendTextMessage(m.Content, ats, pkg.Opts.Robot.IsAtAll)
if err != nil { if err != nil {
log.Error("%+v", err) return err
_, _ = fmt.Fprintln(w, err)
return
} }
log.Info("send message <%s> success", content) log.Info("sendMessage message <%s> success", m.Content)
_, _ = io.WriteString(w, "success")
return
} else { } else {
log.Error("dingTalk 1 m allow send 20 msg. msg %v discarded.", return errors.New("dingTalk 1 m allow sendMessage 20 msg. msg discarded")
content)
_, _ = io.WriteString(w, "dingTalk 1 m allow send 20 msg. msg discarded.")
return
} }
return nil
} }