-
Notifications
You must be signed in to change notification settings - Fork 41
/
client.go
484 lines (429 loc) · 13.6 KB
/
client.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package gotgproto
//go:generate go run ./generator
import (
"context"
"fmt"
"runtime"
"sync"
"time"
"github.com/gotd/td/session"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/auth"
"github.com/gotd/td/telegram/dcs"
"github.com/gotd/td/telegram/message"
"github.com/gotd/td/tg"
"go.uber.org/zap"
"github.com/celestix/gotgproto/dispatcher"
intErrors "github.com/celestix/gotgproto/errors"
"github.com/celestix/gotgproto/ext"
"github.com/celestix/gotgproto/functions"
"github.com/celestix/gotgproto/sessionMaker"
"github.com/celestix/gotgproto/storage"
)
const VERSION = "v1.0.0-beta19"
type Client struct {
// Dispatcher handlers the incoming updates and execute mapped handlers. It is recommended to use dispatcher.MakeDispatcher function for this field.
Dispatcher dispatcher.Dispatcher
// PublicKeys of telegram.
//
// If not provided, embedded public keys will be used.
PublicKeys []telegram.PublicKey
// DC ID to connect.
//
// If not provided, 2 will be used by default.
DC int
// DCList is initial list of addresses to connect.
DCList dcs.List
// Resolver to use.
Resolver dcs.Resolver
// MigrationTimeout configures migration timeout.
MigrationTimeout time.Duration
// AckBatchSize is limit of MTProto ACK buffer size.
AckBatchSize int
// AckInterval is maximum time to buffer MTProto ACK.
AckInterval time.Duration
// RetryInterval is duration between send retries.
RetryInterval time.Duration
// MaxRetries is limit of send retries.
MaxRetries int
// ExchangeTimeout is timeout of every key exchange request.
ExchangeTimeout time.Duration
// DialTimeout is timeout of creating connection.
DialTimeout time.Duration
// CompressThreshold is a threshold in bytes to determine that message
// is large enough to be compressed using GZIP.
// If < 0, compression will be disabled.
// If == 0, default value will be used.
CompressThreshold int
// Whether to show the copyright line in console or no.
DisableCopyright bool
// Logger is instance of zap.Logger. No logs by default.
Logger *zap.Logger
// Session info of the authenticated user, use sessionMaker.NewSession function to fill this field.
sessionStorage session.Storage
// Self contains details of logged in user in the form of *tg.User.
Self *tg.User
// Code for the language used on the device's OS, ISO 639-1 standard.
SystemLangCode string
// Code for the language used on the client, ISO 639-1 standard.
ClientLangCode string
// PeerStorage is the storage for all the peers.
// It is recommended to use storage.NewPeerStorage function for this field.
PeerStorage *storage.PeerStorage
// NoAutoAuth is a flag to disable automatic authentication
// if the current session is invalid.
NoAutoAuth bool
*telegram.Client
authConversator AuthConversator
clientType clientType
ctx context.Context
err error
autoFetchReply bool
cancel context.CancelFunc
running bool
appId int
apiHash string
opts *ClientOpts
}
type ClientOpts struct {
// Logger is instance of zap.Logger. No logs by default.
Logger *zap.Logger
// Whether to store session and peer storage in memory or not
//
// Note: Sessions and Peers won't be persistent if this field is set to true.
InMemory bool
// PublicKeys of telegram.
//
// If not provided, embedded public keys will be used.
PublicKeys []telegram.PublicKey
// DC ID to connect.
//
// If not provided, 2 will be used by default.
DC int
// DCList is initial list of addresses to connect.
DCList dcs.List
// Resolver to use.
Resolver dcs.Resolver
// Whether to show the copyright line in console or no.
DisableCopyright bool
// Session info of the authenticated user, use sessionMaker.NewSession function to fill this field.
Session sessionMaker.SessionConstructor
// StorageConfig is the configuration for the storage.
StorageConfig *storage.StorageConfig
// Setting this field to true will lead to automatically fetch the reply_to_message for a new message update.
//
// Set to `false` by default.
AutoFetchReply bool
// Setting this field to true will lead to automatically fetch the entire reply_to_message chain for a new message update.
//
// Set to `false` by default.
FetchEntireReplyChain bool
// Code for the language used on the device's OS, ISO 639-1 standard.
SystemLangCode string
// Code for the language used on the client, ISO 639-1 standard.
ClientLangCode string
// Custom client device
Device *telegram.DeviceConfig
// Panic handles all the panics that occur during handler execution.
PanicHandler dispatcher.PanicHandler
// Error handles all the unknown errors which are returned by the handler callback functions.
ErrorHandler dispatcher.ErrorHandler
// Custom Middlewares
Middlewares []telegram.Middleware
// Custom Run() Middleware
// Can be used for floodWaiter package
// https://github.com/celestix/gotgproto/blob/beta/examples/middleware/main.go#L41
RunMiddleware func(
origRun func(ctx context.Context, f func(ctx context.Context) error) (err error),
ctx context.Context,
f func(ctx context.Context) (err error),
) (err error)
// A custom context to use for the client.
// If not provided, context.Background() will be used.
// Note: This context will be used for the entire lifecycle of the client.
Context context.Context
// AuthConversator is the interface for the authenticator.
// gotgproto.BasicConversator is used by default.
AuthConversator AuthConversator
// MigrationTimeout configures migration timeout.
MigrationTimeout time.Duration
// AckBatchSize is limit of MTProto ACK buffer size.
AckBatchSize int
// AckInterval is maximum time to buffer MTProto ACK.
AckInterval time.Duration
// RetryInterval is duration between send retries.
RetryInterval time.Duration
// MaxRetries is limit of send retries.
MaxRetries int
// ExchangeTimeout is timeout of every key exchange request.
ExchangeTimeout time.Duration
// DialTimeout is timeout of creating connection.
DialTimeout time.Duration
// CompressThreshold is a threshold in bytes to determine that message
// is large enough to be compressed using GZIP.
// If < 0, compression will be disabled.
// If == 0, default value will be used.
CompressThreshold int
// NoAutoAuth is a flag to disable automatic authentication
// if the current session is invalid.
NoAutoAuth bool
// NoAutoStart is a flag to disable automatic start of the client.
NoAutoStart bool
}
var DefaultOpts = &ClientOpts{
SystemLangCode: "en",
ClientLangCode: "en",
}
// NewClient creates a new gotgproto client and logs in to telegram.
func NewClient(appId int, apiHash string, cType clientType, opts *ClientOpts) (*Client, error) {
if opts == nil {
opts = DefaultOpts
}
if opts.StorageConfig == nil {
opts.StorageConfig = storage.DefaultStorageConfig
}
if opts.Context == nil {
opts.Context = context.Background()
}
ctx, cancel := context.WithCancel(opts.Context)
peerStorage, sessionStorage, err := sessionMaker.NewSessionStorage(ctx, opts.Session, cType.getValue(), opts.StorageConfig)
if err != nil {
cancel()
return nil, err
}
if opts.AuthConversator == nil {
opts.AuthConversator = BasicConversator()
}
d := dispatcher.NewNativeDispatcher(opts.AutoFetchReply, opts.FetchEntireReplyChain, opts.ErrorHandler, opts.PanicHandler, peerStorage)
c := Client{
Resolver: opts.Resolver,
PublicKeys: opts.PublicKeys,
DC: opts.DC,
DCList: opts.DCList,
MigrationTimeout: opts.MigrationTimeout,
AckBatchSize: opts.AckBatchSize,
AckInterval: opts.AckInterval,
RetryInterval: opts.RetryInterval,
MaxRetries: opts.MaxRetries,
ExchangeTimeout: opts.ExchangeTimeout,
DialTimeout: opts.DialTimeout,
CompressThreshold: opts.CompressThreshold,
DisableCopyright: opts.DisableCopyright,
Logger: opts.Logger,
SystemLangCode: opts.SystemLangCode,
ClientLangCode: opts.ClientLangCode,
NoAutoAuth: opts.NoAutoAuth,
authConversator: opts.AuthConversator,
Dispatcher: d,
PeerStorage: peerStorage,
sessionStorage: sessionStorage,
clientType: cType,
ctx: ctx,
autoFetchReply: opts.AutoFetchReply,
cancel: cancel,
appId: appId,
apiHash: apiHash,
opts: opts,
}
c.printCredit()
if opts.NoAutoStart {
return &c, nil
}
return &c, c.Start()
}
func (c *Client) initTelegramClient(
device *telegram.DeviceConfig,
middlewares []telegram.Middleware,
) {
if device == nil {
device = &telegram.DeviceConfig{
DeviceModel: "GoTGProto",
SystemVersion: runtime.GOOS,
AppVersion: VERSION,
SystemLangCode: c.SystemLangCode,
LangCode: c.ClientLangCode,
}
}
c.Client = telegram.NewClient(c.appId, c.apiHash, telegram.Options{
DCList: c.DCList,
Resolver: c.Resolver,
DC: c.DC,
PublicKeys: c.PublicKeys,
MigrationTimeout: c.MigrationTimeout,
AckBatchSize: c.AckBatchSize,
AckInterval: c.AckInterval,
RetryInterval: c.RetryInterval,
MaxRetries: c.MaxRetries,
ExchangeTimeout: c.ExchangeTimeout,
DialTimeout: c.DialTimeout,
CompressThreshold: c.CompressThreshold,
UpdateHandler: c.Dispatcher,
SessionStorage: c.sessionStorage,
Logger: c.Logger,
Device: *device,
Middlewares: middlewares,
})
}
func (c *Client) Login(conversator ...AuthConversator) error {
authClient := c.Auth()
status, err := authClient.Status(c.ctx)
if err != nil {
return fmt.Errorf("auth status: %w", err)
}
if status.Authorized {
return nil
}
_conversator := c.authConversator
if len(conversator) > 0 {
_conversator = conversator[0]
}
switch c.clientType.getType() {
case clientTypeVPhone:
if c.NoAutoAuth {
return intErrors.ErrSessionUnauthorized
}
phoneNr := c.clientType.getValue()
if err := newAuthFlow(
authClient,
_conversator,
phoneNr,
auth.SendCodeOptions{},
).Execute(c.ctx); err != nil {
return fmt.Errorf("auth flow: %w", err)
}
case clientTypeVBot:
if !status.Authorized {
if _, err := c.Auth().Bot(c.ctx, c.clientType.getValue()); err != nil {
return fmt.Errorf("bot auth: %w", err)
}
}
default:
return fmt.Errorf("invalid client type, must be either clientTypeVPhone or clientTypeVBot")
}
return nil
}
func (ch *Client) printCredit() {
if !ch.DisableCopyright {
fmt.Printf(`
GoTGProto %s, Copyright (C) 2024 Anony <github.com/celestix>
Licensed under the terms of GNU General Public License v3
`, VERSION)
}
}
func (c *Client) initialize(wg *sync.WaitGroup) func(ctx context.Context) error {
return func(ctx context.Context) error {
if err := c.Login(); err != nil {
return err
}
self, err := c.Client.Self(ctx)
if err != nil {
return err
}
c.Self = self
c.Dispatcher.Initialize(ctx, c.Stop, c.Client, self)
c.PeerStorage.AddPeer(self.ID, self.AccessHash, storage.TypeUser, self.Username)
// notify channel that client is up
wg.Done()
c.running = true
<-c.ctx.Done()
return c.ctx.Err()
}
}
// ExportStringSession EncodeSessionToString encodes the client session to a string in base64.
//
// Note: You must not share this string with anyone, it contains auth details for your logged in account.
func (c *Client) ExportStringSession() (string, error) {
// InMemorySession case
loadedSessionData, err := c.sessionStorage.LoadSession(c.ctx)
if err == nil {
loadedSession := &storage.Session{
Version: storage.LatestVersion,
Data: loadedSessionData,
}
return functions.EncodeSessionToString(loadedSession)
}
session, err := c.PeerStorage.GetSession(c.clientType.getValue())
if err != nil {
return "", fmt.Errorf("get session: %w", err)
}
return functions.EncodeSessionToString(session)
}
// Idle keeps the current goroutined blocked until the client is stopped.
func (c *Client) Idle() error {
<-c.ctx.Done()
return c.err
}
// CreateContext creates a new pseudo updates context.
// A context retrieved from this method should be reused.
func (c *Client) CreateContext() *ext.Context {
return ext.NewContext(
c.ctx,
c.API(),
c.PeerStorage,
c.Self,
message.NewSender(c.API()),
&tg.Entities{
Users: map[int64]*tg.User{
c.Self.ID: c.Self,
},
},
c.autoFetchReply,
)
}
// Stop cancels the context.Context being used for the client
// and stops it.
//
// Notes:
//
// 1.) Client.Idle() will exit if this method is called.
//
// 2.) You can call Client.Start() to start the client again
// if it was stopped using this method.
func (c *Client) Stop() {
c.cancel()
c.running = false
}
// Start connects the client to telegram servers and logins.
// It will return error if the client is already running.
func (c *Client) Start(opts ...*ClientOpts) error {
if c.running {
return intErrors.ErrClientAlreadyRunning
}
if len(opts) > 0 {
c.opts = opts[0]
}
if c.opts == nil {
}
if c.appId == 0 || len(c.apiHash) == 0 {
return intErrors.ErrClientNotInitialized
}
if c.ctx.Err() == context.Canceled {
c.ctx, c.cancel = context.WithCancel(context.Background())
}
c.initTelegramClient(c.opts.Device, c.opts.Middlewares)
wg := sync.WaitGroup{}
wg.Add(1)
go func(c *Client) {
if c.opts.RunMiddleware == nil {
c.err = c.Run(c.ctx, c.initialize(&wg))
} else {
c.err = c.opts.RunMiddleware(
c.Run,
c.ctx,
c.initialize(&wg),
)
}
if c.err != nil {
wg.Done()
}
}(c)
// wait till client starts
wg.Wait()
return c.err
}
// RefreshContext casts the new context.Context and telegram session
// to ext.Context (It may be used after doing Stop and Start calls respectively.)
func (c *Client) RefreshContext(ctx *ext.Context) {
(*ctx).Context = c.ctx
(*ctx).Raw = c.API()
}