diff options
author | bodqhrohro <bodqhrohro@gmail.com> | 2019-11-04 01:15:43 +0300 |
---|---|---|
committer | bodqhrohro <bodqhrohro@gmail.com> | 2019-11-04 01:15:43 +0300 |
commit | aaf7233c89af4dfbc2a8b9f5e11b4ff5e9713567 (patch) | |
tree | d449c7a41d1945e249cd066921c7a436f7614f84 /telegram/client.go | |
parent | 7e036fd795cc0b5710d3b049dc98f4538c32da6a (diff) |
Presence/iq handling and rudimental telegram client
Diffstat (limited to 'telegram/client.go')
-rw-r--r-- | telegram/client.go | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/telegram/client.go b/telegram/client.go new file mode 100644 index 0000000..b0cee44 --- /dev/null +++ b/telegram/client.go @@ -0,0 +1,118 @@ +package telegram + +import ( + "fmt" + "github.com/pkg/errors" + "path/filepath" + "strconv" + + "dev.narayana.im/narayana/telegabber/config" + + "github.com/zelenin/go-tdlib/client" +) + +var logConstants = map[string]int32{ + "fatal": 0, + "error": 1, + "warn": 2, + "info": 3, + "debug": 4, + "verbose": 5, + "all": 1023, +} + +func stringToLogConstant(c string) int32 { + level, ok := logConstants[c] + if !ok { + level = 0 + } + + return level +} + +type TelegramClient struct { + client *client.Client + jid string + parameters *client.TdlibParameters + online bool + logVerbosity client.Option +} + +// NewClient instantiates a Telegram App +func NewClient(conf config.TelegramConfig, jid string) (TelegramClient, error) { + logVerbosity := client.WithLogVerbosity(&client.SetLogVerbosityLevelRequest{ + NewVerbosityLevel: stringToLogConstant(conf.Loglevel), + }) + + apiId, err := strconv.Atoi(conf.Tdlib.Client.APIID) + if err != nil { + return TelegramClient{}, errors.Wrap(err, "Wrong api_id") + } + + parameters := client.TdlibParameters{ + UseTestDc: false, + + DatabaseDirectory: filepath.Join("./sessions/", jid), + FilesDirectory: filepath.Join("./sessions/", jid, "/files/"), + + UseFileDatabase: true, + UseChatInfoDatabase: conf.Tdlib.Client.UseChatInfoDatabase, + UseMessageDatabase: true, + UseSecretChats: conf.Tdlib.Client.UseSecretChats, + + ApiId: int32(apiId), + ApiHash: conf.Tdlib.Client.APIHash, + + SystemLanguageCode: "en", + DeviceModel: conf.Tdlib.Client.DeviceModel, + SystemVersion: "1.0.0", + ApplicationVersion: conf.Tdlib.Client.ApplicationVersion, + + EnableStorageOptimizer: true, + IgnoreFileNames: false, + } + + return TelegramClient{ + parameters: ¶meters, + jid: jid, + logVerbosity: logVerbosity, + }, nil +} + +func updateHandler(tdlibClient *client.Client) { + listener := tdlibClient.GetListener() + defer listener.Close() + + for update := range listener.Updates { + if update.GetClass() == client.ClassUpdate { + fmt.Printf("%#v", update) + } + } +} + +func (c *TelegramClient) Connect() error { + if c.online { + return nil + } + + authorizer := client.ClientAuthorizer() + authorizer.TdlibParameters <- c.parameters + + tdlibClient, err := client.NewClient(authorizer, c.logVerbosity) + if err != nil { + return errors.Wrap(err, "Coudn't initialize a Telegram client instance") + } + + c.client = tdlibClient + c.online = true + + go updateHandler(c.client) + + return nil +} + +func (c *TelegramClient) Disconnect() { + if !c.online { + return + } +} |