-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.go
143 lines (124 loc) · 3.15 KB
/
func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"github.com/gin-gonic/gin"
)
// 处理 GitHub Webhook 请求
func handleWebhook(c *gin.Context) {
var payload map[string]interface{}
if err := c.ShouldBindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// 打印收到的 GitHub Webhook 数据
fmt.Printf("Received Webhook: %v\n", payload)
// 处理 Webhook 数据(如提取 Issue 信息)
action, _ := payload["action"].(string)
issue, _ := payload["issue"].(map[string]interface{})
title, _ := issue["title"].(string)
user, _ := issue["user"].(map[string]interface{})
username, _ := user["login"].(string)
body, _ := issue["body"].(string)
url, _ := issue["html_url"].(string)
// 构建要发送的消息
hbody := handleMarkdown(body)
class, date, time, _title, err := extractIssueParams(title)
if err != nil {
fmt.Println("Error extractIssueParams:", err)
return
}
filename := cleanFileName(_title) + ".png"
// 构建消息
var titlet string
var bodyt string
var ititle string
titlet = "[" + class + "]" + _title
switch class {
case "茶话会":
bodyt = "日期:" + date + " " + "时长:" + time + "\n" + hbody
ititle = "## " + _title + "\r\n" + "日期:" + date + " " + "时长:" + time + "\r\n"
default:
bodyt = ""
timeinit()
ititle = "## " + class + _title + "\r\n" + "日期:" + currentDate + "\r\n"
}
// 获取图片
_, err = getimg(ititle+body, filename)
if err != nil {
fmt.Println("Error getimg:", err)
return
}
msg := map[string]string{
"body": bodyt,
"title": titlet,
"username": username,
"url": url,
"date": date,
}
//群号
group_id := config.GroupID
// 通过 WebSocket 向 QQ 机器人发送消息
// test := true
if action == "opened" {
message_id, err := sendMessageToWebSocket(filename, int64(group_id))
if err != nil {
fmt.Println("Error sending group message:", err)
return
}
//设置为精华消息
err = setEssenceMsg(message_id)
if err != nil {
fmt.Println("Error setting essence message:", err)
return
}
//设置为群公告
err = setGroupNotice(msg, int64(group_id))
if err != nil {
fmt.Println("Error setting essence message:", err)
return
}
}
c.JSON(http.StatusOK, gin.H{"status": "Webhook received"})
}
// 处理 WebSocket 连接
func handleWebSocket(c *gin.Context) {
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to upgrade connection"})
return
}
defer ws.Close()
for {
// 读取 WebSocket 消息
_, msg, err := ws.ReadMessage()
if err != nil {
fmt.Println("Read Error:", err)
break
}
fmt.Printf("Received from WebSocket: %s\n", msg)
}
}
// 加载配置文件
func loadConfig(filename string) (*Config, error) {
// 打开文件
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
// 读取文件内容
bytes, err := io.ReadAll(file)
if err != nil {
return nil, err
}
// 解析 JSON
var config Config
if err := json.Unmarshal(bytes, &config); err != nil {
return nil, err
}
return &config, nil
}