mirror of
https://github.com/awfufu/qbot.git
synced 2026-03-01 05:19:44 +08:00
chore: update README.md
This commit is contained in:
47
README.md
47
README.md
@@ -1,15 +1,15 @@
|
||||
# qbot
|
||||
|
||||
使用双向 HTTP 连接与 NapCat 通信。
|
||||
使用 Golang 实现的 NapCat QQ 机器人框架,通过双向 HTTP 连接与 NapCat 通信。
|
||||
|
||||
## example
|
||||
|
||||
NapCat 配置:
|
||||
|
||||
- 设置 HTTP 服务端,监听 `http://napcat-ip:3000`
|
||||
- 设置 HTTP 客户端,地址 `http://qbot-ip:3001`
|
||||
- NapCat HTTP 服务端,对应 `qbot.HttpClient`
|
||||
- NapCat HTTP 客户端,对应 `qbot.HttpServer`
|
||||
|
||||
*(可指定任意端口,保证二者之间能通信即可。)*
|
||||
> 可指定任意端口,保证二者之间能通信即可。
|
||||
|
||||
下面是一个 echo 示例。
|
||||
|
||||
@@ -18,34 +18,49 @@ package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/awfufu/qbot"
|
||||
)
|
||||
|
||||
func main() {
|
||||
bot := qbot.NewBot("qbot-ip:3001") // 填写 NapCat 的 HTTP 客户端地址
|
||||
bot.ConnectNapcat("http://napcat-ip:3000") // 填写 NapCat 的 HTTP 服务端 URL
|
||||
bot.OnMessage(func(b *qbot.Bot, msg *qbot.Message) {
|
||||
if msg.Raw == "hello" {
|
||||
b.SendGroupMsg(msg.GroupID,
|
||||
qbot.At(msg.UserID), "world")
|
||||
receiver := qbot.HttpServer(":3002")
|
||||
sender := qbot.HttpClient("http://napcat:3000")
|
||||
|
||||
for {
|
||||
select {
|
||||
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
|
||||
(you) > [hello]
|
||||
(bot) < [world]
|
||||
(you) > /echo helloworld
|
||||
(bot) < helloworld
|
||||
```
|
||||
|
||||
## run
|
||||
|
||||
```sh
|
||||
go mod init yourproject
|
||||
go get github.com/awfufu/qbot@v0.1.0
|
||||
go mod tidy
|
||||
# edit main.go
|
||||
go get github.com/awfufu/qbot
|
||||
go mod tidy
|
||||
go run main.go
|
||||
```
|
||||
Reference in New Issue
Block a user