2019-11-05 00:25:15 +00:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2019-11-20 21:45:30 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-11-05 00:25:15 +00:00
|
|
|
"github.com/zelenin/go-tdlib/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Connect starts TDlib connection
|
|
|
|
func (c *Client) Connect() error {
|
|
|
|
if c.online {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-20 21:45:30 +00:00
|
|
|
log.Warn("Connecting to Telegram network...")
|
|
|
|
|
2019-11-05 00:25:15 +00:00
|
|
|
authorizer := client.ClientAuthorizer()
|
2019-11-07 21:09:53 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
state, ok := <-authorizer.State
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = authorizationStateHandler(state)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-11-05 00:25:15 +00:00
|
|
|
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
|
|
|
|
}
|
2019-11-07 21:09:53 +00:00
|
|
|
|
2019-11-20 21:45:30 +00:00
|
|
|
log.Warn("Disconnecting from Telegram network...")
|
|
|
|
|
|
|
|
// TODO: send unavailable presence to cached chats
|
|
|
|
|
2019-11-07 21:09:53 +00:00
|
|
|
c.client.Stop()
|
|
|
|
c.online = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func authorizationStateHandler(state client.AuthorizationState) bool {
|
|
|
|
switch state.AuthorizationStateType() {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2019-11-05 00:25:15 +00:00
|
|
|
}
|