-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.go
More file actions
290 lines (234 loc) · 7.86 KB
/
Copy pathbot.go
File metadata and controls
290 lines (234 loc) · 7.86 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package botapi
import (
"context"
"strings"
"sync"
"github.com/go-faster/errors"
"github.com/gotd/log"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/message"
"github.com/gotd/td/telegram/peers"
"github.com/gotd/td/telegram/updates"
"github.com/gotd/td/tg"
)
// Bot is a Telegram Bot API client implemented over MTProto.
//
// Construct it with New, register update handlers on its Dispatcher, then call
// Run to connect and serve. Bot is safe for concurrent use once running.
type Bot struct {
token string
log log.Logger
client *telegram.Client
raw *tg.Client
sender *message.Sender
peers *peers.Manager
gaps *updates.Manager
disp tg.UpdateDispatcher
router router
onStart func(ctx context.Context)
// commands collects the (command, description) pairs registered through
// OnCommand so Run can publish them to Telegram. registerCommands gates the
// publish.
commandsMu sync.Mutex
commands []BotCommand
registerCommands bool
// runMu guards runCtx, the bot's run-lifetime context, used for background
// (proactive) sends that must outlive a single update handler.
runMu sync.Mutex
runCtx context.Context
self *tg.User
}
// New constructs an unconnected Bot from a BotFather token. It performs no
// network I/O; call Run to connect, authorize and serve updates.
func New(token string, opt Options) (*Bot, error) {
if token == "" {
return nil, errors.New("token is required")
}
if opt.AppID == 0 || opt.AppHash == "" {
return nil, errors.New("AppID and AppHash are required (see https://my.telegram.org)")
}
opt.setDefaults()
lg := opt.Logger
disp := tg.NewUpdateDispatcher()
// peers.Manager needs a *tg.Client, but the real one only exists after
// telegram.NewClient, which in turn needs the update handler built from the
// manager. Break the cycle with a placeholder we backfill below (same trick
// the gotd examples use).
rawPlaceholder := new(tg.Client)
pm := peers.Options{
Storage: opt.Storage,
Cache: opt.Storage,
Logger: log.Named(lg, "peers"),
}.Build(rawPlaceholder)
gaps := updates.New(updates.Config{
Handler: disp,
Storage: opt.Storage,
AccessHasher: pm,
Logger: log.Named(lg, "gaps"),
OnChannelTooLong: func(channelID int64) {
log.For(lg).Warn(context.Background(), "Channel too long", log.Int64("channel_id", channelID))
},
})
// Harvest access hashes from every update, then feed gap recovery.
h := pm.UpdateHook(gaps)
client := telegram.NewClient(opt.AppID, opt.AppHash, telegram.Options{
Logger: log.Named(lg, "client"),
Device: opt.Device,
UpdateHandler: h,
SessionStorage: opt.Storage,
Middlewares: buildMiddlewares(opt, h),
Resolver: opt.resolver,
PublicKeys: opt.publicKeys,
DCList: opt.dcList,
})
*rawPlaceholder = *client.API()
b := &Bot{
token: token,
log: opt.Logger,
client: client,
raw: client.API(),
sender: message.NewSender(client.API()),
peers: pm,
gaps: gaps,
disp: disp,
onStart: opt.OnStart,
registerCommands: !opt.DisableCommandRegistration,
}
b.installHandlers()
return b, nil
}
// registerCommand records a command registered through OnCommand so Run can
// publish the set to Telegram. Duplicate names keep their first description.
func (b *Bot) registerCommand(name, description string) {
name = strings.TrimPrefix(name, "/")
if name == "" {
return
}
b.commandsMu.Lock()
defer b.commandsMu.Unlock()
for _, c := range b.commands {
if c.Command == name {
return
}
}
b.commands = append(b.commands, BotCommand{Command: name, Description: description})
}
// publishCommands reports the collected OnCommand set to Telegram (default
// scope). Failures are logged, not fatal, since a bad description should not
// stop the bot from serving.
func (b *Bot) publishCommands(ctx context.Context) {
if !b.registerCommands {
return
}
b.commandsMu.Lock()
cmds := append([]BotCommand(nil), b.commands...)
b.commandsMu.Unlock()
if len(cmds) == 0 {
return
}
if err := b.SetMyCommands(ctx, cmds); err != nil {
b.logger().Warn(ctx, "Register bot commands", log.Error(err))
return
}
b.logger().Debug(ctx, "Registered bot commands", log.Int("count", len(cmds)))
}
// Run connects, authorizes as a bot, and blocks serving updates until ctx is
// canceled or a fatal error occurs. Register handlers before calling Run.
func (b *Bot) Run(ctx context.Context) error {
return b.client.Run(ctx, func(ctx context.Context) error {
// Expose the connection-lifetime context for background sends, and clear
// it on shutdown so background work stops with the bot.
b.setRunCtx(ctx)
defer b.setRunCtx(nil)
status, err := b.client.Auth().Status(ctx)
if err != nil {
return errors.Wrap(err, "auth status")
}
if !status.Authorized {
if _, err := b.client.Auth().Bot(ctx, b.token); err != nil {
return errors.Wrap(err, "bot login")
}
}
me, err := b.client.Self(ctx)
if err != nil {
return errors.Wrap(err, "get self")
}
if !me.Bot {
return errors.New("authorized account is not a bot")
}
b.self = me
if err := b.peers.Init(ctx); err != nil {
return errors.Wrap(err, "init peers")
}
return b.gaps.Run(ctx, b.raw, me.ID, updates.AuthOptions{
IsBot: true,
OnStart: func(ctx context.Context) {
b.logger().Info(ctx, "Bot started",
log.Int64("id", me.ID),
log.String("username", me.Username),
)
b.publishCommands(ctx)
if b.onStart != nil {
b.onStart(ctx)
}
},
})
})
}
// Raw returns the underlying gotd/td API client for direct MTProto calls. It is
// the escape hatch for anything the Bot API surface does not (yet) cover.
func (b *Bot) Raw() *tg.Client { return b.raw }
// Dispatcher returns the update dispatcher for registering raw MTProto update
// handlers. The typed Bot API handler framework is built on top of this and
// will be added in a later phase.
func (b *Bot) Dispatcher() *tg.UpdateDispatcher { return &b.disp }
// Sender returns the message sender used for outgoing messages.
func (b *Bot) Sender() *message.Sender { return b.sender }
// Peers returns the peer manager (resolution and access-hash storage).
func (b *Bot) Peers() *peers.Manager { return b.peers }
// Self returns the bot's own user. It is nil until Run has authorized.
func (b *Bot) Self() *tg.User { return b.self }
// Logger returns the bot's structured logger (the github.com/gotd/log port).
func (b *Bot) Logger() log.Logger { return b.log }
// logger wraps the bot's logger in an ergonomic Helper for internal logging.
func (b *Bot) logger() log.Helper { return log.For(b.log) }
func (b *Bot) setRunCtx(ctx context.Context) {
b.runMu.Lock()
b.runCtx = ctx
b.runMu.Unlock()
}
// Background returns a context tied to the bot's run lifetime, for proactive or
// background sends that are not in response to an update — e.g. from a timer,
// queue or goroutine. It is canceled when the bot stops.
//
// A handler's own context is per-update (and may carry a Timeout deadline), so
// it must not be used for work that outlives the handler. Use Background
// instead:
//
// bot.OnCommand("remind", "Remind in a minute", func(c *botapi.Context) error {
// chat, _ := c.Chat()
// ctx := c.Bot.Background()
// go func() {
// time.Sleep(time.Minute)
// c.Bot.SendMessage(ctx, chat, "⏰ reminder")
// }()
// return nil
// })
//
// Before Run has connected (or after it has stopped) Background returns an
// already-canceled context, so background sends fail fast rather than block.
func (b *Bot) Background() context.Context {
b.runMu.Lock()
ctx := b.runCtx
b.runMu.Unlock()
if ctx == nil {
return canceledContext
}
return ctx
}
// canceledContext is returned by Background before the bot is running.
var canceledContext = func() context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
}()