feat: 更新代码以支持使用Gin框架处理HTTP请求,重构加载新功能方式,使用反射动态加载,修改之前硬编码
This commit is contained in:
parent
b0bf290731
commit
c63b71b2a4
18 changed files with 307 additions and 122 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -1,6 +1,7 @@
|
|||
data.db
|
||||
test.json
|
||||
test*.json
|
||||
*.exe
|
||||
request.json
|
||||
config.toml
|
||||
messages.json
|
||||
messages.json
|
||||
.vscode/settings.json
|
||||
|
|
BIN
go-bot
BIN
go-bot
Binary file not shown.
30
go.mod
30
go.mod
|
@ -1,6 +1,6 @@
|
|||
module go-bot
|
||||
|
||||
go 1.22.2
|
||||
go 1.22.5
|
||||
|
||||
require github.com/BurntSushi/toml v1.3.2
|
||||
|
||||
|
@ -10,8 +10,36 @@ require (
|
|||
github.com/parnurzeal/gorequest v0.3.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytedance/sonic v1.11.6 // indirect
|
||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
golang.org/x/arch v0.8.0 // indirect
|
||||
golang.org/x/crypto v0.24.0 // indirect
|
||||
golang.org/x/sys v0.21.0 // indirect
|
||||
golang.org/x/text v0.16.0 // indirect
|
||||
google.golang.org/protobuf v1.34.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/elazarl/goproxy v0.0.0-20240618083138-03be62527ccb // indirect
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/moul/http2curl v1.0.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/smartystreets/goconvey v1.8.1 // indirect
|
||||
|
|
75
go.sum
75
go.sum
|
@ -1,26 +1,101 @@
|
|||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/elazarl/goproxy v0.0.0-20240618083138-03be62527ccb h1:2SoxRauy2IqekRMggrQk3yNI5X6omSnk6ugVbFywwXs=
|
||||
github.com/elazarl/goproxy v0.0.0-20240618083138-03be62527ccb/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
|
||||
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
||||
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
||||
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||
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/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
|
||||
github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
|
||||
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
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=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/moul/http2curl v1.0.0 h1:dRMWoAtb+ePxMlLkrCbAqh4TlPHXvoGUSQ323/9Zahs=
|
||||
github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ=
|
||||
github.com/parnurzeal/gorequest v0.3.0 h1:SoFyqCDC9COr1xuS6VA8fC8RU7XyrJZN2ona1kEX7FI=
|
||||
github.com/parnurzeal/gorequest v0.3.0/go.mod h1:3Kh2QUMJoqw3icWAecsyzkpY7UzRfDhbRdTjtNwNiUE=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
|
||||
github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=
|
||||
github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec=
|
||||
github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=
|
||||
github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
||||
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
|
||||
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
|
||||
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
|
||||
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
|
||||
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
|
||||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
||||
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
||||
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||
|
|
89
main.go
89
main.go
|
@ -10,24 +10,15 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"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)
|
||||
|
@ -47,7 +38,6 @@ func insertMessage(db *sql.DB, data map[string]interface{}) error {
|
|||
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!")
|
||||
|
@ -58,78 +48,77 @@ func insertMessage(db *sql.DB, data map[string]interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func handlePost(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
// handlePost 处理 POST 请求
|
||||
func handlePost(c *gin.Context) {
|
||||
// 从请求体中读取数据
|
||||
body, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
// 请求体数据读取失败
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Error reading request body"})
|
||||
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{} 类型的变量
|
||||
// 将 JSON 数据解析为 map 类型
|
||||
var data map[string]interface{}
|
||||
err = json.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
fmt.Println("Error decoding JSON:", err)
|
||||
// JSON 数据解析失败
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Error decoding JSON"})
|
||||
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,
|
||||
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!"))
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "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)
|
||||
// })
|
||||
r := gin.Default()
|
||||
r.POST("/", handlePost)
|
||||
|
||||
fmt.Println("Server listening on port 5580...")
|
||||
// APIURL 为 go-cqhttp 配置的事件上报地址
|
||||
http.ListenAndServe(APIURL, nil)
|
||||
fmt.Println("Server listening on", APIURL, "...")
|
||||
if err := r.Run(APIURL); err != nil {
|
||||
log.Fatal("服务器启动失败:", err)
|
||||
}
|
||||
}
|
||||
|
|
43
test/testimg.go
Normal file
43
test/testimg.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"go-bot/utils"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
img := utils.Image2Base64("/home/liyp/aa.jpg")
|
||||
// 保存Base64字符串到文件
|
||||
err := os.WriteFile("output_base64.txt", []byte(img), 0644)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to write base64 string to file: %s", err)
|
||||
}
|
||||
// 将base64转回图片
|
||||
// 去掉格式标志
|
||||
|
||||
parts := strings.Split(img, ",")
|
||||
if len(parts) != 2 {
|
||||
log.Fatalf("invalid data URI")
|
||||
}
|
||||
base64String := parts[1]
|
||||
|
||||
// 解码Base64字符串
|
||||
fileContent, err := base64.StdEncoding.DecodeString(base64String)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to decode base64 string: %s", err)
|
||||
}
|
||||
|
||||
// 将解码后的内容保存为图片文件
|
||||
err = os.WriteFile("output_image.jpg", fileContent, 0644)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to write image file: %s", err)
|
||||
}
|
||||
|
||||
fmt.Println("Image saved as output_image.jpg")
|
||||
|
||||
// println(img)
|
||||
}
|
21
utils/image2base64.go
Normal file
21
utils/image2base64.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Image2Base64(path string) string {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
if data, err := io.ReadAll(file); err == nil {
|
||||
return "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(data)
|
||||
}
|
||||
return ""
|
||||
|
||||
}
|
|
@ -7,11 +7,6 @@ import (
|
|||
)
|
||||
|
||||
func Router(data map[string]interface{}) {
|
||||
// 将map转换为格式化的JSON字符串
|
||||
// jsonString, err := json.MarshalIndent(data, "", " ")
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
|
||||
// 输出格式化后的JSON
|
||||
// fmt.Println(string(jsonString))
|
||||
|
|
|
@ -10,6 +10,14 @@ import (
|
|||
"github.com/parnurzeal/gorequest"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterWorkerFactory("ai", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
||||
return &AI{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type AI struct {
|
||||
*StdAns
|
||||
}
|
||||
|
|
|
@ -1,13 +1,18 @@
|
|||
package workers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go-bot/config"
|
||||
"net/http"
|
||||
|
||||
"github.com/parnurzeal/gorequest"
|
||||
)
|
||||
|
||||
type Worker interface {
|
||||
CheckPermission() string
|
||||
GetMsg() string
|
||||
SendMsg(msg string) bool
|
||||
}
|
||||
type StdAns struct {
|
||||
AllowGroup []interface{}
|
||||
AllowUser []interface{}
|
||||
|
@ -26,11 +31,34 @@ type StdAns struct {
|
|||
RawMsg string
|
||||
}
|
||||
|
||||
// 定义一个map类型的cfg,用于存储配置信息
|
||||
var cfg map[string]interface{}
|
||||
|
||||
// func init() {
|
||||
// cfg = config.GetConfig()
|
||||
// }
|
||||
// 定义一个WorkerFactory类型的函数,用于创建Worker
|
||||
type WorkerFactory func(parms []string, uid, gid, role, mid, rawMsg string) Worker
|
||||
|
||||
// 定义一个workerFactories的map,用于存储WorkerFactory
|
||||
var workerFactories = make(map[string]WorkerFactory)
|
||||
|
||||
// 定义一个RegisterWorkerFactory函数,用于注册WorkerFactory
|
||||
func RegisterWorkerFactory(name string, factory WorkerFactory) {
|
||||
workerFactories[name] = factory
|
||||
fmt.Printf("Register WorkerFactory: %s\n", name)
|
||||
}
|
||||
|
||||
// 定义一个NewWorker函数,用于创建Worker
|
||||
func NewWorker(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
||||
fmt.Printf("NewWorker: %s, %s, %s, %s, %s\n", parms, uid, gid, role, mid)
|
||||
if factory, ok := workerFactories[parms[0]]; ok {
|
||||
|
||||
fmt.Printf("Use WorkerFactory: %s\n", parms[0])
|
||||
return factory(parms, uid, gid, role, mid, rawMsg)
|
||||
}
|
||||
return &Emm{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
}
|
||||
|
||||
func NewStdAns(parms []string, uid, gid, role, mid, rawMsg string) *StdAns {
|
||||
// var cfg map[string]interface{}
|
||||
|
||||
|
@ -114,13 +142,17 @@ func (s *StdAns) SendMsg(msg string) bool {
|
|||
// println("core:", url)
|
||||
// fmt.Println("请求地址:", url)
|
||||
fmt.Println("响应信息:\n", msg)
|
||||
resp, err := http.Post(url, "application/json", bytes.NewBuffer(requestBody))
|
||||
if err != nil {
|
||||
request := gorequest.New()
|
||||
resp, body, errs := request.Post(url).
|
||||
Set("Content-Type", "application/json").
|
||||
Send(string(requestBody)).End()
|
||||
if len(errs) > 0 {
|
||||
|
||||
fmt.Println("Error sending request:", err)
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
// res, _ := io.ReadAll(resp.Body)
|
||||
// fmt.Println("响应返回:", string(res))
|
||||
fmt.Println("响应返回:", body)
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -1,5 +1,13 @@
|
|||
package workers
|
||||
|
||||
func init() {
|
||||
RegisterWorkerFactory("emm", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
||||
return &Emm{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type Emm struct {
|
||||
*StdAns
|
||||
}
|
||||
|
|
|
@ -9,6 +9,14 @@ import (
|
|||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterWorkerFactory("hhsh", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
||||
return &Hhsh{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type Hhsh struct {
|
||||
*StdAns
|
||||
}
|
||||
|
|
|
@ -12,6 +12,14 @@ import (
|
|||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterWorkerFactory("ip", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
||||
return &Ip{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type Ip struct {
|
||||
*StdAns
|
||||
}
|
||||
|
@ -36,16 +44,7 @@ func (a *Ip) GetMsg() string {
|
|||
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(json_data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("x-api-key", "d84eae04-0654-4173-bc6a-c457c4fc3f4d")
|
||||
// 打印请求信息
|
||||
// fmt.Println("请求方法:", req.Method)
|
||||
// fmt.Println("请求URL:", req.URL.String())
|
||||
// fmt.Println("请求头:")
|
||||
// for key, values := range req.Header {
|
||||
// for _, value := range values {
|
||||
// fmt.Printf(" %s: %s\n", key, value)
|
||||
// }
|
||||
// }
|
||||
// fmt.Println("请求体:", string(json_data))
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: 5 * time.Second, // 设置超时时间为5秒
|
||||
}
|
||||
|
|
|
@ -9,6 +9,14 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterWorkerFactory("lsp", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
||||
return &Lsp{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type Lsp struct {
|
||||
*StdAns
|
||||
}
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
package workers
|
||||
|
||||
import "fmt"
|
||||
|
||||
func NewWorker(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
||||
fmt.Println("NewWorker:", parms)
|
||||
switch parms[0] {
|
||||
|
||||
case "ping":
|
||||
return &Ping{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
// 在这里添加其他 worker 的 case
|
||||
case "pkg":
|
||||
return &Pkg{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
case "hhsh":
|
||||
return &Hhsh{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
case "ip":
|
||||
return &Ip{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
case "lsp":
|
||||
return &Lsp{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
case "ai":
|
||||
return &AI{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
default:
|
||||
return &Emm{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg)}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,13 @@
|
|||
package workers
|
||||
|
||||
func init() {
|
||||
RegisterWorkerFactory("ping", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
||||
return &Ping{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type Ping struct {
|
||||
*StdAns
|
||||
}
|
||||
|
|
|
@ -10,6 +10,14 @@ import (
|
|||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterWorkerFactory("pkg", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
||||
return &Pkg{
|
||||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type Pkg struct {
|
||||
*StdAns
|
||||
}
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
package workers
|
||||
|
||||
// Worker 接口描述了不同类的共同行为
|
||||
type Worker interface {
|
||||
CheckPermission() string
|
||||
GetMsg() string
|
||||
SendMsg(msg string) bool
|
||||
}
|
Loading…
Reference in a new issue