aboutsummaryrefslogblamecommitdiff
path: root/telegram/connect.go
blob: 400c70656dabb50922f64077a4e25fe1484073e7 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15














                                               













                                                             



















                                                                                        










                                                                      
 
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()
	go func() {
		for {
			state, ok := <-authorizer.State
			if !ok {
				return
			}

			ok = authorizationStateHandler(state)
			if !ok {
				return
			}
		}
	}()

	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
	}

	c.client.Stop()
	c.online = false
}

func authorizationStateHandler(state client.AuthorizationState) bool {
	switch state.AuthorizationStateType() {
	// TODO
	}

	return true
}