go-bot/main.go

133 lines
3.5 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"
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 {
// Parse JSON data
// var data map[string]interface{}
// err := json.Unmarshal(jsonData, &data)
// if err != nil {
// log.Fatal(err)
// }
if data["post_type"] == "message" {
// id := data["message_id"].(float64)
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, 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
// Insert data into database
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-04-01 09:42:29 +08:00
func handlePost(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusBadRequest)
return
}
2024-04-03 22:04:03 +08:00
// fmt.Println("handlePost:", string(body))
2024-04-01 09:42:29 +08:00
// 解码 JSON 数据到 map[string]interface{} 类型的变量
var data map[string]interface{}
err = json.Unmarshal(body, &data)
if err != nil {
fmt.Println("Error decoding JSON:", err)
return
}
2024-04-20 15:11:49 +08:00
// Open SQLite database
db, err := sql.Open("sqlite3", "./data.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Create table
_, 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,
2024-04-21 12:22:35 +08:00
message_id INTEGER UNIQUE NOT NULL,
2024-04-20 15:11:49 +08:00
raw_message TEXT,
sender_user_id INTEGER,
sender_nickname TEXT,
sender_card TEXT,
2024-04-21 12:22:35 +08:00
sender_role TEXT,
message_seq INTEGER
2024-04-20 15:11:49 +08:00
)`)
if err != nil {
log.Fatal(err)
}
// Insert message into database
err = insertMessage(db, data)
if err != nil {
log.Fatal(err)
}
2024-04-01 09:42:29 +08:00
utils.Router(data)
w.Write([]byte("JSON data received successfully!"))
}
func main() {
cfg := config.GetConfig()
APIURL := cfg["APIURL"].(string)
// config.PrintConfig(cfg, "")
// print(cfg["AllowGroup"].([]interface{})[0].(string))
2024-04-03 22:02:08 +08:00
http.HandleFunc("/", handlePost)
// 协程支持
// http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// go handlePost(w, r)
// })
2024-04-01 09:42:29 +08:00
fmt.Println("Server listening on port 5580...")
// APIURL 为 go-cqhttp 配置的事件上报地址
http.ListenAndServe(APIURL, nil)
2024-04-01 09:42:29 +08:00
}