go-bot/main.go
liyp 8e4ad23278 feat(test): 添加测试数据生成和API请求功能
- test.go中添加了生成测试数据的逻辑,并使用gorequest库发起了HTTP POST请求。
- main.go中调整了插入消息的条件,现在包括'message_sent'类型。
- .gitignore中新增了请求数据的忽略项request.json。
- go.mod中新增了gorequest和goconvey库的依赖。

此功能使得测试更加自动化,能够更方便地进行数据的模拟和测试。
2024-07-02 18:16:26 +08:00

135 lines
3.7 KiB
Go

package main
import (
"database/sql"
"fmt"
"go-bot/config"
"go-bot/utils"
"io"
"log"
"net/http"
"time"
"github.com/goccy/go-json"
_ "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" || data["post_type"] == "message_sent" {
// 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)
message_seq := data["message_seq"].(float64)
fmt.Println(post_type, message_time, int64(group_id), int64(message_id), raw_message, sender_user_id, sender_nickname, sender_card, sender_role, int64(message_seq))
// 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, 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)
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 UNIQUE NOT NULL,
raw_message TEXT,
sender_user_id INTEGER,
sender_nickname TEXT,
sender_card TEXT,
sender_role TEXT,
message_seq INTEGER
)`)
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() {
cfg := config.GetConfig()
APIURL, ok := cfg["APIURL"].(string)
if !ok {
log.Fatal("加载配置失败!")
}
// config.PrintConfig(cfg, "")
// print(cfg["AllowGroup"].([]interface{})[0].(string))
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)
}