没有什么好讲的,直接贴代码,消息格式我用的markdown。
sendMsg.go:
package main
import (
"flag"
"fmt"
"github.com/goccy/go-json"
"github.com/tidwall/gjson"
"io/ioutil"
"net/http"
"strings"
)
func main() {
// params
var msg string
flag.StringVar(&msg, "msg", "null", "空消息")
flag.Parse()
type Params struct {
Msgtype string `json:"msgtype"`
Markdown struct {
Content string `json:"content"`
} `json:"markdown"`
}
params := Params{
Msgtype: "markdown",
Markdown: struct {
Content string `json:"content"`
}{
Content: msg,
},
}
//fmt.Printf("%v\n", params)
json_date, err := json.Marshal(¶ms)
if err != nil {
fmt.Sprintf("序列化失败:%s", err)
fmt.Println()
}
//fmt.Printf("序列化后:%v\n", string(json_date))
// POST
var wx_send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxx"
//fmt.Println(wx_send_url)
response, err := http.Post(wx_send_url, "text/html;application/json", strings.NewReader(string(json_date)))
if err != nil {
fmt.Println(err)
}
// Get errcode
body, err := ioutil.ReadAll(response.Body)
defer response.Body.Close()
errcode := gjson.Get(string(body), "errcode").String()
if errcode != "0" {
fmt.Println("fail")
//fmt.Println(json.Unmarshal([]byte(str), &body))
} else {
fmt.Println("success")
}
}
交叉编译参数的设置:
win 编译生成Linux下的可执行文件:
go env -w CGO_ENABLED=0
go env -w GOOS=linux
go env -w GOARCH=amd64
编译完记得改回来:
go env -w CGO_ENABLED=0
go env -w GOOS=windows
go env -w GOARCH=amd64