mirror of
https://github.com/awfufu/qbot.git
synced 2026-03-01 13:29:43 +08:00
chore: update README.md
This commit is contained in:
47
README.md
47
README.md
@@ -1,15 +1,15 @@
|
|||||||
# qbot
|
# qbot
|
||||||
|
|
||||||
使用双向 HTTP 连接与 NapCat 通信。
|
使用 Golang 实现的 NapCat QQ 机器人框架,通过双向 HTTP 连接与 NapCat 通信。
|
||||||
|
|
||||||
## example
|
## example
|
||||||
|
|
||||||
NapCat 配置:
|
NapCat 配置:
|
||||||
|
|
||||||
- 设置 HTTP 服务端,监听 `http://napcat-ip:3000`
|
- NapCat HTTP 服务端,对应 `qbot.HttpClient`
|
||||||
- 设置 HTTP 客户端,地址 `http://qbot-ip:3001`
|
- NapCat HTTP 客户端,对应 `qbot.HttpServer`
|
||||||
|
|
||||||
*(可指定任意端口,保证二者之间能通信即可。)*
|
> 可指定任意端口,保证二者之间能通信即可。
|
||||||
|
|
||||||
下面是一个 echo 示例。
|
下面是一个 echo 示例。
|
||||||
|
|
||||||
@@ -18,34 +18,49 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/awfufu/qbot"
|
"github.com/awfufu/qbot"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
bot := qbot.NewBot("qbot-ip:3001") // 填写 NapCat 的 HTTP 客户端地址
|
receiver := qbot.HttpServer(":3002")
|
||||||
bot.ConnectNapcat("http://napcat-ip:3000") // 填写 NapCat 的 HTTP 服务端 URL
|
sender := qbot.HttpClient("http://napcat:3000")
|
||||||
bot.OnMessage(func(b *qbot.Bot, msg *qbot.Message) {
|
|
||||||
if msg.Raw == "hello" {
|
for {
|
||||||
b.SendGroupMsg(msg.GroupID,
|
select {
|
||||||
qbot.At(msg.UserID), "world")
|
case msg := <-receiver.OnMessage():
|
||||||
|
if msg.ChatType != qbot.Group {
|
||||||
|
continue // Not a group message
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(msg.Array) > 0 && msg.Array[0].Type() == qbot.TextType {
|
||||||
|
if after, ok := strings.CutPrefix(msg.Array[0].GetTextItem(), "/echo "); ok {
|
||||||
|
sender.SendGroupMsg(msg.GroupID, after, msg.Array[1:])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case err := <-receiver.Error():
|
||||||
|
// Handle HTTP server error
|
||||||
|
log.Printf("http server error: %v", err)
|
||||||
|
receiver.Close()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
log.Fatal(bot.Run())
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```text
|
```text
|
||||||
(you) > [hello]
|
(you) > /echo helloworld
|
||||||
(bot) < [world]
|
(bot) < helloworld
|
||||||
```
|
```
|
||||||
|
|
||||||
## run
|
## run
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
go mod init yourproject
|
go mod init yourproject
|
||||||
go get github.com/awfufu/qbot@v0.1.0
|
|
||||||
go mod tidy
|
|
||||||
# edit main.go
|
# edit main.go
|
||||||
|
go get github.com/awfufu/qbot
|
||||||
|
go mod tidy
|
||||||
go run main.go
|
go run main.go
|
||||||
```
|
```
|
||||||
Reference in New Issue
Block a user