go-bot/main.go

125 lines
3.4 KiB
Go
Raw Normal View History

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"
"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 {
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)
fmt.Println(post_type, message_time, int64(group_id), int64(message_id), raw_message, 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
}
// 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 {
// 请求体数据读取失败
c.JSON(http.StatusBadRequest, gin.H{"error": "Error reading request body"})
2024-04-01 09:42:29 +08:00
return
}
// 将 JSON 数据解析为 map 类型
2024-04-01 09:42:29 +08:00
var data map[string]interface{}
err = json.Unmarshal(body, &data)
if err != nil {
// JSON 数据解析失败
c.JSON(http.StatusBadRequest, gin.H{"error": "Error decoding JSON"})
2024-04-01 09:42:29 +08:00
return
}
2024-04-20 15:11:49 +08:00
// 打开数据库
2024-04-20 15:11:49 +08:00
db, err := sql.Open("sqlite3", "./data.db")
if err != nil {
// 数据库打开失败
2024-04-20 15:11:49 +08:00
log.Fatal(err)
}
defer db.Close()
// 创建表
2024-04-20 15:11:49 +08:00
_, 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,
2024-04-21 12:22:35 +08:00
message_seq INTEGER
)`)
2024-04-20 15:11:49 +08:00
if err != nil {
// 表创建失败
2024-04-20 15:11:49 +08:00
log.Fatal(err)
}
// 插入数据
2024-04-20 15:11:49 +08:00
err = insertMessage(db, data)
if err != nil {
// 数据插入失败
2024-04-20 15:11:49 +08:00
log.Fatal(err)
}
// 调用路由处理函数
2024-04-01 09:42:29 +08:00
utils.Router(data)
c.JSON(http.StatusOK, gin.H{"message": "JSON data received successfully!"})
2024-04-01 09:42:29 +08:00
}
func main() {
cfg := config.GetConfig()
APIURL, ok := cfg["APIURL"].(string)
if !ok {
log.Fatal("加载配置失败!")
}
r := gin.Default()
r.POST("/", handlePost)
2024-04-01 09:42:29 +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
}