Documentation
¶
Overview ¶
Package tgflow implements helpers that reduce boilerplate for telegram client.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Auth ¶
type Auth struct {
Auth UserAuthenticator
Options telegram.SendCodeOptions
}
Auth simplifies boilerplate for authentication flow.
func NewAuth ¶
func NewAuth(auth UserAuthenticator, opt telegram.SendCodeOptions) Auth
NewAuth initializes new authentication flow.
func (Auth) Run ¶
func (f Auth) Run(ctx context.Context, client AuthFlowClient) error
Run starts authentication flow on client.
Example ¶
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/tgflow"
)
func main() {
check := func(err error) {
if err != nil {
panic(err)
}
}
appIDString := os.Getenv("APP_ID")
appHash := os.Getenv("APP_HASH")
phone := os.Getenv("PHONE")
pass := os.Getenv("PASSWORD")
if appIDString == "" || appHash == "" || phone == "" || pass == "" {
log.Fatal("PHONE, PASSWORD, APP_ID or APP_HASH is not set: skip")
}
appID, err := strconv.Atoi(appIDString)
check(err)
ctx := context.Background()
client := telegram.NewClient(appID, appHash, telegram.Options{})
check(client.Connect(ctx))
codeAsk := func(ctx context.Context) (string, error) {
fmt.Print("code:")
code, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return "", err
}
code = strings.ReplaceAll(code, "\n", "")
return code, nil
}
check(tgflow.NewAuth(
tgflow.ConstantAuth(phone, pass, tgflow.CodeAuthenticatorFunc(codeAsk)),
telegram.SendCodeOptions{},
).Run(ctx, client))
}
type AuthFlowClient ¶
type AuthFlowClient interface {
AuthSignIn(ctx context.Context, phone, code, codeHash string) error
AuthSendCode(ctx context.Context, phone string, options telegram.SendCodeOptions) (codeHash string, err error)
AuthPassword(ctx context.Context, password string) error
}
AuthFlowClient abstracts telegram client for Auth.
type CodeAuthenticator ¶
CodeAuthenticator asks user for received authentication code.
type CodeAuthenticatorFunc ¶
CodeAuthenticatorFunc is functional wrapper for CodeAuthenticator.
type UserAuthenticator ¶
type UserAuthenticator interface {
Phone(ctx context.Context) (string, error)
Password(ctx context.Context) (string, error)
CodeAuthenticator
}
UserAuthenticator asks user for phone, password and received authentication code.
func ConstantAuth ¶
func ConstantAuth(phone, password string, code CodeAuthenticator) UserAuthenticator
ConstantAuth creates UserAuthenticator with constant phone and password.
Click to show internal directories.
Click to hide internal directories.