48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package screens
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
|
||
"example.com/m/bot"
|
||
"github.com/NicoNex/echotron/v3"
|
||
"github.com/rs/zerolog/log"
|
||
)
|
||
|
||
type Login struct{}
|
||
|
||
func (s *Login) Enter(b *bot.Bot, mode bot.RenderMode) {
|
||
telegramID := b.ChatID
|
||
var username *string
|
||
|
||
chatInfo, err := b.GetChat(telegramID)
|
||
if err == nil && chatInfo.Result != nil && chatInfo.Result.Username != "" {
|
||
username = &chatInfo.Result.Username
|
||
}
|
||
|
||
token, err := b.Backend.CreateLoginToken(context.Background(), telegramID, username)
|
||
if err != nil {
|
||
log.Error().Err(err).Msg("Failed to create login token")
|
||
b.SendMessage("❌ Произошла ошибка при создании токена входа. Попробуйте позже.", b.ChatID, nil)
|
||
return
|
||
}
|
||
|
||
loginURL := b.Backend.GetLoginURL(token)
|
||
|
||
usernameDisplay := "аноним"
|
||
if username != nil && *username != "" {
|
||
usernameDisplay = *username
|
||
}
|
||
|
||
message := fmt.Sprintf("Привет, %s!\n\nНажми кнопку ниже для входа на сайт.", usernameDisplay)
|
||
b.SendMessageWithURLButton(message, "Войти на сайт", loginURL)
|
||
}
|
||
func (s *Login) HandleCallback(b *bot.Bot, u *echotron.Update) { return }
|
||
|
||
func (s *Login) HandleMessage(b *bot.Bot, u *echotron.Update) { return }
|
||
|
||
func (s *Login) Handle(b *bot.Bot, u *echotron.Update) {
|
||
b.SetState(&MainMenu{}, bot.NewMessage)
|
||
return
|
||
}
|