Skip to content

Commit 44755cb

Browse files
committed
docs: add coding guide for creating a chatbot with OneBot integration
1 parent 4801f2d commit 44755cb

5 files changed

Lines changed: 67 additions & 7 deletions

File tree

docs/.vitepress/config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default defineConfig({
1919
items: [
2020
{ text: '介绍', link: '/start/introduction' },
2121
{ text: '准备', link: '/start/start' },
22+
{ text: '写一个聊天机器人吧', link: '/start/coding' },
2223
]
2324
},
2425
{

docs/images/code1.png

93.3 KB
Loading

docs/other/onebot11.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
# OneBot 11 服务端
2-
## NapCat
3-
## LLoneBot
4-
## Lagrange.OneBot
5-
## AstralGocq
6-
## go-cqhttp
1+
# OneBot 11 实现
2+
## [NapCat](https://github.com/NapNeko/NapCatQQ)
3+
基于TypeScript构建的Bot框架,通过相应的启动器或者框架,主动调用QQ Node模块提供给客户端的接口,实现Bot的功能.
4+
## [LLoneBot](https://github.com/LLOneBot/LLOneBot)
5+
LiteLoaderQQNT 插件,实现 OneBot 11 和 Satori 协议,用于 QQ 机器人开发
6+
## [Lagrange.OneBot](https://github.com/LagrangeDev/Lagrange.Core)
7+
Lagrange.Core 实现了 OneBot V11 的通信协议, 可以和主流 Bot 框架进行通信
8+
## [AstralGocq](https://github.com/ProtocolScience/AstralGocq)
9+
基于 Mirai 以及 MiraiGo 的 OneBot Golang Android NT协议原生实现
10+
## [go-cqhttp (Lagrange.GO)](https://github.com/LagrangeDev/go-cqhttp)
11+
基于 Lagrange.Core 以及 LagrangeGo 的 OneBot Golang 原生实现

docs/start/coding.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 写一个聊天机器人吧
2+
3+
## 创建项目
4+
```bash
5+
mkdir nsxai
6+
cd ./nsxai
7+
go mod init nsxai
8+
```
9+
## 添加依赖
10+
```bash
11+
go get -u github.com/nsxdevx/nsxbot
12+
```
13+
## 连接到OneBot
14+
这里采用的是将OneBot实现作为WServer, 我们主动去连接.
15+
```go
16+
package main
17+
18+
import (
19+
"context"
20+
"time"
21+
22+
nsx "github.com/nsxdevx/nsxbot"
23+
"github.com/nsxdevx/nsxbot/driver"
24+
)
25+
26+
func main() {
27+
driver := driver.NewWSClient(1*time.Second, driver.WSnode{
28+
Url: "localhost:4000",
29+
})
30+
bot := nsx.Default(driver)
31+
bot.Run(context.Background())
32+
}
33+
```
34+
运行,你应该在控制台看到如下输出
35+
![](../images/code1.png)
36+
## 测试消息交互
37+
```go
38+
func main() {
39+
driver := driver.NewWSClient(1*time.Second, driver.WSnode{
40+
Url: "localhost:4000",
41+
})
42+
bot := nsx.Default(driver)
43+
pvt := nsx.OnEvent[types.EventPvtMsg](bot)
44+
45+
adminuin, _ := strconv.ParseInt(os.Getenv("ADMIN_UIN"), 10, 64)
46+
pvt.Handle(func(ctx *nsx.Context[types.EventPvtMsg]) {
47+
ctx.Msg.Reply(ctx, "测试")
48+
}, filter.OnlyUsers(adminuin))
49+
50+
bot.Run(context.Background())
51+
}
52+
```
53+
现在尝试使用admin的qq发送一条消息到你的Bot,应该会收到“测试”回复

docs/start/start.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# 准备
22
- NsxBot由 [Golang](https://go.dev/) 实现,需要安装golang环境,推荐版本>=1.24.x。
33
- NsxBot 上手非常简单,并且,[Golang](https://go.dev/) 也是非常易上手的一门编程语言,所以即使你没有使用过 [Golang](https://go.dev/),依然能够快速开发。
4-
- NsxBot 只是一个 [OneBot 11](https://github.com/botuniverse/onebot-11) 开发框架,你需要准备 [OneBot 11 实现](/other/onebot11)
4+
- NsxBot 只是一个 [OneBot 11](https://github.com/botuniverse/onebot-11) 开发框架,你需要准备 [OneBot 11 实现](/other/onebot11)
5+
- 阅读 OneBot 11 协议,了解 [OneBot 11](https://github.com/botuniverse/onebot-11) 的基本要求和设计。

0 commit comments

Comments
 (0)