парсер
This commit is contained in:
80
tg_parser/cmd/auth/main.go
Normal file
80
tg_parser/cmd/auth/main.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gotd/td/session"
|
||||
"github.com/gotd/td/telegram"
|
||||
"github.com/gotd/td/telegram/auth"
|
||||
"github.com/gotd/td/tg"
|
||||
)
|
||||
|
||||
func main() {
|
||||
apiID := mustEnvInt("TELEGRAM__API_ID")
|
||||
apiHash := mustEnv("TELEGRAM__API_HASH")
|
||||
sessionFile := mustEnv("TELEGRAM__SESSION_FILE")
|
||||
|
||||
phone := strings.TrimSpace(os.Getenv("TELEGRAM__PHONE"))
|
||||
if phone == "" {
|
||||
phone = prompt("Phone (e.g. +79991234567): ")
|
||||
}
|
||||
|
||||
password := strings.TrimSpace(os.Getenv("TELEGRAM__PASSWORD"))
|
||||
if password == "" {
|
||||
password = prompt("2FA password (empty if not set): ")
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
client := telegram.NewClient(apiID, apiHash, telegram.Options{
|
||||
SessionStorage: &session.FileStorage{Path: sessionFile},
|
||||
})
|
||||
|
||||
err := client.Run(ctx, func(ctx context.Context) error {
|
||||
return client.Auth().IfNecessary(ctx, auth.NewFlow(
|
||||
auth.Constant(phone, password, auth.CodeAuthenticatorFunc(
|
||||
func(ctx context.Context, sentCode *tg.AuthSentCode) (string, error) {
|
||||
return prompt("Enter code: "), nil
|
||||
},
|
||||
)),
|
||||
auth.SendCodeOptions{},
|
||||
))
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "auth failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("✓ Authorized")
|
||||
}
|
||||
|
||||
func mustEnv(key string) string {
|
||||
value := strings.TrimSpace(os.Getenv(key))
|
||||
if value == "" {
|
||||
fmt.Fprintf(os.Stderr, "missing required env: %s\n", key)
|
||||
os.Exit(1)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func mustEnvInt(key string) int {
|
||||
raw := mustEnv(key)
|
||||
v, err := strconv.Atoi(raw)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "invalid int in %s: %v\n", key, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func prompt(label string) string {
|
||||
fmt.Print(label)
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
value, _ := reader.ReadString('\n')
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
Reference in New Issue
Block a user