-
Notifications
You must be signed in to change notification settings - Fork 4
/
bot.go
74 lines (68 loc) · 1.55 KB
/
bot.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Package rei ReiBot created in 2022.5.31
package rei
import (
tgba "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
// Bot bot 的配置
type Bot struct {
// Token bot 的 token
// see https://core.telegram.org/bots#3-how-do-i-create-a-bot
Token string
// Buffer 控制消息队列的长度
Buffer int
// UpdateConfig 配置消息获取
tgba.UpdateConfig
// SuperUsers 超级用户
SuperUsers []int64
// Debug 控制调试信息的输出与否
Debug bool
// Handler 注册对各种事件的处理
Handler *Handler
// handlers 方便调用的 handler
handlers map[string]GeneralHandleType
}
// Start clients without blocking
func Start(bots ...Bot) {
for _, c := range bots {
tc := NewTelegramClient(&c)
tc.Connect()
go tc.Listen()
}
}
// Run clients and block self in listening last one
func Run(bots ...Bot) {
var tc TelegramClient
switch len(bots) {
case 0:
return
case 1:
c := bots[0]
tc = NewTelegramClient(&c)
default:
for _, c := range bots[:len(bots)-1] {
tc := NewTelegramClient(&c)
tc.Connect()
go tc.Listen()
}
c := bots[len(bots)-1]
tc = NewTelegramClient(&c)
}
tc.Connect()
tc.Listen()
}
// GetBot 获取指定的bot (Ctx) 实例
func GetBot(id int64) *Ctx {
caller, ok := clients.Load(id)
if !ok {
return nil
}
return &Ctx{Caller: caller}
}
// RangeBot 遍历所有bot (Ctx)实例
//
// 单次操作返回 true 则继续遍历,否则退出
func RangeBot(iter func(id int64, ctx *Ctx) bool) {
clients.Range(func(key int64, value *TelegramClient) bool {
return iter(key, &Ctx{Caller: value})
})
}