2024-04-01 09:42:29 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2024-04-20 15:11:49 +08:00
|
|
|
|
"database/sql"
|
2024-04-01 09:42:29 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"go-bot/config"
|
|
|
|
|
"go-bot/utils"
|
|
|
|
|
"io"
|
2024-04-20 15:11:49 +08:00
|
|
|
|
"log"
|
2024-04-01 09:42:29 +08:00
|
|
|
|
"net/http"
|
2024-04-20 15:11:49 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
2024-07-05 22:33:55 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-04-21 17:13:20 +08:00
|
|
|
|
"github.com/goccy/go-json"
|
2024-04-20 15:11:49 +08:00
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2024-04-01 09:42:29 +08:00
|
|
|
|
)
|
|
|
|
|
|
2024-04-20 15:11:49 +08:00
|
|
|
|
func insertMessage(db *sql.DB, data map[string]interface{}) error {
|
2024-07-02 18:16:26 +08:00
|
|
|
|
if data["post_type"] == "message" || data["post_type"] == "message_sent" {
|
2024-04-20 15:11:49 +08:00
|
|
|
|
post_type := data["post_type"].(string)
|
|
|
|
|
message_type := data["message_type"].(string)
|
2024-04-21 11:05:23 +08:00
|
|
|
|
loc, err := time.LoadLocation("Asia/Shanghai")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
message_time := (time.Unix(int64(data["time"].(float64)), 0).In(loc)).Format("2006-01-02 15:04:05")
|
2024-04-20 15:11:49 +08:00
|
|
|
|
group_id := data["group_id"].(float64)
|
|
|
|
|
message_id := data["message_id"].(float64)
|
|
|
|
|
raw_message := data["raw_message"].(string)
|
|
|
|
|
sender := data["sender"].(map[string]interface{})
|
|
|
|
|
sender_user_id := sender["user_id"].(float64)
|
|
|
|
|
sender_nickname := sender["nickname"].(string)
|
|
|
|
|
sender_card := sender["card"].(string)
|
|
|
|
|
if sender_card == "" {
|
|
|
|
|
sender_card = sender_nickname
|
|
|
|
|
}
|
|
|
|
|
sender_role := sender["role"].(string)
|
2024-04-21 12:22:35 +08:00
|
|
|
|
message_seq := data["message_seq"].(float64)
|
|
|
|
|
|
2024-07-14 21:38:39 +08:00
|
|
|
|
fmt.Println("消息类型:", post_type, " 发送时间:", message_time, " 群号:", int64(group_id), " 消息id:", int64(message_id), " 原始消息:", raw_message, " 发送者id:", sender_user_id, " 发送者昵称:", sender_nickname, " 发送者名片:", sender_card, " 发送者角色:", sender_role, " 消息序列:", int64(message_seq))
|
2024-04-21 12:22:35 +08:00
|
|
|
|
_, err = db.Exec("INSERT INTO messages ( post_type, message_type, time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role, message_seq) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
|
|
|
post_type, message_type, message_time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role, message_seq)
|
2024-04-20 15:11:49 +08:00
|
|
|
|
fmt.Println("Data inserted successfully!")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 22:33:55 +08:00
|
|
|
|
// handlePost 处理 POST 请求
|
|
|
|
|
func handlePost(c *gin.Context) {
|
|
|
|
|
// 从请求体中读取数据
|
|
|
|
|
body, err := io.ReadAll(c.Request.Body)
|
2024-04-01 09:42:29 +08:00
|
|
|
|
if err != nil {
|
2024-07-05 22:33:55 +08:00
|
|
|
|
// 请求体数据读取失败
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Error reading request body"})
|
2024-04-01 09:42:29 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2024-07-05 22:33:55 +08:00
|
|
|
|
|
|
|
|
|
// 将 JSON 数据解析为 map 类型
|
2024-04-01 09:42:29 +08:00
|
|
|
|
var data map[string]interface{}
|
|
|
|
|
err = json.Unmarshal(body, &data)
|
|
|
|
|
if err != nil {
|
2024-07-05 22:33:55 +08:00
|
|
|
|
// JSON 数据解析失败
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Error decoding JSON"})
|
2024-04-01 09:42:29 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2024-07-14 21:38:39 +08:00
|
|
|
|
// 输出解析后的 JSON 数据
|
|
|
|
|
fmt.Printf("data: %s\n\n", string(body))
|
2024-07-05 22:33:55 +08:00
|
|
|
|
// 打开数据库
|
2024-04-20 15:11:49 +08:00
|
|
|
|
db, err := sql.Open("sqlite3", "./data.db")
|
|
|
|
|
if err != nil {
|
2024-07-05 22:33:55 +08:00
|
|
|
|
// 数据库打开失败
|
2024-04-20 15:11:49 +08:00
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer db.Close()
|
2024-07-05 22:33:55 +08:00
|
|
|
|
|
|
|
|
|
// 创建表
|
2024-04-20 15:11:49 +08:00
|
|
|
|
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS messages (
|
2024-07-05 22:33:55 +08:00
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
post_type TEXT,
|
|
|
|
|
message_type TEXT,
|
|
|
|
|
time DATETIME,
|
|
|
|
|
group_id INTEGER,
|
|
|
|
|
message_id INTEGER UNIQUE NOT NULL,
|
|
|
|
|
raw_message TEXT,
|
|
|
|
|
sender_user_id INTEGER,
|
|
|
|
|
sender_nickname TEXT,
|
|
|
|
|
sender_card TEXT,
|
|
|
|
|
sender_role TEXT,
|
2024-04-21 12:22:35 +08:00
|
|
|
|
message_seq INTEGER
|
2024-07-05 22:33:55 +08:00
|
|
|
|
)`)
|
2024-04-20 15:11:49 +08:00
|
|
|
|
if err != nil {
|
2024-07-05 22:33:55 +08:00
|
|
|
|
// 表创建失败
|
2024-04-20 15:11:49 +08:00
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2024-07-05 22:33:55 +08:00
|
|
|
|
|
|
|
|
|
// 插入数据
|
2024-04-20 15:11:49 +08:00
|
|
|
|
err = insertMessage(db, data)
|
|
|
|
|
if err != nil {
|
2024-07-05 22:33:55 +08:00
|
|
|
|
// 数据插入失败
|
2024-04-20 15:11:49 +08:00
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 22:33:55 +08:00
|
|
|
|
// 调用路由处理函数
|
2024-07-14 21:38:39 +08:00
|
|
|
|
if data["post_type"] == "message" || data["post_type"] == "message_sent" {
|
|
|
|
|
utils.Router(data)
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "JSON data received successfully!"})
|
|
|
|
|
}
|
2024-04-01 09:42:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
2024-06-30 16:12:26 +08:00
|
|
|
|
cfg := config.GetConfig()
|
2024-06-30 23:34:00 +08:00
|
|
|
|
APIURL, ok := cfg["APIURL"].(string)
|
|
|
|
|
if !ok {
|
|
|
|
|
log.Fatal("加载配置失败!")
|
|
|
|
|
}
|
2024-04-03 21:53:57 +08:00
|
|
|
|
|
2024-07-05 22:33:55 +08:00
|
|
|
|
r := gin.Default()
|
|
|
|
|
r.POST("/", handlePost)
|
2024-04-01 09:42:29 +08:00
|
|
|
|
|
2024-07-05 22:33:55 +08:00
|
|
|
|
fmt.Println("Server listening on", APIURL, "...")
|
|
|
|
|
if err := r.Run(APIURL); err != nil {
|
|
|
|
|
log.Fatal("服务器启动失败:", err)
|
|
|
|
|
}
|
2024-04-01 09:42:29 +08:00
|
|
|
|
}
|