aboutsummaryrefslogtreecommitdiff
path: root/telegram/connect.go
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2019-11-24 20:10:29 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2019-11-24 20:10:29 +0300
commit653b1bde94b89d91862007a18b8730ff58673f59 (patch)
tree5b0c8674f918eb49dd27e53918ff10529c0ff55c /telegram/connect.go
parentc0c21a35a4cfd326423ad530926a0e96c1b07dcf (diff)
Telegram authorization
Diffstat (limited to 'telegram/connect.go')
-rw-r--r--telegram/connect.go57
1 files changed, 36 insertions, 21 deletions
diff --git a/telegram/connect.go b/telegram/connect.go
index c30f9a0..7e92bc8 100644
--- a/telegram/connect.go
+++ b/telegram/connect.go
@@ -3,6 +3,8 @@ package telegram
import (
"github.com/pkg/errors"
+ "dev.narayana.im/narayana/telegabber/xmpp/gateway"
+
log "github.com/sirupsen/logrus"
"github.com/zelenin/go-tdlib/client"
)
@@ -15,26 +17,15 @@ func (c *Client) Connect() error {
log.Warn("Connecting to Telegram network...")
- authorizer := client.ClientAuthorizer()
- go func() {
- for {
- state, ok := <-authorizer.State
- if !ok {
- return
- }
+ c.authorizer = client.ClientAuthorizer()
- ok = authorizationStateHandler(state)
- if !ok {
- return
- }
- }
- }()
+ go c.interactor()
- authorizer.TdlibParameters <- c.parameters
+ c.authorizer.TdlibParameters <- c.parameters
- tdlibClient, err := client.NewClient(authorizer, c.logVerbosity)
+ tdlibClient, err := client.NewClient(c.authorizer, c.logVerbosity)
if err != nil {
- return errors.Wrap(err, "Coudn't initialize a Telegram client instance")
+ return errors.Wrap(err, "Couldn't initialize a Telegram client instance")
}
c.client = tdlibClient
@@ -59,10 +50,34 @@ func (c *Client) Disconnect() {
c.online = false
}
-func authorizationStateHandler(state client.AuthorizationState) bool {
- switch state.AuthorizationStateType() {
- // TODO
- }
+func (c *Client) interactor() {
+ for {
+ state, ok := <-c.authorizer.State
+ if !ok {
+ return
+ }
+
+ stateType := state.AuthorizationStateType()
+ log.Infof("Telegram authorization state: %#v", stateType)
- return true
+ switch stateType {
+ case client.TypeAuthorizationStateWaitPhoneNumber:
+ log.Warn("Logging in...")
+ if c.Session.Login != "" {
+ c.authorizer.PhoneNumber <- c.Session.Login
+ } else {
+ gateway.SendMessage(c.jid, "", "Please, enter your Telegram login via /login 12345", c.xmpp)
+ }
+ case client.TypeAuthorizationStateWaitCode:
+ log.Warn("Waiting for authorization code...")
+ gateway.SendMessage(c.jid, "", "Please, enter authorization code via /code 12345", c.xmpp)
+ case client.TypeAuthorizationStateWaitPassword:
+ log.Warn("Waiting for 2FA password...")
+ gateway.SendMessage(c.jid, "", "Please, enter 2FA passphrase via /password 12345", c.xmpp)
+ case client.TypeAuthorizationStateReady:
+ log.Warn("Authorization successful!")
+ // TODO
+ return
+ }
+ }
}