mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-18 19:15:03 +03:00
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:
@@ -31,6 +31,9 @@ import (
|
||||
|
||||
var (
|
||||
bot *telego.Bot
|
||||
// botMu guards bot: EnsureBot may replace it while a background goroutine
|
||||
// (the screen artwork warm-up) is still using the previous liveBot().
|
||||
botMu sync.RWMutex
|
||||
|
||||
// botCancel stores the function to cancel the context, stopping Long Polling gracefully.
|
||||
botCancel context.CancelFunc
|
||||
@@ -66,6 +69,13 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
// liveBot is the current connection, or nil before login and after logout.
|
||||
func liveBot() *telego.Bot {
|
||||
botMu.RLock()
|
||||
defer botMu.RUnlock()
|
||||
return bot
|
||||
}
|
||||
|
||||
// clientDraft is one chat's add-client wizard state. Per-protocol secrets are
|
||||
// filled per-inbound on submit, so only the universal fields live here.
|
||||
type clientDraft struct {
|
||||
@@ -346,13 +356,16 @@ func (t *Tgbot) Start(i18nFS embed.FS) error {
|
||||
}
|
||||
|
||||
// Create new Telegram bot instance
|
||||
botMu.Lock()
|
||||
bot, err = t.NewBot(tgBotToken, tgBotProxy, tgBotAPIServer)
|
||||
botMu.Unlock()
|
||||
if err != nil {
|
||||
logger.Error("Failed to initialize Telegram bot API:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
t.trySetBotCommands(bot)
|
||||
t.resolveScreenArt()
|
||||
t.trySetBotCommands(liveBot())
|
||||
|
||||
// Start receiving Telegram bot messages
|
||||
tgBotMutex.Lock()
|
||||
@@ -373,17 +386,11 @@ func (t *Tgbot) trySetBotCommands(bot *telego.Bot) {
|
||||
}
|
||||
}()
|
||||
|
||||
// Only /start is advertised: the rest of the actions live on buttons, and
|
||||
// the old list merely duplicated the menu. The commands still work.
|
||||
err := bot.SetMyCommands(context.Background(), &telego.SetMyCommandsParams{
|
||||
Commands: []telego.BotCommand{
|
||||
{Command: "start", Description: t.I18nBot("tgbot.commands.startDesc")},
|
||||
{Command: "help", Description: t.I18nBot("tgbot.commands.helpDesc")},
|
||||
{Command: "status", Description: t.I18nBot("tgbot.commands.statusDesc")},
|
||||
{Command: "id", Description: t.I18nBot("tgbot.commands.idDesc")},
|
||||
{Command: "usage", Description: t.I18nBot("tgbot.commands.usageDesc")},
|
||||
{Command: "inbound", Description: t.I18nBot("tgbot.commands.inboundDesc")},
|
||||
{Command: "restart", Description: t.I18nBot("tgbot.commands.restartDesc")},
|
||||
{Command: "clearall", Description: t.I18nBot("tgbot.commands.clearallDesc")},
|
||||
{Command: "broadcast", Description: t.I18nBot("tgbot.commands.broadcastDesc")},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
@@ -551,7 +558,7 @@ func inAdminTopic(document *telego.SendDocumentParams) *telego.SendDocumentParam
|
||||
return document
|
||||
}
|
||||
|
||||
// SetHostname sets the hostname for the bot.
|
||||
// SetHostname sets the hostname for the liveBot().
|
||||
func (t *Tgbot) SetHostname() {
|
||||
host, err := os.Hostname()
|
||||
if err != nil {
|
||||
@@ -594,7 +601,7 @@ func StopBot() {
|
||||
}
|
||||
|
||||
if cancel != nil {
|
||||
logger.Info("Sending cancellation signal to Telegram bot...")
|
||||
logger.Info("Sending cancellation signal to Telegram liveBot()...")
|
||||
// Cancels the context passed to UpdatesViaLongPolling; this closes updates channel
|
||||
// and lets botHandler.Start() exit cleanly.
|
||||
cancel()
|
||||
|
||||
Reference in New Issue
Block a user