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:
@@ -29,6 +29,10 @@ func (f *fakeBotAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
f.text = r.FormValue("text")
|
||||
f.replyMarkup = r.FormValue("reply_markup")
|
||||
io.WriteString(w, `{"ok":true,"result":{"message_id":1}}`)
|
||||
case strings.HasSuffix(r.URL.Path, "/getChat"):
|
||||
// Echo the requested id into the username so a resolver test can tell the
|
||||
// channel lookup from the chat lookup.
|
||||
io.WriteString(w, `{"ok":true,"result":{"id":-100,"type":"channel","username":"u`+r.FormValue("chat_id")+`"}}`)
|
||||
default:
|
||||
io.WriteString(w, `{"ok":true,"result":true}`)
|
||||
}
|
||||
@@ -105,7 +109,7 @@ func TestTestEnvironmentRoutesGetMe(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHandleStartRepliesPrivateOnly(t *testing.T) {
|
||||
t.Run("private replies", func(t *testing.T) {
|
||||
t.Run("private replies in english by default", func(t *testing.T) {
|
||||
api := &fakeBotAPI{}
|
||||
b := newTestBot(t, api)
|
||||
b.handleStart(context.Background(), b.api, &models.Update{Message: &models.Message{
|
||||
@@ -114,6 +118,35 @@ func TestHandleStartRepliesPrivateOnly(t *testing.T) {
|
||||
if api.chatID != "42" || !strings.Contains(api.replyMarkup, "web_app") {
|
||||
t.Errorf("private /start: chat=%q markup=%q, want a web_app reply", api.chatID, api.replyMarkup)
|
||||
}
|
||||
// No reported language -> English welcome + English button.
|
||||
if !strings.Contains(api.text, "Hi!") {
|
||||
t.Errorf("text = %q, want the English welcome", api.text)
|
||||
}
|
||||
if !strings.Contains(api.replyMarkup, "Open") {
|
||||
t.Errorf("reply_markup = %q, want the English button", api.replyMarkup)
|
||||
}
|
||||
})
|
||||
t.Run("uses the sender's reported language", func(t *testing.T) {
|
||||
api := &fakeBotAPI{}
|
||||
b := newTestBot(t, api)
|
||||
b.handleStart(context.Background(), b.api, &models.Update{Message: &models.Message{
|
||||
Chat: models.Chat{ID: 42, Type: models.ChatTypePrivate}, Text: "/start",
|
||||
From: &models.User{ID: 7, LanguageCode: "ru"},
|
||||
}})
|
||||
if !strings.Contains(api.text, "Привет!") {
|
||||
t.Errorf("text = %q, want the Russian welcome for a ru sender", api.text)
|
||||
}
|
||||
})
|
||||
t.Run("embeds resolved follow handles", func(t *testing.T) {
|
||||
api := &fakeBotAPI{}
|
||||
b := newTestBot(t, api)
|
||||
b.channelUsername, b.chatUsername = "erudit", "erudite_chat"
|
||||
b.handleStart(context.Background(), b.api, &models.Update{Message: &models.Message{
|
||||
Chat: models.Chat{ID: 42, Type: models.ChatTypePrivate}, Text: "/start",
|
||||
}})
|
||||
if !strings.Contains(api.text, "@erudit") || !strings.Contains(api.text, "@erudite_chat") {
|
||||
t.Errorf("text = %q, want the follow handles", api.text)
|
||||
}
|
||||
})
|
||||
t.Run("group ignored", func(t *testing.T) {
|
||||
api := &fakeBotAPI{}
|
||||
@@ -127,6 +160,26 @@ func TestHandleStartRepliesPrivateOnly(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestResolveWelcomeHandles(t *testing.T) {
|
||||
api := &fakeBotAPI{}
|
||||
b := newTestBot(t, api)
|
||||
b.channelID, b.chatID = 111, 222
|
||||
b.resolveWelcomeHandles(context.Background())
|
||||
// The fake echoes the requested id into the username, so each lookup is independent.
|
||||
if b.channelUsername != "u111" {
|
||||
t.Errorf("channelUsername = %q, want u111", b.channelUsername)
|
||||
}
|
||||
if b.chatUsername != "u222" {
|
||||
t.Errorf("chatUsername = %q, want u222", b.chatUsername)
|
||||
}
|
||||
// An unset id resolves to no handle (and makes no getChat call).
|
||||
b.channelID = 0
|
||||
b.resolveWelcomeHandles(context.Background())
|
||||
if b.channelUsername != "" {
|
||||
t.Errorf("channelUsername = %q, want empty for id 0", b.channelUsername)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartPayload(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"/start g123": "g123",
|
||||
|
||||
Reference in New Issue
Block a user