diff --git a/README.md b/README.md index 3c29f79..9bd83e1 100644 --- a/README.md +++ b/README.md @@ -1 +1,28 @@ # 使用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` 。 diff --git a/go.mod b/go.mod index f122a12..08a12ea 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module go-bot -go 1.22.1 +go 1.21.0 diff --git a/test/test.go b/test/test.go index 56e5404..7c95e11 100644 --- a/test/test.go +++ b/test/test.go @@ -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) + + } +} diff --git a/utils/router.go b/utils/router.go index 330f165..21b3316 100644 --- a/utils/router.go +++ b/utils/router.go @@ -30,12 +30,10 @@ func Router(data map[string]interface{}) { // 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) message := worker.CheckPermission() - if message != "0" { + if message == "0" { + message = worker.GetMsg() worker.SendMsg(message) - } //else { - // message = worker.GetMsg() - // worker.SendMsg(message) - //} + } } diff --git a/workers/pkg.go b/workers/pkg.go index 7b1fe15..6f27c8e 100644 --- a/workers/pkg.go +++ b/workers/pkg.go @@ -14,6 +14,7 @@ type Pkg struct { } func (a *Pkg) GetMsg() string { + if len(a.Parms) < 2 { return "请输入包名 如:/pkg linux 查询 linux 相关软件" } @@ -21,7 +22,8 @@ func (a *Pkg) GetMsg() string { if len(a.Parms) > 2 { 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) if err != nil { return err.Error() @@ -31,7 +33,7 @@ func (a *Pkg) GetMsg() string { if err != nil { return err.Error() } - fmt.Println("pkg body:", string(body)) + // fmt.Println("pkg body:", string(body)) // var pkg []Package var pkg map[string]interface{} err = json.Unmarshal(body, &pkg) @@ -59,59 +61,59 @@ func (a *Pkg) GetMsg() string { if err != nil { fmt.Println(err) } - searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name" - searchReq, err := http.Get(searchUrl) - if err != nil { - fmt.Println(err) - } - defer searchReq.Body.Close() - searchRe, err := io.ReadAll(searchReq.Body) - if err != nil { - fmt.Println(err) - } - var searchMap map[string]interface{} - err = json.Unmarshal(searchRe, &searchMap) - if err != nil { - fmt.Println(err) - } - searchResults := searchMap["results"].([]interface{}) - searchResult := searchResults[0].(map[string]interface{}) - maintainer, ok := searchResult["Maintainer"].(string) - if !ok { - maintainer = "孤儿包" - } - last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05") - var msg string - msg += "仓库:AUR\n" - msg += "包名:" + searchResult["Name"].(string) + "\n" - msg += "版本:" + searchResult["Version"].(string) + "\n" - msg += "描述:" + searchResult["Description"].(string) + "\n" - msg += "维护者:" + maintainer + "\n" - msg += "链接:" + searchResult["URL"].(string) + "\n" - msg += "更新时间:" + last_update + "\n" + if len(suggestions) > 0 { + searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name" + searchReq, err := http.Get(searchUrl) + if err != nil { + fmt.Println(err) + } + defer searchReq.Body.Close() + searchRe, err := io.ReadAll(searchReq.Body) + if err != nil { + fmt.Println(err) + } + var searchMap map[string]interface{} + err = json.Unmarshal(searchRe, &searchMap) + if err != nil { + fmt.Println(err) + } + searchResults := searchMap["results"].([]interface{}) + searchResult := searchResults[0].(map[string]interface{}) + maintainer, ok := searchResult["Maintainer"].(string) + if !ok { + maintainer = "孤儿包" + } + last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05") + var msg string + msg += "仓库:AUR\n" + msg += "包名:" + searchResult["Name"].(string) + "\n" + msg += "版本:" + searchResult["Version"].(string) + "\n" + msg += "描述:" + searchResult["Description"].(string) + "\n" + msg += "维护者:" + maintainer + "\n" + msg += "链接:" + searchResult["URL"].(string) + "\n" + msg += "更新时间:" + last_update + "\n" - fmt.Println(msg) - return msg + fmt.Println(msg) + return msg + } else { + return "没有找到相关软件" + } } result := resultSlipe[0].(map[string]interface{}) + // 输出结果字符串 + // fmt.Println("result:", result) + // if result != nil { + var msg string - fmt.Println("result:", result) - if result != nil { - var msg string - // for _, v := range result[0] { - msg += "仓库:" + result["repo"].(string) + "\n" - msg += "包名:" + result["pkgname"].(string) + "\n" - msg += "版本:" + result["pkgver"].(string) + "\n" - msg += "描述:" + result["pkgdesc"].(string) + "\n" - msg += "打包:" + result["packager"].(string) + "\n" - msg += "链接:" + result["url"].(string) + "\n" - msg += "更新日期:" + result["last_update"].(string) + "\n" + msg += "仓库:" + result["repo"].(string) + "\n" + msg += "包名:" + result["pkgname"].(string) + "\n" + msg += "版本:" + result["pkgver"].(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 "没有找到相关软件" - } }