-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot_test.go
37 lines (33 loc) · 965 Bytes
/
bot_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package test
import (
"fmt"
"net/http"
"testing"
"github.com/ProtobufBot/go-pbbot"
"github.com/ProtobufBot/go-pbbot/proto_gen/onebot"
)
func TestBotServer(t *testing.T) {
pbbot.HandleConnect = func(bot *pbbot.Bot) {
fmt.Printf("新机器人已连接:%d\n", bot.BotId)
fmt.Println("所有机器人列表:")
for botId, _ := range pbbot.Bots {
println(botId)
}
}
pbbot.HandleGroupMessage = func(bot *pbbot.Bot, event *onebot.GroupMessageEvent) {
rawMsg := event.RawMessage
groupId := event.GroupId
userId := event.UserId
replyMsg := pbbot.NewMsg().Text("hello world").At(userId).Text("你发送了:" + rawMsg)
_, _ = bot.SendGroupMessage(groupId, replyMsg, false)
}
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
if err := pbbot.UpgradeWebsocket(w, req); err != nil {
fmt.Println("创建机器人失败")
}
})
if err := http.ListenAndServe(":8081", nil); err != nil {
panic(err)
}
select {}
}