feat(telegram): localized /start welcome with channel & chat follow links
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 15s
CI / ui (pull_request) Has been skipped
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m17s
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 15s
CI / ui (pull_request) Has been skipped
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m17s
The main bot answered /start with a single English line ("Tap to open Scrabble.").
Localize it: Russian or English by the sender's reported Telegram language
(Message.from.language_code, which the Bot API carries on the message itself — there is
no separate user-update event — English fallback), with the longer welcome copy and a
localized launch button ("Открыть «Эрудит»" / "Open “Erudite”").
The welcome links the game channel and the discussion chat by their public @username,
resolved once at startup from the configured TELEGRAM_GAME_CHANNEL_ID / TELEGRAM_CHAT_ID
via getChat and cached. A handle that is unset, private, or unreadable degrades to a
generic noun ("the channel" / "our chat") rather than a dangling "@", so the paragraph
always reads cleanly (the bot's info screen still lists the real links). Adds
GameChannelID to bot.Config (wired from the existing config) for the channel handle.
Tests: startText localization + handle embedding + per-slot generic fallback; handleStart
language selection; resolveWelcomeHandles. README updated.
This commit is contained in:
@@ -33,8 +33,12 @@ type Config struct {
|
||||
SendRatePerSecond int
|
||||
// ChatID is the moderated discussion chat the bot gates write access in; 0
|
||||
// disables chat gating (and the chat_member long-poll subscription). Gating needs
|
||||
// the bot to be an administrator there with the restrict-members right.
|
||||
// the bot to be an administrator there with the restrict-members right. Its public
|
||||
// @username is also resolved at startup for the /start welcome's discussion link.
|
||||
ChatID int64
|
||||
// GameChannelID is the game channel whose public @username the /start welcome links
|
||||
// to (resolved from this id via getChat at startup); 0 omits that follow link.
|
||||
GameChannelID int64
|
||||
}
|
||||
|
||||
// EligibilityResolver answers whether the Telegram user identified by externalID
|
||||
@@ -54,6 +58,13 @@ type Bot struct {
|
||||
limiter *rate.Limiter
|
||||
// chatID is the moderated discussion chat (0 disables gating).
|
||||
chatID int64
|
||||
// channelID is the game channel (0 omits its welcome follow link).
|
||||
channelID int64
|
||||
// channelUsername and chatUsername are the public @usernames (without the leading
|
||||
// @) of the game channel and the discussion chat, resolved once at startup
|
||||
// (resolveWelcomeHandles) for the /start welcome's follow links; "" when unresolved.
|
||||
channelUsername string
|
||||
chatUsername string
|
||||
// botID is the bot's own Telegram user id (resolved at startup); it skips the
|
||||
// chat_member updates the bot's own restrict actions generate — the grant loop guard.
|
||||
botID int64
|
||||
@@ -69,7 +80,7 @@ func New(cfg Config, log *zap.Logger) (*Bot, error) {
|
||||
if log == nil {
|
||||
log = zap.NewNop()
|
||||
}
|
||||
t := &Bot{miniAppURL: cfg.MiniAppURL, log: log, chatID: cfg.ChatID}
|
||||
t := &Bot{miniAppURL: cfg.MiniAppURL, log: log, chatID: cfg.ChatID, channelID: cfg.GameChannelID}
|
||||
if cfg.SendRatePerSecond > 0 {
|
||||
t.limiter = rate.NewLimiter(rate.Limit(cfg.SendRatePerSecond), cfg.SendRatePerSecond)
|
||||
}
|
||||
@@ -123,9 +134,43 @@ func (t *Bot) Run(ctx context.Context) {
|
||||
if t.chatID != 0 {
|
||||
t.logChatAdminStatus(ctx)
|
||||
}
|
||||
t.resolveWelcomeHandles(ctx)
|
||||
t.api.Start(ctx)
|
||||
}
|
||||
|
||||
// resolveWelcomeHandles resolves, once at startup, the public @usernames of the game
|
||||
// channel and the discussion chat from their configured ids (getChat), caching them for
|
||||
// the /start welcome's follow links. It runs before the update loop, so the handles are
|
||||
// set before any /start is handled; a chat that is unset, private (no public username)
|
||||
// or unreadable simply leaves its handle empty and the welcome omits that follow link.
|
||||
func (t *Bot) resolveWelcomeHandles(ctx context.Context) {
|
||||
t.channelUsername = t.resolveUsername(ctx, t.channelID, "game channel")
|
||||
t.chatUsername = t.resolveUsername(ctx, t.chatID, "discussion chat")
|
||||
}
|
||||
|
||||
// resolveUsername returns the public @username (without the leading @) of the chat with
|
||||
// the given id, or "" when id is 0, the chat has no public username, or getChat fails —
|
||||
// logging the reason, since a missing handle silently drops a welcome follow link.
|
||||
func (t *Bot) resolveUsername(ctx context.Context, id int64, label string) string {
|
||||
if id == 0 {
|
||||
return ""
|
||||
}
|
||||
chat, err := t.api.GetChat(ctx, &tgbot.GetChatParams{ChatID: id})
|
||||
if err != nil {
|
||||
t.log.Warn("welcome: getChat failed; follow link omitted",
|
||||
zap.String("chat", label), zap.Int64("id", id), zap.Error(err))
|
||||
return ""
|
||||
}
|
||||
if chat.Username == "" {
|
||||
t.log.Warn("welcome: chat has no public @username; follow link omitted",
|
||||
zap.String("chat", label), zap.Int64("id", id))
|
||||
return ""
|
||||
}
|
||||
t.log.Info("welcome: resolved follow link",
|
||||
zap.String("chat", label), zap.String("username", chat.Username))
|
||||
return chat.Username
|
||||
}
|
||||
|
||||
// logChatAdminStatus checks, at startup, whether the bot can actually gate the
|
||||
// moderated chat — it must be an administrator there with the restrict-members
|
||||
// ("Ban users") right, or Telegram delivers no chat_member updates and restricts
|
||||
@@ -198,11 +243,19 @@ func (t *Bot) handleStart(ctx context.Context, api *tgbot.Bot, update *models.Up
|
||||
if update.Message.Chat.Type != models.ChatTypePrivate {
|
||||
return
|
||||
}
|
||||
// The sender's Telegram language rides on the message itself (Message.from.language_code
|
||||
// in the Bot API — there is no separate user-update event); fall back to English when it
|
||||
// is absent.
|
||||
lang := ""
|
||||
if update.Message.From != nil {
|
||||
lang = update.Message.From.LanguageCode
|
||||
}
|
||||
text, button := startText(lang, t.channelUsername, t.chatUsername)
|
||||
startParam := startPayload(update.Message.Text)
|
||||
if _, err := api.SendMessage(ctx, &tgbot.SendMessageParams{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
Text: "Tap to open Scrabble.",
|
||||
ReplyMarkup: t.launchMarkup("Open Scrabble", startParam),
|
||||
Text: text,
|
||||
ReplyMarkup: t.launchMarkup(button, startParam),
|
||||
}); err != nil {
|
||||
t.log.Warn("reply to start failed", zap.Error(err))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user