修改pkg部分逻辑,增加测试方式,补充使用方式

This commit is contained in:
liyp 2024-04-02 20:29:25 +08:00
parent 48243b79d5
commit 1f0618c69d
5 changed files with 111 additions and 57 deletions

View file

@ -1 +1,28 @@
# 使用Go语言重新实现 [sihuan/XZZ](https://github.com/sihuan/XZZ) 机器人项目 # 使用Go语言重新实现 [sihuan/XZZ](https://github.com/sihuan/XZZ) 机器人项目
## 使用方式:
- 下载一个`go-cqhttp` 配置http模式修改为下面类似配置
```yaml
- http: # HTTP 通信设置
address: 0.0.0.0:5700 # HTTP监听地址
version: 11 # OneBot协议版本, 支持 11/12
timeout: 5 # 反向 HTTP 超时时间, 单位秒,<5 时将被忽略
long-polling: # 长轮询拓展
enabled: false # 是否开启
max-queue-size: 2000 # 消息队列大小0 表示不限制队列大小,谨慎使用
middlewares:
<<: *default # 引用默认中间件
post: # 反向HTTP POST地址列表
- url: http://0.0.0.0:5580 # 地址
```
- 配置事件过滤规则
```jaon
{
"message_type": "group",
"raw_message":{
".regex":"^!"
}
}
```
表示只接收以 ! 开头的指令,同时修改配置文件默认中间件锚点的事件过滤器文件目录 `filter: filter.json` ,还要修改上报数据类型为 `array`

2
go.mod
View file

@ -1,3 +1,3 @@
module go-bot module go-bot
go 1.22.1 go 1.21.0

View file

@ -1 +1,28 @@
package test package main
import (
"bufio"
"fmt"
"go-bot/workers"
"os"
"strings"
)
func main() {
for {
reader := bufio.NewReader(os.Stdin)
fmt.Print("输入指令:(不要带/)")
raw_msg, _ := reader.ReadString('\n')
// 去除末尾的换行符
raw_msg = strings.TrimRight(raw_msg, "\r\n")
parms := strings.Split(raw_msg, " ")
worker := workers.NewWorker(parms, "11", "111", "111", "222", raw_msg)
message := worker.GetMsg()
fmt.Println("message:", message)
}
}

View file

@ -30,12 +30,10 @@ func Router(data map[string]interface{}) {
// fmt.Println(worker.CheckPermission()) // fmt.Println(worker.CheckPermission())
// ans := NewStdAns(parms[0], fmt.Sprintf("%d", int(uid)), fmt.Sprintf("%d", int(gid)), role, fmt.Sprintf("%d", int(mid)), raw_msg) // ans := NewStdAns(parms[0], fmt.Sprintf("%d", int(uid)), fmt.Sprintf("%d", int(gid)), role, fmt.Sprintf("%d", int(mid)), raw_msg)
message := worker.CheckPermission() message := worker.CheckPermission()
if message != "0" { if message == "0" {
message = worker.GetMsg()
worker.SendMsg(message) worker.SendMsg(message)
} //else {
// message = worker.GetMsg() }
// worker.SendMsg(message)
//}
} }

View file

@ -14,6 +14,7 @@ type Pkg struct {
} }
func (a *Pkg) GetMsg() string { func (a *Pkg) GetMsg() string {
if len(a.Parms) < 2 { if len(a.Parms) < 2 {
return "请输入包名 如:/pkg linux 查询 linux 相关软件" return "请输入包名 如:/pkg linux 查询 linux 相关软件"
} }
@ -21,7 +22,8 @@ func (a *Pkg) GetMsg() string {
if len(a.Parms) > 2 { if len(a.Parms) > 2 {
url += "&repo=" + strings.ToUpper(a.Parms[2][:1]) + a.Parms[2][1:] url += "&repo=" + strings.ToUpper(a.Parms[2][:1]) + a.Parms[2][1:]
} }
fmt.Println("pkg url:", url) // 输出请求地址
// fmt.Println("pkg url:", url)
req, err := http.Get(url) req, err := http.Get(url)
if err != nil { if err != nil {
return err.Error() return err.Error()
@ -31,7 +33,7 @@ func (a *Pkg) GetMsg() string {
if err != nil { if err != nil {
return err.Error() return err.Error()
} }
fmt.Println("pkg body:", string(body)) // fmt.Println("pkg body:", string(body))
// var pkg []Package // var pkg []Package
var pkg map[string]interface{} var pkg map[string]interface{}
err = json.Unmarshal(body, &pkg) err = json.Unmarshal(body, &pkg)
@ -59,6 +61,7 @@ func (a *Pkg) GetMsg() string {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
if len(suggestions) > 0 {
searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name" searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name"
searchReq, err := http.Get(searchUrl) searchReq, err := http.Get(searchUrl)
if err != nil { if err != nil {
@ -92,14 +95,17 @@ func (a *Pkg) GetMsg() string {
fmt.Println(msg) fmt.Println(msg)
return msg return msg
} else {
return "没有找到相关软件"
}
} }
result := resultSlipe[0].(map[string]interface{}) result := resultSlipe[0].(map[string]interface{})
// 输出结果字符串
fmt.Println("result:", result) // fmt.Println("result:", result)
if result != nil { // if result != nil {
var msg string var msg string
// for _, v := range result[0] {
msg += "仓库:" + result["repo"].(string) + "\n" msg += "仓库:" + result["repo"].(string) + "\n"
msg += "包名:" + result["pkgname"].(string) + "\n" msg += "包名:" + result["pkgname"].(string) + "\n"
msg += "版本:" + result["pkgver"].(string) + "\n" msg += "版本:" + result["pkgver"].(string) + "\n"
@ -110,8 +116,4 @@ func (a *Pkg) GetMsg() string {
return msg return msg
} else {
// req, _ := http.Get("https://aur.tuna.tsinghua.edu.cn/rpc/?v=5&type=info&arg=" + a.parms[1])
return "没有找到相关软件"
}
} }