telegram package refactoring

This commit is contained in:
bodqhrohro 2019-11-05 02:25:15 +02:00
parent 0f047c3816
commit 55797b98a0
3 changed files with 44 additions and 34 deletions

View file

@ -30,7 +30,8 @@ func stringToLogConstant(c string) int32 {
return level return level
} }
type TelegramClient struct { // Client stores the metadata for lazily invoked TDlib instance
type Client struct {
client *client.Client client *client.Client
jid string jid string
parameters *client.TdlibParameters parameters *client.TdlibParameters
@ -39,14 +40,14 @@ type TelegramClient struct {
} }
// NewClient instantiates a Telegram App // NewClient instantiates a Telegram App
func NewClient(conf config.TelegramConfig, jid string) (TelegramClient, error) { func NewClient(conf config.TelegramConfig, jid string) (Client, error) {
logVerbosity := client.WithLogVerbosity(&client.SetLogVerbosityLevelRequest{ logVerbosity := client.WithLogVerbosity(&client.SetLogVerbosityLevelRequest{
NewVerbosityLevel: stringToLogConstant(conf.Loglevel), NewVerbosityLevel: stringToLogConstant(conf.Loglevel),
}) })
apiId, err := strconv.Atoi(conf.Tdlib.Client.APIID) apiID, err := strconv.Atoi(conf.Tdlib.Client.APIID)
if err != nil { if err != nil {
return TelegramClient{}, errors.Wrap(err, "Wrong api_id") return Client{}, errors.Wrap(err, "Wrong api_id")
} }
parameters := client.TdlibParameters{ parameters := client.TdlibParameters{
@ -60,7 +61,7 @@ func NewClient(conf config.TelegramConfig, jid string) (TelegramClient, error) {
UseMessageDatabase: true, UseMessageDatabase: true,
UseSecretChats: conf.Tdlib.Client.UseSecretChats, UseSecretChats: conf.Tdlib.Client.UseSecretChats,
ApiId: int32(apiId), ApiId: int32(apiID),
ApiHash: conf.Tdlib.Client.APIHash, ApiHash: conf.Tdlib.Client.APIHash,
SystemLanguageCode: "en", SystemLanguageCode: "en",
@ -72,7 +73,7 @@ func NewClient(conf config.TelegramConfig, jid string) (TelegramClient, error) {
IgnoreFileNames: false, IgnoreFileNames: false,
} }
return TelegramClient{ return Client{
parameters: &parameters, parameters: &parameters,
jid: jid, jid: jid,
logVerbosity: logVerbosity, logVerbosity: logVerbosity,
@ -89,30 +90,3 @@ func updateHandler(tdlibClient *client.Client) {
} }
} }
} }
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
}
}

36
telegram/connect.go Normal file
View file

@ -0,0 +1,36 @@
package telegram
import (
"github.com/pkg/errors"
"github.com/zelenin/go-tdlib/client"
)
// Connect starts TDlib connection
func (c *Client) 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
}
// Disconnect drops TDlib connection
func (c *Client) Disconnect() {
if !c.online {
return
}
}

View file

@ -8,7 +8,7 @@ import (
"gosrc.io/xmpp/stanza" "gosrc.io/xmpp/stanza"
) )
var sessions map[string]telegram.TelegramClient var sessions map[string]telegram.Client
func logPacketType(p stanza.Packet) { func logPacketType(p stanza.Packet) {
log.Warn("Ignoring packet: %T\n", p) log.Warn("Ignoring packet: %T\n", p)