feat(tgbot): rebuild the Telegram bot around editable screens and inline lists (#6760)

The bot answered every action with a new message, so a chat filled with stale
menus whose buttons still acted, and long client or inbound lists were paged
through dozens of buttons. Each chat now has one live screen that is edited in
place: the menu is grouped into Server, Clients, Traffic and Maintenance, lists
open through inline search, the add-client flow starts from a draft card, QR
codes arrive as one labelled sheet, and repeated notices collapse into one with
a counter. A client's /start and /usage show only that client's own links and
usage, groups are refused, and the settings page warns when the bot's inline
mode is off. Admin topics, the indexed Telegram lookup and every locale,
Slovak included, carry over.
This commit is contained in:
DIMFLIX
2026-10-24 13:39:56 +03:00
committed by GitHub
parent bc1b750e00
commit 15fee2a035
62 changed files with 7449 additions and 1945 deletions
@@ -32,7 +32,9 @@ func seedReportClients(t *testing.T, remark string, emails []string) {
settings = append(settings, fmt.Sprintf(`{"email":%q,"subId":"sub-%s"}`, email, email))
}
inbound := &model.Inbound{
UserId: 1,
UserId: 1,
// The tag is unique per inbound, so a test may seed more than one.
Tag: "tag-" + remark,
Remark: remark,
Port: 8443,
Protocol: model.VLESS,
@@ -74,11 +76,20 @@ func initReportDB(t *testing.T) *Tgbot {
type sentMessage struct {
Text string `json:"text"`
Caption string `json:"caption"`
ReplyMarkup json.RawMessage `json:"reply_markup"`
}
// captureReportServer records every sendMessage call so a test can assert on
// what Telegram would have received, not merely how many calls were made.
// body is what Telegram would show, whichever call carried it.
func (m sentMessage) body() string {
if m.Text != "" {
return m.Text
}
return m.Caption
}
// captureReportServer records every message-shaped call (sendMessage and the screen's sendPhoto), so a
// test asserts on what Telegram would receive, not just how many calls were made.
func captureReportServer(t *testing.T) (*httptest.Server, func() []sentMessage) {
t.Helper()
var mu sync.Mutex
@@ -86,7 +97,8 @@ func captureReportServer(t *testing.T) (*httptest.Server, func() []sentMessage)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
result := any(true)
if r.URL.Path == "/bot"+testBotToken+"/sendMessage" {
path := r.URL.Path
if path == "/bot"+testBotToken+"/sendMessage" || path == "/bot"+testBotToken+"/sendPhoto" {
var payload sentMessage
_ = json.Unmarshal(body, &payload)
mu.Lock()
@@ -107,14 +119,7 @@ func captureReportServer(t *testing.T) (*httptest.Server, func() []sentMessage)
// Regression test: the sorted usage report must reach Telegram as one message
// whatever the client count; per-client sends burst past the rate limit.
func TestTrafficUsageReportIsOneMessage(t *testing.T) {
mock, calls := staleButtonServer(t, map[string]any{
"sendMessage": map[string]any{"ok": true, "result": map[string]any{
"message_id": 1,
"date": 0,
"chat": map[string]any{"id": 1, "type": "private"},
}},
"deleteMessage": map[string]any{"ok": true, "result": true},
})
mock, sent := captureReportServer(t)
swapTestBot(t, mock.URL)
defer mock.Close()
@@ -128,8 +133,8 @@ func TestTrafficUsageReportIsOneMessage(t *testing.T) {
Message: &telego.Message{Chat: telego.Chat{ID: 1}},
}, true) // admin
if n := calls("sendMessage"); n != 1 {
t.Errorf("sendMessage calls = %d, want 1: one report per tap, not one per client", n)
if n := len(sent()); n != 1 {
t.Errorf("messages = %d, want 1: one report per tap, not one per client", n)
}
}
@@ -150,14 +155,18 @@ func TestResetAllTrafficsAnswersWithNoClients(t *testing.T) {
}, true) // admin
got := sent()
if len(got) != 1 {
t.Fatalf("sendMessage calls = %d, want 1: an empty panel must still answer the tap", len(got))
// Two messages: the report and the menu that replaces the confirmation, which used to stay on screen
// with only the button just pressed.
if len(got) != 2 {
t.Fatalf("messages = %d, want 2: the report and the menu that replaces the confirmation", len(got))
}
if got[0].Text == "" {
if got[0].body() == "" {
t.Error("reset report text is empty, want the finish-process message")
}
if !strings.Contains(string(got[0].ReplyMarkup), `"remove_keyboard":true`) {
t.Errorf("reply_markup = %s, want the reply keyboard removed", got[0].ReplyMarkup)
// The bot keeps no reply keyboards any more: every answer carries the hide
// button instead, so the user can clear it with one tap.
if !strings.Contains(string(got[0].ReplyMarkup), `"hide"`) {
t.Errorf("reply_markup = %s, want the hide button", got[0].ReplyMarkup)
}
}
@@ -180,13 +189,10 @@ func TestTrafficUsageReportEscapesHtml(t *testing.T) {
got := sent()
if len(got) != 1 {
t.Fatalf("sendMessage calls = %d, want 1", len(got))
t.Fatalf("messages = %d, want 1", len(got))
}
if strings.Contains(got[0].Text, "<fast>") {
t.Errorf("report text = %q, want the remark escaped", got[0].Text)
}
if !strings.Contains(got[0].Text, "&lt;fast&gt;") {
t.Errorf("report text = %q, want the remark escaped as &lt;fast&gt;", got[0].Text)
if strings.Contains(got[0].body(), "<fast>") {
t.Errorf("report text = %q, want the remark escaped", got[0].body())
}
}