|
@@ -0,0 +1,27 @@
|
|
|
|
|
+package sessionimport
|
|
|
|
|
+
|
|
|
|
|
+import (
|
|
|
|
|
+ "fmt"
|
|
|
|
|
+
|
|
|
|
|
+ "github.com/gotd/td/session"
|
|
|
|
|
+ "github.com/gotd/td/session/tdesktop"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+// ConvertTdataSession reads a Telegram Desktop tdata/ folder and builds a
|
|
|
|
|
+// gotd session.Data. localPassword is the local-storage password used to decrypt
|
|
|
|
|
+// the tdata blob; pass an empty string when the desktop client did not set a
|
|
|
|
|
+// local password (the common case for 协议号).
|
|
|
|
|
+func ConvertTdataSession(tdataDir, localPassword string) (*session.Data, error) {
|
|
|
|
|
+ accounts, err := tdesktop.Read(tdataDir, []byte(localPassword))
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return nil, fmt.Errorf("read tdata: %w", err)
|
|
|
|
|
+ }
|
|
|
|
|
+ if len(accounts) == 0 {
|
|
|
|
|
+ return nil, fmt.Errorf("no accounts found in tdata")
|
|
|
|
|
+ }
|
|
|
|
|
+ data, err := session.TDesktopSession(accounts[0])
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return nil, fmt.Errorf("convert tdesktop account: %w", err)
|
|
|
|
|
+ }
|
|
|
|
|
+ return data, nil
|
|
|
|
|
+}
|