diff --git a/examples/auth-using-api-base/main.go b/examples/auth-using-api-base/main.go new file mode 100644 index 0000000..254b3f7 --- /dev/null +++ b/examples/auth-using-api-base/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "github.com/celestix/gotgproto" + "github.com/celestix/gotgproto/examples/auth-using-api-base/web" + "github.com/celestix/gotgproto/sessionMaker" + "log" +) + +func main() { + + // start web api + go web.Start() + + clientType := gotgproto.ClientType{ + // put your phone here or just leave it empty + // if you leave it empty, you will be asked to enter your phone number + Phone: "", + } + client, err := gotgproto.NewClient( + // Get AppID from https://my.telegram.org/apps + 123456, + // Get ApiHash from https://my.telegram.org/apps + "API_HASH_HERE", + // ClientType, as we defined above + clientType, + // Optional parameters of client + &gotgproto.ClientOpts{ + + // custom authenticator using web api + AuthConversator: web.GetWebAuth(), + Session: sessionMaker.SqliteSession("webbot"), + }, + ) + if err != nil { + log.Fatalln("failed to start client:", err) + } + client.Idle() + +} diff --git a/examples/auth-using-api-base/web/api.go b/examples/auth-using-api-base/web/api.go new file mode 100644 index 0000000..563e1bb --- /dev/null +++ b/examples/auth-using-api-base/web/api.go @@ -0,0 +1,37 @@ +package web + +import ( + "fmt" + "net/http" +) + +// Start a web server and wait +func Start() { + http.HandleFunc("/", setInfo) + http.ListenAndServe(":9997", nil) +} + +// setInfo handle user info, set phone, code or passwd +func setInfo(w http.ResponseWriter, req *http.Request) { + action := req.URL.Query().Get("set") + + switch action { + + case "phone": + num := req.URL.Query().Get("phone") + phone := "+" + num + ReceivePhone(phone) + fmt.Fprintf(w, "phone received: %s", phone) + + case "code": + code := req.URL.Query().Get("code") + ReceiveCode(code) + fmt.Fprintf(w, "code received: %s", code) + + case "passwd": + passwd := req.URL.Query().Get("passwd") + ReceivePasswd(passwd) + fmt.Fprintf(w, "passwd received: %s", passwd) + + } +} diff --git a/examples/auth-using-api-base/web/webauth.go b/examples/auth-using-api-base/web/webauth.go new file mode 100644 index 0000000..2b035c2 --- /dev/null +++ b/examples/auth-using-api-base/web/webauth.go @@ -0,0 +1,55 @@ +package web + +import ( + "fmt" + "github.com/celestix/gotgproto" +) + +type webAuth struct{} + +var ( + phoneChan = make(chan string) + codeChan = make(chan string) + passwdChan = make(chan string) +) + +func GetWebAuth() gotgproto.AuthConversator { + return &webAuth{} +} + +func (w *webAuth) AskPhoneNumber() (string, error) { + fmt.Println("waiting for phone...") + code := <-phoneChan + return code, nil +} + +func (w *webAuth) AskCode() (string, error) { + fmt.Println("waiting for code...") + code := <-codeChan + return code, nil +} + +func (w *webAuth) AskPassword() (string, error) { + fmt.Println("waiting for 2fa password...") + code := <-passwdChan + return code, nil +} + +func (w *webAuth) RetryPassword(attemptsLeft int) (string, error) { + fmt.Println("The 2FA Code you just entered seems to be incorrect,") + fmt.Println("Attempts Left:", attemptsLeft) + fmt.Println("Please try again.... ") + return w.AskCode() +} + +func ReceivePhone(phone string) { + phoneChan <- phone +} + +func ReceiveCode(code string) { + codeChan <- code +} + +func ReceivePasswd(passwd string) { + passwdChan <- passwd +}