forked from celestix/gotgproto
/
client.go
355 lines (315 loc) · 9.48 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
package gotgproto
//go:generate go run ./generator
import (
"context"
"fmt"
"github.com/anonyindian/gotgproto/dispatcher"
intErrors "github.com/anonyindian/gotgproto/errors"
"github.com/anonyindian/gotgproto/ext"
"github.com/anonyindian/gotgproto/functions"
"github.com/anonyindian/gotgproto/sessionMaker"
"github.com/anonyindian/gotgproto/storage"
"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"
"github.com/pkg/errors"
"go.uber.org/zap"
"runtime"
"sync"
)
const VERSION = "v1.0.0-beta10"
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
// 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
clientType ClientType
ctx context.Context
err error
autoFetchReply bool
cancel context.CancelFunc
running bool
*telegram.Client
appId int
apiHash string
}
// Type of client to login to, can be of 2 types:
// 1.) Bot (Fill BotToken in this case)
// 2.) User (Fill Phone in this case)
type ClientType struct {
// BotToken is the unique API Token for the bot you're trying to authorize, get it from @BotFather.
BotToken string
// Mobile number of the authenticating user.
Phone string
}
type ClientOpts struct {
// Logger is instance of zap.Logger. No logs by default.
Logger *zap.Logger
// 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.SessionName
// 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
// 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
}
// 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 = &ClientOpts{
SystemLangCode: "en",
ClientLangCode: "en",
}
}
ctx, cancel := context.WithCancel(context.Background())
var sessionStorage telegram.SessionStorage
isInMemory := opts.Session.GetName() == sessionMaker.InMemorySessionName
if opts.Session == nil || isInMemory {
d, _ := opts.Session.GetData()
s := session.StorageMemory{}
err := s.StoreSession(ctx, d)
if err != nil {
cancel()
return nil, err
}
sessionStorage = &s
storage.Load("", true)
} else {
sessionStorage = &sessionMaker.SessionStorage{
Session: opts.Session,
}
}
d := dispatcher.NewNativeDispatcher(opts.AutoFetchReply, opts.ErrorHandler, opts.PanicHandler)
// client := telegram.NewClient(appId, apiHash, telegram.Options{
// DCList: opts.DCList,
// UpdateHandler: d,
// SessionStorage: sessionStorage,
// Logger: opts.Logger,
// Device: telegram.DeviceConfig{
// DeviceModel: "GoTGProto",
// SystemVersion: runtime.GOOS,
// AppVersion: VERSION,
// SystemLangCode: opts.SystemLangCode,
// LangCode: opts.ClientLangCode,
// },
// Middlewares: []telegram.Middleware{
// floodwait.NewSimpleWaiter().WithMaxRetries(25),
// ratelimit.New(rate.Every(100*time.Millisecond), 5),
// },
// })
c := Client{
Resolver: opts.Resolver,
PublicKeys: opts.PublicKeys,
DC: opts.DC,
DCList: opts.DCList,
DisableCopyright: opts.DisableCopyright,
Logger: opts.Logger,
SystemLangCode: opts.SystemLangCode,
ClientLangCode: opts.ClientLangCode,
Dispatcher: d,
sessionStorage: sessionStorage,
clientType: cType,
ctx: ctx,
autoFetchReply: opts.AutoFetchReply,
cancel: cancel,
appId: appId,
apiHash: apiHash,
}
c.printCredit()
return &c, c.Start(opts)
}
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,
UpdateHandler: c.Dispatcher,
SessionStorage: c.sessionStorage,
Logger: c.Logger,
Device: *device,
Middlewares: middlewares,
})
}
func (c *Client) login() error {
authClient := c.Auth()
if c.clientType.BotToken == "" {
authFlow := auth.NewFlow(termAuth{
phone: c.clientType.Phone,
client: authClient,
},
auth.SendCodeOptions{})
if err := IfAuthNecessary(authClient, c.ctx, Flow(authFlow)); err != nil {
return err
}
} else {
status, err := authClient.Status(c.ctx)
if err != nil {
return errors.Wrap(err, "auth status")
}
if !status.Authorized {
if _, err := c.Auth().Bot(c.ctx, c.clientType.BotToken); err != nil {
return errors.Wrap(err, "login")
}
}
}
return nil
}
func (ch *Client) printCredit() {
if !ch.DisableCopyright {
fmt.Printf(`
GoTGProto %s, Copyright (C) 2023 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 {
err := c.login()
if 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)
storage.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
loadSession, err := c.sessionStorage.LoadSession(c.ctx)
if err == nil {
return string(loadSession), nil
}
// todo. what if session is InMemorySession? We got panic
return functions.EncodeSessionToString(storage.GetSession())
}
// 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.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 c.ctx.Err() == context.Canceled {
c.ctx, c.cancel = context.WithCancel(context.Background())
}
c.initTelegramClient(opts.Device, opts.Middlewares)
wg := sync.WaitGroup{}
wg.Add(1)
go func(c *Client) {
c.err = c.Run(c.ctx, c.initialize(&wg))
}(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()
}