-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.go
63 lines (54 loc) · 1.54 KB
/
terminal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package examples
import (
"bufio"
"context"
"errors"
"fmt"
"os"
"strings"
"syscall"
"golang.org/x/term"
"github.com/gotd/td/telegram/auth"
"github.com/gotd/td/tg"
)
// Terminal implements auth.UserAuthenticator prompting the terminal for
// input.
//
// This is only example implementation, you should not use it in your code.
// Copy it and modify to fit your needs.
type Terminal struct {
PhoneNumber string // optional, will be prompted if empty
}
func (Terminal) SignUp(ctx context.Context) (auth.UserInfo, error) {
return auth.UserInfo{}, errors.New("signing up not implemented in Terminal")
}
func (Terminal) AcceptTermsOfService(ctx context.Context, tos tg.HelpTermsOfService) error {
return &auth.SignUpRequired{TermsOfService: tos}
}
func (Terminal) Code(ctx context.Context, sentCode *tg.AuthSentCode) (string, error) {
fmt.Print("Enter code: ")
code, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(code), nil
}
func (a Terminal) Phone(_ context.Context) (string, error) {
if a.PhoneNumber != "" {
return a.PhoneNumber, nil
}
fmt.Print("Enter phone in international format (e.g. +1234567890): ")
phone, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(phone), nil
}
func (Terminal) Password(_ context.Context) (string, error) {
fmt.Print("Enter 2FA password: ")
bytePwd, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", err
}
return strings.TrimSpace(string(bytePwd)), nil
}