127 lines
3.7 KiB
Go
127 lines
3.7 KiB
Go
package main
|
||
|
||
import (
|
||
"database/sql"
|
||
"fmt"
|
||
"go-bot/config"
|
||
"go-bot/utils"
|
||
"io"
|
||
"log"
|
||
"net/http"
|
||
"time"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/goccy/go-json"
|
||
_ "github.com/mattn/go-sqlite3"
|
||
)
|
||
|
||
func insertMessage(db *sql.DB, data map[string]interface{}) error {
|
||
if data["post_type"] == "message" || data["post_type"] == "message_sent" {
|
||
post_type := data["post_type"].(string)
|
||
message_type := data["message_type"].(string)
|
||
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")
|
||
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)
|
||
message_seq := data["message_seq"].(float64)
|
||
|
||
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))
|
||
_, 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)
|
||
fmt.Println("Data inserted successfully!")
|
||
if err != nil {
|
||
log.Fatal(err)
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// handlePost 处理 POST 请求
|
||
func handlePost(c *gin.Context) {
|
||
// 从请求体中读取数据
|
||
body, err := io.ReadAll(c.Request.Body)
|
||
if err != nil {
|
||
// 请求体数据读取失败
|
||
c.JSON(http.StatusBadRequest, gin.H{"error": "Error reading request body"})
|
||
return
|
||
}
|
||
|
||
// 将 JSON 数据解析为 map 类型
|
||
var data map[string]interface{}
|
||
err = json.Unmarshal(body, &data)
|
||
if err != nil {
|
||
// JSON 数据解析失败
|
||
c.JSON(http.StatusBadRequest, gin.H{"error": "Error decoding JSON"})
|
||
return
|
||
}
|
||
// 输出解析后的 JSON 数据
|
||
fmt.Printf("data: %s\n\n", string(body))
|
||
// 打开数据库
|
||
db, err := sql.Open("sqlite3", "./data.db")
|
||
if err != nil {
|
||
// 数据库打开失败
|
||
log.Fatal(err)
|
||
}
|
||
defer db.Close()
|
||
|
||
// 创建表
|
||
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS messages (
|
||
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,
|
||
message_seq INTEGER
|
||
)`)
|
||
if err != nil {
|
||
// 表创建失败
|
||
log.Fatal(err)
|
||
}
|
||
|
||
// 插入数据
|
||
err = insertMessage(db, data)
|
||
if err != nil {
|
||
// 数据插入失败
|
||
log.Fatal(err)
|
||
}
|
||
|
||
// 调用路由处理函数
|
||
if data["post_type"] == "message" || data["post_type"] == "message_sent" {
|
||
utils.Router(data)
|
||
c.JSON(http.StatusOK, gin.H{"message": "JSON data received successfully!"})
|
||
}
|
||
}
|
||
|
||
func main() {
|
||
cfg := config.GetConfig()
|
||
APIURL, ok := cfg["APIURL"].(string)
|
||
if !ok {
|
||
log.Fatal("加载配置失败!")
|
||
}
|
||
|
||
r := gin.Default()
|
||
r.POST("/", handlePost)
|
||
|
||
fmt.Println("Server listening on", APIURL, "...")
|
||
if err := r.Run(APIURL); err != nil {
|
||
log.Fatal("服务器启动失败:", err)
|
||
}
|
||
}
|