Files
tgex-backend/telegram_bot/screens/login.go
Artem Tsyrulnikov 2fb8c83041 new telegram bot
2025-12-30 19:50:15 +03:00

48 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
}