Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for logging in using tdesktop session data (TDATA) #58

Merged
merged 2 commits into from Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading