修改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,59 +61,59 @@ func (a *Pkg) GetMsg() string {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name" if len(suggestions) > 0 {
searchReq, err := http.Get(searchUrl) searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name"
if err != nil { searchReq, err := http.Get(searchUrl)
fmt.Println(err) if err != nil {
} fmt.Println(err)
defer searchReq.Body.Close() }
searchRe, err := io.ReadAll(searchReq.Body) defer searchReq.Body.Close()
if err != nil { searchRe, err := io.ReadAll(searchReq.Body)
fmt.Println(err) if err != nil {
} fmt.Println(err)
var searchMap map[string]interface{} }
err = json.Unmarshal(searchRe, &searchMap) var searchMap map[string]interface{}
if err != nil { err = json.Unmarshal(searchRe, &searchMap)
fmt.Println(err) if err != nil {
} fmt.Println(err)
searchResults := searchMap["results"].([]interface{}) }
searchResult := searchResults[0].(map[string]interface{}) searchResults := searchMap["results"].([]interface{})
maintainer, ok := searchResult["Maintainer"].(string) searchResult := searchResults[0].(map[string]interface{})
if !ok { maintainer, ok := searchResult["Maintainer"].(string)
maintainer = "孤儿包" if !ok {
} maintainer = "孤儿包"
last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05") }
var msg string last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05")
msg += "仓库AUR\n" var msg string
msg += "包名:" + searchResult["Name"].(string) + "\n" msg += "仓库AUR\n"
msg += "版本:" + searchResult["Version"].(string) + "\n" msg += "包名:" + searchResult["Name"].(string) + "\n"
msg += "描述:" + searchResult["Description"].(string) + "\n" msg += "版本:" + searchResult["Version"].(string) + "\n"
msg += "维护者:" + maintainer + "\n" msg += "描述:" + searchResult["Description"].(string) + "\n"
msg += "链接:" + searchResult["URL"].(string) + "\n" msg += "维护者:" + maintainer + "\n"
msg += "更新时间:" + last_update + "\n" msg += "链接:" + searchResult["URL"].(string) + "\n"
msg += "更新时间:" + last_update + "\n"
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)
// if result != nil {
var msg string
fmt.Println("result:", result) msg += "仓库:" + result["repo"].(string) + "\n"
if result != nil { msg += "包名:" + result["pkgname"].(string) + "\n"
var msg string msg += "版本:" + result["pkgver"].(string) + "\n"
// for _, v := range result[0] { msg += "描述:" + result["pkgdesc"].(string) + "\n"
msg += "仓库:" + result["repo"].(string) + "\n" msg += "打包:" + result["packager"].(string) + "\n"
msg += "包名:" + result["pkgname"].(string) + "\n" msg += "链接:" + result["url"].(string) + "\n"
msg += "版本:" + result["pkgver"].(string) + "\n" msg += "更新日期:" + result["last_update"].(string) + "\n"
msg += "描述:" + result["pkgdesc"].(string) + "\n"
msg += "打包:" + result["packager"].(string) + "\n"
msg += "链接:" + result["url"].(string) + "\n"
msg += "更新日期:" + result["last_update"].(string) + "\n"
return msg return msg
} else {
// req, _ := http.Get("https://aur.tuna.tsinghua.edu.cn/rpc/?v=5&type=info&arg=" + a.parms[1])
return "没有找到相关软件"
}
} }