go-bot/main.go

139 lines
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"
2024-04-20 15:11:49 +08:00
"log"
"net"
2024-04-01 09:42:29 +08:00
"net/http"
"strings"
2024-04-20 15:11:49 +08:00
"time"
"github.com/gin-gonic/gin"
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)
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
}
// 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 类型
2024-04-01 09:42:29 +08:00
var data map[string]interface{}
err := c.BindJSON(&data)
2024-04-01 09:42:29 +08:00
if err != nil {
c.JSON(400, gin.H{"error": gin.H{
"message": "Request must be proper JSON",
"type": "invalid_request_error",
"param": nil,
"code": err.Error(),
}})
2024-04-01 09:42:29 +08:00
return
}
// 打开数据库
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-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": data})
2024-07-14 21:38:39 +08:00
}
2024-04-01 09:42:29 +08:00
}
func main() {
cfg := config.GetConfig()
APIURL, ok := cfg["APIURL"].(string)
if !ok {
log.Fatal("加载配置失败!")
}
gin.SetMode(gin.ReleaseMode)
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 {
if opError, ok := err.(*net.OpError); ok && opError.Op == "listen" {
log.Fatal("服务器启动失败:端口被占用。")
} else if strings.Contains(err.Error(), "address already in use") {
log.Fatal("服务器启动失败:端口被占用。")
} else {
log.Fatal("服务器启动失败:", err)
}
}
2024-04-01 09:42:29 +08:00
}