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.
 
 

88 lines
2.2 KiB

package handlers
import (
"context"
"gitea.russia9.dev/russia9/muskrat/storage"
"gitea.russia9.dev/russia9/muskrat/types"
"gitea.russia9.dev/russia9/muskrat/userbot"
"github.com/gotd/td/session"
"github.com/gotd/td/telegram"
"gopkg.in/telebot.v3"
"os"
"strconv"
"time"
)
func Password(ctx telebot.Context, metadata []string) error {
sess := storage.Registration[ctx.Sender().ID]
if len(sess) == 0 {
_ = storage.ClearState(context.Background(), ctx.Sender().ID)
return List(ctx)
}
s := session.StorageMemory{}
err := s.StoreSession(context.Background(), sess)
if err != nil {
_ = storage.ClearState(context.Background(), ctx.Sender().ID)
return err
}
appID, err := strconv.Atoi(os.Getenv("TELEGRAM_APP_ID"))
if err != nil {
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 {
_, err = client.Auth().Password(context.Background(), ctx.Text())
if err != nil {
return err
}
// Success
self, err := client.Self(context.Background())
if err != nil {
return err
}
// Success
account := &types.Account{
ID: self.ID,
User: ctx.Sender().ID,
Session: storage.Registration[ctx.Sender().ID],
}
err = storage.DB.Create(account).Error
if err != nil {
return ctx.Send("Аккаунт с таким ID уже есть в боте")
}
if len(userbot.Userbots) == 0 {
userbot.Userbots = make(map[int64]*userbot.Userbot)
}
if userbot.Userbots[account.ID] != nil {
userbot.Userbots[account.ID].Stop()
delete(userbot.Userbots, account.ID)
}
ubot := &userbot.Userbot{
Account: account,
}
go ubot.Start(c)
userbot.Userbots[account.ID] = ubot
time.Sleep(time.Second * 2)
delete(storage.Registration, ctx.Sender().ID)
_ = storage.ClearState(context.Background(), ctx.Sender().ID)
return ctx.Send("Успешный логин" + self.FirstName + " " + self.LastName)
})
if err != nil {
delete(storage.Registration, ctx.Sender().ID)
_ = storage.ClearState(context.Background(), ctx.Sender().ID)
return ctx.Send("Неправильный пароль")
}
return err
}