tgflow

package
v0.13.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package tgflow implements helpers that reduce boilerplate for telegram client.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrPasswordNotProvided = errors.New("password requested but not provided")

ErrPasswordNotProvided means that password requested by Telegram, but not provided by user.

Functions

This section is empty.

Types

type Auth

type Auth struct {
	Auth    UserAuthenticator
	Options telegram.SendCodeOptions
}

Auth simplifies boilerplate for authentication flow.

func NewAuth

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))
}
Output:

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

type CodeAuthenticator interface {
	Code(ctx context.Context) (string, error)
}

CodeAuthenticator asks user for received authentication code.

type CodeAuthenticatorFunc

type CodeAuthenticatorFunc func(ctx context.Context) (string, error)

CodeAuthenticatorFunc is functional wrapper for CodeAuthenticator.

func (CodeAuthenticatorFunc) Code

Code implements CodeAuthenticator interface.

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 CodeOnlyAuth added in v0.11.0

func CodeOnlyAuth(phone string, code CodeAuthenticator) UserAuthenticator

CodeOnlyAuth creates UserAuthenticator with constant phone and no password.

func ConstantAuth

func ConstantAuth(phone, password string, code CodeAuthenticator) UserAuthenticator

ConstantAuth creates UserAuthenticator with constant phone and password.

func TestAuth added in v0.11.0

func TestAuth(randReader io.Reader, dc int) UserAuthenticator

TestAuth returns UserAuthenticator that authenticates via testing credentials.

Can be used only with testing server.

Example
package main

import (
	"context"
	"crypto/rand"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/telegram/tgflow"
)

func main() {
	// Example of using test server.
	const dcID = 2

	ctx := context.Background()
	client := telegram.NewClient(telegram.TestAppID, telegram.TestAppHash, telegram.Options{
		Addr: telegram.AddrTest,
	})
	if err := client.Connect(ctx); err != nil {
		panic(err)
	}

	if err := tgflow.NewAuth(
		tgflow.TestAuth(rand.Reader, dcID),
		telegram.SendCodeOptions{},
	).Run(ctx, client); err != nil {
		panic(err)
	}
}
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL