main
Administrator 2024-04-03 13:55:27 +08:00
parent 2ea85c9184
commit 3db09ecc6d
3 changed files with 128 additions and 0 deletions

83
func.go Normal file
View File

@ -0,0 +1,83 @@
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
// 处理字节切片,返回结构体
func (O OriginContent) Dealwithbody(body []byte) OriginContent {
alreadyDealwithbody := string(body)
spiltdecodedvalue := strings.Split(alreadyDealwithbody, "&")
value := make(map[int][]string)
for k := range spiltdecodedvalue {
value[k] = strings.Split(spiltdecodedvalue[k], "=")
}
urlDecode, err := url.QueryUnescape(value[1][1])
if err != nil {
fmt.Printf("urldecode失败,错误为:%v", err)
}
var wow = NewOrignContent(value[0][1], urlDecode, value[2][1])
fmt.Println(wow)
return wow
}
func verificationCodeHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "方法不允许哦^ ^", http.StatusMethodNotAllowed)
return
}
// 读取请求体数据
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "读取请求体失败", http.StatusInternalServerError)
return
}
// 把返回体通过方法赋值给全局对象
newObj = newObj.Dealwithbody(body)
//fmt.Printf("%s%s%s\n", time.Now(), " ", content)
// 如果请求内容包含 "验证码",则发送到企业微信机器人
if strings.Contains(newObj.content, "验证码") || strings.Contains(newObj.content, "华为") {
sendToWechatBot(newObj)
}
// 返回响应
w.Write([]byte("请求企业微信~"))
}
func (O OriginContent) Send() {
// 这里实现发送信息到企业微信机器人的逻辑
var message string
if strings.Contains(newObj.content, "华为") {
// 如果 content 中包含 "华为"
message = fmt.Sprintf(`{"msgtype": "text", "text": {"content": "%s","mentioned_mobile_list":["18701488607"]}}`, newObj.content)
} else {
// 如果 content 中不包含 "华为"
message = fmt.Sprintf(`{"msgtype": "text", "text": {"content": "%s"}}`, newObj.content)
}
// 发送 HTTP POST 请求到企业微信机器人的 Webhook
resp, err := http.Post(webhookURLVerCode, "application/json", strings.NewReader(message))
if err != nil {
fmt.Println("Error sending message to Wechat Bot:", err)
return
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Printf("关闭返回体失败,错误是:%v", err)
}
}(resp.Body)
// 读取响应
respBody, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("读取返回体失败~:", err)
return
}
fmt.Println("发送webhook成功~", string(respBody))
}

13
main.go Normal file
View File

@ -0,0 +1,13 @@
package main
import (
"net/http"
)
func main() {
http.HandleFunc("/verificationcode/", verificationCodeHandler)
err := http.ListenAndServe(":8080", nil)
if err != nil {
return
}
}

32
struct.go Normal file
View File

@ -0,0 +1,32 @@
package main
// 声明接口类型Notifier
type Notifier interface {
Send() //注册方法Send()
}
// 常量定义,qy机器人的webhook地址
const webhookURLVerCode = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7516e5e3-ba65-41e0-b858-3b3ef618fae2"
// 结构体定义
type OriginContent struct {
from string
content string
timestamp string
}
// 调用send()方法
func sendToWechatBot(N Notifier) {
N.Send()
}
// 结构体构造
func NewOrignContent(from, content, timestamp string) OriginContent {
return OriginContent{
from,
content,
timestamp,
}
}
var newObj OriginContent