Skip to content

Commit

Permalink
add web auth example (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emanxu authored Jan 14, 2024
1 parent e8ca146 commit 0d8f4d5
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
40 changes: 40 additions & 0 deletions examples/auth-using-api-base/main.go
Original file line number Diff line number Diff line change
@@ -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()

}
37 changes: 37 additions & 0 deletions examples/auth-using-api-base/web/api.go
Original file line number Diff line number Diff line change
@@ -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)

}
}
55 changes: 55 additions & 0 deletions examples/auth-using-api-base/web/webauth.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 0d8f4d5

Please sign in to comment.