-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth.go
69 lines (58 loc) · 1.75 KB
/
auth.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
package main
import (
"flag"
tdlib "github.com/zelenin/go-tdlib/client"
"log"
"os"
"path/filepath"
)
func (config *Config) authorize() *tdlib.Client {
var authOnly = flag.Bool("auth-only", false, "Only authorize to Telegram and then exit")
flag.Parse()
authorizer := tdlib.ClientAuthorizer()
go tdlib.CliInteractor(authorizer)
authorizer.TdlibParameters <- &tdlib.SetTdlibParametersRequest{
UseTestDc: false,
DatabaseDirectory: filepath.Join(config.StateDir, ".tdlib", "database"),
FilesDirectory: filepath.Join(config.StateDir, ".tdlib", "files"),
UseFileDatabase: true,
UseChatInfoDatabase: true,
UseMessageDatabase: true,
UseSecretChats: false,
ApiId: config.ApiId,
ApiHash: config.ApiHash,
SystemLanguageCode: "en",
DeviceModel: "Server",
SystemVersion: "1.0.0",
ApplicationVersion: "1.0.0",
EnableStorageOptimizer: true,
IgnoreFileNames: false,
}
_, err := tdlib.SetLogVerbosityLevel(&tdlib.SetLogVerbosityLevelRequest{
NewVerbosityLevel: config.LogVerbosityLevel,
})
if err != nil {
log.Fatalf("SetLogVerbosityLevel error: %v", err)
}
client, err := tdlib.NewClient(authorizer)
if err != nil {
log.Fatalf("NewClient error: %v", err)
}
optionValue, err := tdlib.GetOption(&tdlib.GetOptionRequest{
Name: "version",
})
if err != nil {
log.Fatalf("GetOption error: %v", err)
}
log.Printf("TDLib version: %s", optionValue.(*tdlib.OptionValueString).Value)
me, err := client.GetMe()
if err != nil {
log.Fatalf("GetMe error: %v", err)
}
log.Printf("Authorized user: %s %s [%s]", me.FirstName, me.LastName, me.Usernames.ActiveUsernames[0])
if *authOnly {
os.Exit(0)
return nil
}
return client
}