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
|
|
|
"encoding/json"
|
|
|
|
"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/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)
|
|
|
|
message_time := (time.Unix(int64(data["time"].(float64)), 0).UTC()).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
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-04-01 09:42:29 +08:00
|
|
|
utils.Router(data)
|
|
|
|
w.Write([]byte("JSON data received successfully!"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-04-03 21:53:57 +08:00
|
|
|
APIURL := config.GlobalConfig.Server.APIURL
|
|
|
|
|
2024-04-03 22:02:08 +08:00
|
|
|
// 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)
|
|
|
|
// })
|
2024-04-01 09:42:29 +08:00
|
|
|
|
|
|
|
fmt.Println("Server listening on port 5580...")
|
2024-04-03 21:53:57 +08:00
|
|
|
// APIURL 为 go-cqhttp 配置的事件上报地址
|
|
|
|
http.ListenAndServe(APIURL, nil)
|
2024-04-01 09:42:29 +08:00
|
|
|
}
|