You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

63 lines
1.6 KiB

package handlers
import (
"bytes"
"context"
"github.com/gotd/td/session"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/auth"
"gopkg.in/tucnak/telebot.v3"
"muskrat/storage"
"os"
"regexp"
"strconv"
)
func Phone(ctx telebot.Context) error {
if !regexp.MustCompile("^[+]?[(]?[0-9]{3}[)]?[-\\s.]?[0-9]{3}[-\\s.]?[0-9]{4,8}$").MatchString(ctx.Text()) {
_ = storage.ClearState(context.Background(), ctx.Sender().ID)
return List(ctx)
}
s := session.StorageMemory{}
appID, err := strconv.Atoi(os.Getenv("TELEGRAM_APP_ID"))
if err != nil {
_ = storage.ClearState(context.Background(), ctx.Sender().ID)
return err
}
client := telegram.NewClient(appID, os.Getenv("TELEGRAM_APP_HASH"), telegram.Options{
SessionStorage: &s,
})
err = client.Run(context.Background(), func(c context.Context) error {
code, err := client.Auth().SendCode(c, ctx.Text(), auth.SendCodeOptions{})
if err != nil {
return err
}
err = storage.SetState(context.Background(), ctx.Sender().ID, "code", ctx.Text(), code.PhoneCodeHash)
if err != nil {
return err
}
return nil
})
if err != nil {
_ = storage.ClearState(context.Background(), ctx.Sender().ID)
return List(ctx)
}
b := new(bytes.Buffer)
err = s.Dump(b)
if err != nil {
_ = storage.ClearState(context.Background(), ctx.Sender().ID)
return err
}
if len(storage.Registration) == 0 {
storage.Registration = make(map[int64][]byte)
}
storage.Registration[ctx.Sender().ID] = b.Bytes()
return ctx.Send("Введите <b>код</b> из личных сообщений в формате <code>1 2 3 4 5</code>:")
}