snowluma: SnowLuma (OneBot 11) Go SDK, drop-in compatible with napcat
This commit is contained in:
54
example_test.go
Normal file
54
example_test.go
Normal file
@ -0,0 +1,54 @@
|
||||
package snowluma_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
snowluma "gitea.doicat.com/bot/snowluma"
|
||||
)
|
||||
|
||||
// 完整示例:配置连接、注册事件与命令,阻塞运行直到收到中断信号。
|
||||
func Example() {
|
||||
bot := snowluma.New().
|
||||
SetToken("your-token").
|
||||
SetForwardWS("ws://127.0.0.1:3001")
|
||||
// 可叠加其他通信方式:
|
||||
// bot.SetReverseWS(":8080")
|
||||
// bot.SetHTTPAPI("http://127.0.0.1:3000")
|
||||
// bot.SetHTTPReport(":8081")
|
||||
|
||||
// 文本命令
|
||||
bot.OnCommand("/ping", func(c *snowluma.Context, args []string) {
|
||||
c.ReplyText("pong!")
|
||||
})
|
||||
|
||||
// 群里 @机器人 就复读
|
||||
bot.OnGroupMessage(func(c *snowluma.Context) {
|
||||
if c.HasAtMe() && !c.IsSelfMessage() {
|
||||
c.ReplyText("收到: " + c.PlainText())
|
||||
}
|
||||
})
|
||||
|
||||
// 进群欢迎
|
||||
bot.OnNoticeType("group_increase", func(c *snowluma.Context) {
|
||||
c.SendGroupMsg(nil, c.Event.Notice.GroupID, snowluma.Text("欢迎新人~"))
|
||||
})
|
||||
|
||||
// 被戳就戳回去
|
||||
bot.OnNoticeType("notify", func(c *snowluma.Context) {
|
||||
n := c.Event.Notice
|
||||
if n.SubType == "poke" && n.TargetID == c.SelfID() {
|
||||
c.SendPoke(nil, n.GroupID, n.UserID)
|
||||
}
|
||||
})
|
||||
|
||||
// 好友/群请求自动同意
|
||||
bot.OnRequest(func(c *snowluma.Context) {
|
||||
c.Approve()
|
||||
})
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer stop()
|
||||
bot.Run(ctx)
|
||||
}
|
||||
Reference in New Issue
Block a user