-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |