修复json格式化bug

master
Administrator 2024-05-02 21:04:26 +08:00
parent e2d2fcfd26
commit cdc5ae2a9b
3 changed files with 25 additions and 11 deletions

View File

@ -26,7 +26,7 @@ _/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
\/ \/ \/ \//_____/ `) \/ \/ \/ \//_____/ `)
fmt.Println("-----------------------------------------------------------------------------------------") fmt.Println("-----------------------------------------------------------------------------------------")
fmt.Println("-----------------------------------------------------------------------------------------") fmt.Println("-----------------------------------------------------------------------------------------")
flag.BoolVar(&liveMode, "livemode", false, "即时模式关闭") flag.BoolVar(&liveMode, "livemode", true, "即时模式默认关闭")
flag.Parse() flag.Parse()
if !liveMode { if !liveMode {
fmt.Println("目前是定时任务模式") fmt.Println("目前是定时任务模式")

View File

@ -1,9 +1,12 @@
package qywechat package qywechat
import ( import (
"bytes" "encoding/json"
"fmt"
"io"
"log" "log"
"net/http" "net/http"
"strings"
) )
//企业微信机器人发送 //企业微信机器人发送
@ -12,22 +15,33 @@ const webhookURL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7b50e0c
func Send(repContent string) { func Send(repContent string) {
// 替换为你的企业微信机器人Webhook URL // 替换为你的企业微信机器人Webhook URL
jsonData, err := json.Marshal(repContent)
// 创建消息体 if err != nil {
requestBody := []byte(`{ fmt.Println("转换为JSON时出错:", err)
"msgtype": "text", return
"text": {
"content": "` + repContent + `"
} }
}`) repContent = string(jsonData)
// 去掉最后三个字符和第一个引号
repContent = repContent[:len(repContent)-3]
repContent = repContent[1:]
fmt.Println(repContent)
// 创建消息体
repContent = fmt.Sprintf(`{"msgtype": "text", "text": {"content": "%s"}}`, repContent)
//repContent = strings.Replace(repContent, "\\", "", 9)
//lastIndex := strings.LastIndex(repContent, "\\")
//if lastIndex != -1 {
// repContent = repContent[:lastIndex] + repContent[lastIndex+1:]
//}
// 发送HTTP POST请求到企业微信机器人Webhook // 发送HTTP POST请求到企业微信机器人Webhook
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(requestBody)) fmt.Println(repContent)
resp, err := http.Post(webhookURL, "application/json", strings.NewReader(repContent))
if err != nil { if err != nil {
log.Fatalf("发送企业微信机器人失败,错误是: %v", err) log.Fatalf("发送企业微信机器人失败,错误是: %v", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(resp.StatusCode, string(body))
log.Println("发送企业微信机器人成功") log.Println("发送企业微信机器人成功")
} }

Binary file not shown.