Stage 15: dual Telegram bots & language-gated variants
Tests · Go / test (push) Successful in 9s
Tests · Integration / integration (push) Successful in 10s
Tests · UI / test (push) Successful in 20s
Tests · Go / test (pull_request) Successful in 8s
Tests · Integration / integration (pull_request) Successful in 11s
Tests · UI / test (pull_request) Successful in 19s
Tests · Go / test (push) Successful in 9s
Tests · Integration / integration (push) Successful in 10s
Tests · UI / test (push) Successful in 20s
Tests · Go / test (pull_request) Successful in 8s
Tests · Integration / integration (pull_request) Successful in 11s
Tests · UI / test (pull_request) Successful in 19s
Service-agnostic refinement of the owner's idea: the sign-in service returns a set of supported game languages with the user identity, and the lobby gates the New Game variant choice by it (en -> English; ru -> Russian + Эрудит). - Connector hosts two bots in one container (one per service language, each its own token + game channel; the same telegram_id spans both). ValidateInitData tries each token and returns the validating bot's service_language + supported_languages. Per-language config (TELEGRAM_BOT_TOKEN_EN/_RU, channels). - supported_languages rides the Session (fbs, session-scoped, not persisted); the UI offers only the matching variants on New Game — gating only the START of a new game (auto-match + friend invite), not accept/open/play; backend does not enforce. - service_language persisted (accounts.service_language, migration 00010, written every login, last-login-wins) and routes the user-facing Notify push back through the right bot (push-target coalesces with preferred_language). - Admin SendToUser/SendToGameChannel gain an operator-chosen language selector in the console (unrelated to ValidateInitData). - Non-Telegram logins carry the gateway default set (GATEWAY_DEFAULT_SUPPORTED_LANGUAGES, all variants). Wire (committed regen): ValidateInitDataResponse +service_language +supported_languages; Session +supported_languages; SendToUser/SendToGameChannel +language. Docs (ARCHITECTURE/FUNCTIONAL/_ru/READMEs) + PLAN updated; stage marked done.
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
// Package connector implements the Telegram gRPC service (pkg/proto/telegram/v1):
|
||||
// the gateway calls ValidateInitData (Mini App auth) and Notify (out-of-app push);
|
||||
// the admin surface (Stage 10) will call SendToUser and SendToGameChannel. The
|
||||
// generic methods address a recipient by the identity external_id, so a future
|
||||
// platform connector can implement the same service.
|
||||
// the admin surface (Stage 10) calls SendToUser and SendToGameChannel. The generic
|
||||
// methods address a recipient by the identity external_id, so a future platform
|
||||
// connector can implement the same service.
|
||||
//
|
||||
// The connector hosts one bot per configured service language (en/ru); the same
|
||||
// Telegram user id spans them all. ValidateInitData tries each bot's token in turn
|
||||
// and reports which bot validated (its service language); the push and admin
|
||||
// methods route to the bot the request selects by language.
|
||||
package connector
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
@@ -28,59 +34,103 @@ type Sender interface {
|
||||
SendText(ctx context.Context, chatID int64, text string) error
|
||||
}
|
||||
|
||||
// Server implements telegramv1.TelegramServer.
|
||||
type Server struct {
|
||||
telegramv1.UnimplementedTelegramServer
|
||||
validator initdata.Validator
|
||||
widgetValidator loginwidget.Validator
|
||||
sender Sender
|
||||
channelID int64
|
||||
log *zap.Logger
|
||||
// BotRuntime is one configured language-tagged bot: its sender, game channel id,
|
||||
// and the two HMAC validators bound to its token.
|
||||
type BotRuntime struct {
|
||||
// Language is the bot's service language (en/ru).
|
||||
Language string
|
||||
// Sender delivers messages through this bot.
|
||||
Sender Sender
|
||||
// ChannelID is this bot's game channel (0 disables channel posts).
|
||||
ChannelID int64
|
||||
// InitValidator verifies Mini App initData signed by this bot's token.
|
||||
InitValidator initdata.Validator
|
||||
// WidgetValidator verifies Login Widget data signed by this bot's token.
|
||||
WidgetValidator loginwidget.Validator
|
||||
}
|
||||
|
||||
// NewServer builds the gRPC service from the Mini App initData validator, the Login
|
||||
// Widget validator (Stage 11), a sender (the bot), and the configured game channel
|
||||
// id (0 disables channel posts).
|
||||
func NewServer(validator initdata.Validator, widgetValidator loginwidget.Validator, sender Sender, channelID int64, log *zap.Logger) *Server {
|
||||
// Server implements telegramv1.TelegramServer over one or more language-tagged bots.
|
||||
type Server struct {
|
||||
telegramv1.UnimplementedTelegramServer
|
||||
bots map[string]BotRuntime // keyed by service language
|
||||
order []string // stable iteration order for validation
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
// NewServer builds the gRPC service from the configured bots (at least one).
|
||||
func NewServer(bots []BotRuntime, log *zap.Logger) *Server {
|
||||
if log == nil {
|
||||
log = zap.NewNop()
|
||||
}
|
||||
return &Server{validator: validator, widgetValidator: widgetValidator, sender: sender, channelID: channelID, log: log}
|
||||
m := make(map[string]BotRuntime, len(bots))
|
||||
order := make([]string, 0, len(bots))
|
||||
for _, b := range bots {
|
||||
m[b.Language] = b
|
||||
order = append(order, b.Language)
|
||||
}
|
||||
return &Server{bots: m, order: order, log: log}
|
||||
}
|
||||
|
||||
// ValidateInitData verifies Mini App launch data and returns the user identity.
|
||||
// ValidateInitData verifies Mini App launch data against each bot's token in turn
|
||||
// and returns the user identity plus the validating bot's service language (which
|
||||
// routes the user's later push) and its set of offered game languages.
|
||||
func (s *Server) ValidateInitData(ctx context.Context, req *telegramv1.ValidateInitDataRequest) (*telegramv1.ValidateInitDataResponse, error) {
|
||||
u, err := s.validator.Validate(req.GetInitData())
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||
var lastErr error
|
||||
for _, lang := range s.order {
|
||||
u, err := s.bots[lang].InitValidator.Validate(req.GetInitData())
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
return &telegramv1.ValidateInitDataResponse{
|
||||
ExternalId: u.ExternalID,
|
||||
Username: u.Username,
|
||||
FirstName: u.FirstName,
|
||||
LanguageCode: u.LanguageCode,
|
||||
ServiceLanguage: lang,
|
||||
SupportedLanguages: []string{lang},
|
||||
}, nil
|
||||
}
|
||||
return &telegramv1.ValidateInitDataResponse{
|
||||
ExternalId: u.ExternalID,
|
||||
Username: u.Username,
|
||||
FirstName: u.FirstName,
|
||||
LanguageCode: u.LanguageCode,
|
||||
}, nil
|
||||
if lastErr == nil {
|
||||
lastErr = errors.New("no bot configured")
|
||||
}
|
||||
return nil, status.Error(codes.InvalidArgument, lastErr.Error())
|
||||
}
|
||||
|
||||
// ValidateLoginWidget verifies Login Widget authorization data and returns the user
|
||||
// identity, for attaching a Telegram identity to an existing account (Stage 11).
|
||||
// ValidateLoginWidget verifies Login Widget authorization data against each bot's
|
||||
// token in turn and returns the user identity, for attaching a Telegram identity to
|
||||
// an existing account (Stage 11).
|
||||
func (s *Server) ValidateLoginWidget(ctx context.Context, req *telegramv1.ValidateLoginWidgetRequest) (*telegramv1.ValidateLoginWidgetResponse, error) {
|
||||
u, err := s.widgetValidator.Validate(req.GetData())
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||
var lastErr error
|
||||
for _, lang := range s.order {
|
||||
u, err := s.bots[lang].WidgetValidator.Validate(req.GetData())
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
return &telegramv1.ValidateLoginWidgetResponse{
|
||||
ExternalId: u.ExternalID,
|
||||
Username: u.Username,
|
||||
FirstName: u.FirstName,
|
||||
}, nil
|
||||
}
|
||||
return &telegramv1.ValidateLoginWidgetResponse{
|
||||
ExternalId: u.ExternalID,
|
||||
Username: u.Username,
|
||||
FirstName: u.FirstName,
|
||||
}, nil
|
||||
if lastErr == nil {
|
||||
lastErr = errors.New("no bot configured")
|
||||
}
|
||||
return nil, status.Error(codes.InvalidArgument, lastErr.Error())
|
||||
}
|
||||
|
||||
// Notify renders and delivers an out-of-app notification. It reports
|
||||
// delivered=false (without an error) for a kind that is not pushed out-of-app or a
|
||||
// delivery the bot could not complete (e.g. the user never started the bot), so the
|
||||
// gateway treats a fallback miss as best-effort.
|
||||
// Notify renders and delivers an out-of-app notification through the bot selected by
|
||||
// the recipient's service language. It reports delivered=false (without an error)
|
||||
// when no bot serves that language, the kind is not pushed out-of-app, or the bot
|
||||
// could not deliver (e.g. the user never started it), so the gateway treats a
|
||||
// fallback miss as best-effort.
|
||||
func (s *Server) Notify(ctx context.Context, req *telegramv1.NotifyRequest) (*telegramv1.NotifyResponse, error) {
|
||||
bot, ok := s.bots[req.GetLanguage()]
|
||||
if !ok {
|
||||
s.log.Warn("notify: no bot for language", zap.String("language", req.GetLanguage()), zap.String("kind", req.GetKind()))
|
||||
return &telegramv1.NotifyResponse{Delivered: false}, nil
|
||||
}
|
||||
msg, ok := render.Render(req.GetKind(), req.GetPayload(), req.GetLanguage())
|
||||
if !ok {
|
||||
return &telegramv1.NotifyResponse{Delivered: false}, nil
|
||||
@@ -89,38 +139,58 @@ func (s *Server) Notify(ctx context.Context, req *telegramv1.NotifyRequest) (*te
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
if err := s.sender.Notify(ctx, chat, msg.Text, msg.ButtonText, msg.StartParam); err != nil {
|
||||
if err := bot.Sender.Notify(ctx, chat, msg.Text, msg.ButtonText, msg.StartParam); err != nil {
|
||||
s.log.Warn("notify delivery failed", zap.String("kind", req.GetKind()), zap.Error(err))
|
||||
return &telegramv1.NotifyResponse{Delivered: false}, nil
|
||||
}
|
||||
return &telegramv1.NotifyResponse{Delivered: true}, nil
|
||||
}
|
||||
|
||||
// SendToUser sends an arbitrary admin message to one user.
|
||||
// SendToUser sends an arbitrary admin message to one user through the bot selected
|
||||
// by language (an operator choice in the admin console).
|
||||
func (s *Server) SendToUser(ctx context.Context, req *telegramv1.SendToUserRequest) (*telegramv1.SendResponse, error) {
|
||||
bot, err := s.botFor(req.GetLanguage())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
chat, err := parseChatID(req.GetExternalId())
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
if err := s.sender.SendText(ctx, chat, req.GetText()); err != nil {
|
||||
if err := bot.Sender.SendText(ctx, chat, req.GetText()); err != nil {
|
||||
s.log.Warn("send to user failed", zap.Error(err))
|
||||
return &telegramv1.SendResponse{Delivered: false}, nil
|
||||
}
|
||||
return &telegramv1.SendResponse{Delivered: true}, nil
|
||||
}
|
||||
|
||||
// SendToGameChannel posts an admin message to the configured game channel.
|
||||
// SendToGameChannel posts an arbitrary admin message to the game channel of the bot
|
||||
// selected by language (an operator choice in the admin console).
|
||||
func (s *Server) SendToGameChannel(ctx context.Context, req *telegramv1.SendToGameChannelRequest) (*telegramv1.SendResponse, error) {
|
||||
if s.channelID == 0 {
|
||||
return nil, status.Error(codes.FailedPrecondition, "game channel is not configured")
|
||||
bot, err := s.botFor(req.GetLanguage())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.sender.SendText(ctx, s.channelID, req.GetText()); err != nil {
|
||||
if bot.ChannelID == 0 {
|
||||
return nil, status.Error(codes.FailedPrecondition, fmt.Sprintf("game channel is not configured for language %q", req.GetLanguage()))
|
||||
}
|
||||
if err := bot.Sender.SendText(ctx, bot.ChannelID, req.GetText()); err != nil {
|
||||
s.log.Warn("send to channel failed", zap.Error(err))
|
||||
return &telegramv1.SendResponse{Delivered: false}, nil
|
||||
}
|
||||
return &telegramv1.SendResponse{Delivered: true}, nil
|
||||
}
|
||||
|
||||
// botFor returns the bot tagged with language, or a FailedPrecondition error when no
|
||||
// bot serves it (admin broadcasts choose the language explicitly).
|
||||
func (s *Server) botFor(language string) (BotRuntime, error) {
|
||||
bot, ok := s.bots[language]
|
||||
if !ok {
|
||||
return BotRuntime{}, status.Error(codes.FailedPrecondition, fmt.Sprintf("no bot configured for language %q", language))
|
||||
}
|
||||
return bot, nil
|
||||
}
|
||||
|
||||
// parseChatID converts a Telegram identity external_id into a numeric chat id.
|
||||
func parseChatID(externalID string) (int64, error) {
|
||||
id, err := strconv.ParseInt(externalID, 10, 64)
|
||||
|
||||
Reference in New Issue
Block a user