测试保存聊天记录

This commit is contained in:
liyp 2024-04-20 15:11:49 +08:00
parent ca02f5fcf1
commit 9f2e3f096d
3 changed files with 84 additions and 1 deletions

8
go.mod
View file

@ -4,4 +4,10 @@ go 1.21.0
require github.com/BurntSushi/toml v1.3.2
require github.com/goccy/go-json v0.10.2 // indirect
require golang.org/x/net v0.17.0 // indirect
require (
github.com/goccy/go-json v0.10.2 // indirect
github.com/gorilla/websocket v1.5.1
github.com/mattn/go-sqlite3 v1.14.22
)

6
go.sum
View file

@ -2,3 +2,9 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=

71
main.go
View file

@ -1,14 +1,55 @@
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)
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
}
func handlePost(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
@ -28,6 +69,36 @@ func handlePost(w http.ResponseWriter, r *http.Request) {
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!"))