Skip to content

Commit

Permalink
Add support for logging in using tdesktop session data (TDATA) (#58)
Browse files Browse the repository at this point in the history
* Added TDATA session support

* Added example to authenticate with TDATA
  • Loading branch information
cynicalwork authored Feb 21, 2024
1 parent 3858369 commit 39fd696
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
54 changes: 54 additions & 0 deletions examples/auth-using-tdata/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"fmt"
"log"
"os"
"path/filepath"

"github.com/celestix/gotgproto"
"github.com/celestix/gotgproto/sessionMaker"
"github.com/gotd/td/session/tdesktop"
)

func main() {
home, err := os.UserHomeDir()
if err != nil {
log.Fatalln(err)
}
telegramDir := filepath.Join(home, ".local/share/TelegramDesktop")
accounts, err := tdesktop.Read(telegramDir, nil)
if err != nil {
log.Fatalln(err)
}

// Type of client to login to, can be of 2 types:
// 1.) Bot (Fill BotToken in this case)
// 2.) User (Fill Phone in this case)
clientType := gotgproto.ClientType{
Phone: "PHONE_NUMBER_HERE",
}

client, err := gotgproto.NewClient(
// Get AppID from https://my.telegram.org/apps
123456,
// Get ApiHash from https://my.telegram.org/apps
"API_HASH_HERE",
// ClientType, as we defined above
clientType,
// Optional parameters of client
&gotgproto.ClientOpts{
// There can be up to 3 tdesktop.Account, we consider here there is
// at least a single on, you can loop through them with
// for _, account := range accounts {// your code}
Session: sessionMaker.TdataSession(accounts[0]).Name("tdata"),
},
)
if err != nil {
log.Fatalln("failed to start client:", err)
}

fmt.Printf("client (@%s) has been started...\n", client.Self.Username)

client.Idle()
}
37 changes: 37 additions & 0 deletions sessionMaker/sessionConstructor.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package sessionMaker

import (
"context"
"encoding/json"
"errors"

"github.com/celestix/gotgproto/functions"
"github.com/celestix/gotgproto/storage"
"github.com/gotd/td/session"
"github.com/gotd/td/session/tdesktop"
)

type SessionConstructor interface {
Expand Down Expand Up @@ -111,3 +113,38 @@ func (s *StringSessionConstructor) loadSession() (string, []byte, error) {
}
return s.name, sd.Data, err
}

type TdataSessionConstructor struct {
Account tdesktop.Account
name string
}

func TdataSession(account tdesktop.Account) *TdataSessionConstructor {
return &TdataSessionConstructor{Account: account}
}

func (s *TdataSessionConstructor) Name(name string) *TdataSessionConstructor {
s.name = name
return s
}

func (s *TdataSessionConstructor) loadSession() (string, []byte, error) {
sd, err := session.TDesktopSession(s.Account)
if err != nil {
return s.name, nil, err
}
ctx := context.Background()
var (
gotdstorage = new(session.StorageMemory)
loader = session.Loader{Storage: gotdstorage}
)
// Save decoded Telegram Desktop session as gotd session.
if err := loader.Save(ctx, sd); err != nil {
return s.name, nil, err
}
data, err := json.Marshal(jsonData{
Version: storage.LatestVersion,
Data: *sd,
})
return s.name, data, err
}

0 comments on commit 39fd696

Please sign in to comment.