127 lines
3.4 KiB
Go
127 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"go-bot/config"
|
|
"go-bot/utils"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
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)
|
|
// 设置东八区时间
|
|
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)
|
|
fmt.Println(post_type, message_time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role)
|
|
// Insert data into database
|
|
_, 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) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
post_type, message_type, message_time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role)
|
|
fmt.Println("Data inserted successfully!")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|
|
// fmt.Println("handlePost:", string(body))
|
|
// 解码 JSON 数据到 map[string]interface{} 类型的变量
|
|
var data map[string]interface{}
|
|
err = json.Unmarshal(body, &data)
|
|
if err != nil {
|
|
fmt.Println("Error decoding JSON:", err)
|
|
return
|
|
}
|
|
|
|
// 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,
|
|
message_id INTEGER,
|
|
raw_message TEXT,
|
|
sender_user_id INTEGER,
|
|
sender_nickname TEXT,
|
|
sender_card TEXT,
|
|
sender_role TEXT
|
|
)`)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
// Insert message into database
|
|
err = insertMessage(db, data)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
utils.Router(data)
|
|
w.Write([]byte("JSON data received successfully!"))
|
|
|
|
}
|
|
|
|
func main() {
|
|
APIURL := config.GlobalConfig.Server.APIURL
|
|
|
|
// PORT := config.GlobalConfig.Server.Port
|
|
// fmt.Println(APIURL)
|
|
// fmt.Println(PORT)
|
|
http.HandleFunc("/", handlePost)
|
|
// 协程支持
|
|
// http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
// go handlePost(w, r)
|
|
// })
|
|
|
|
fmt.Println("Server listening on port 5580...")
|
|
// APIURL 为 go-cqhttp 配置的事件上报地址
|
|
http.ListenAndServe(APIURL, nil)
|
|
}
|