mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-18 11:05:02 +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:
@@ -12,71 +12,6 @@ import (
|
||||
tu "github.com/mymmrac/telego/telegoutil"
|
||||
)
|
||||
|
||||
// sendResponse sends the response message based on the onlyMessage flag.
|
||||
func (t *Tgbot) sendResponse(chatId int64, msg string, onlyMessage, isAdmin bool) {
|
||||
if onlyMessage {
|
||||
t.SendMsgToTgbot(chatId, msg)
|
||||
} else {
|
||||
t.SendAnswer(chatId, msg, isAdmin)
|
||||
}
|
||||
}
|
||||
|
||||
// SendAnswer sends a response message with an inline keyboard to the specified chat.
|
||||
func (t *Tgbot) SendAnswer(chatId int64, msg string, isAdmin bool) {
|
||||
numericKeyboard := tu.InlineKeyboard(
|
||||
tu.InlineKeyboardRow(
|
||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.SortedTrafficUsageReport")).WithCallbackData(t.encodeQuery("get_sorted_traffic_usage_report")),
|
||||
),
|
||||
tu.InlineKeyboardRow(
|
||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.serverUsage")).WithCallbackData(t.encodeQuery("get_usage")),
|
||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.ResetAllTraffics")).WithCallbackData(t.encodeQuery("reset_all_traffics")),
|
||||
),
|
||||
tu.InlineKeyboardRow(
|
||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.dbBackup")).WithCallbackData(t.encodeQuery("get_backup")),
|
||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.getBanLogs")).WithCallbackData(t.encodeQuery("get_banlogs")),
|
||||
),
|
||||
tu.InlineKeyboardRow(
|
||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.getInbounds")).WithCallbackData(t.encodeQuery("inbounds")),
|
||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.depleteSoon")).WithCallbackData(t.encodeQuery("deplete_soon")),
|
||||
),
|
||||
tu.InlineKeyboardRow(
|
||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.commands")).WithCallbackData(t.encodeQuery("commands")),
|
||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.onlines")).WithCallbackData(t.encodeQuery("onlines")),
|
||||
),
|
||||
tu.InlineKeyboardRow(
|
||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.allClients")).WithCallbackData(t.encodeQuery("get_inbounds")),
|
||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.addClient")).WithCallbackData(t.encodeQuery("add_client")),
|
||||
),
|
||||
tu.InlineKeyboardRow(
|
||||
tu.InlineKeyboardButton(t.I18nBot("pages.settings.subSettings")).WithCallbackData(t.encodeQuery("admin_client_sub_links")),
|
||||
tu.InlineKeyboardButton(t.I18nBot("subscription.individualLinks")).WithCallbackData(t.encodeQuery("admin_client_individual_links")),
|
||||
tu.InlineKeyboardButton(t.I18nBot("qrCode")).WithCallbackData(t.encodeQuery("admin_client_qr_links")),
|
||||
),
|
||||
// TODOOOOOOOOOOOOOO: Add restart button here.
|
||||
)
|
||||
numericKeyboardClient := tu.InlineKeyboard(
|
||||
tu.InlineKeyboardRow(
|
||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.clientUsage")).WithCallbackData(t.encodeQuery("client_traffic")),
|
||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.commands")).WithCallbackData(t.encodeQuery("client_commands")),
|
||||
),
|
||||
tu.InlineKeyboardRow(
|
||||
tu.InlineKeyboardButton(t.I18nBot("pages.settings.subSettings")).WithCallbackData(t.encodeQuery("client_sub_links")),
|
||||
tu.InlineKeyboardButton(t.I18nBot("subscription.individualLinks")).WithCallbackData(t.encodeQuery("client_individual_links")),
|
||||
),
|
||||
tu.InlineKeyboardRow(
|
||||
tu.InlineKeyboardButton(t.I18nBot("qrCode")).WithCallbackData(t.encodeQuery("client_qr_links")),
|
||||
),
|
||||
)
|
||||
|
||||
var ReplyMarkup telego.ReplyMarkup
|
||||
if isAdmin {
|
||||
ReplyMarkup = numericKeyboard
|
||||
} else {
|
||||
ReplyMarkup = numericKeyboardClient
|
||||
}
|
||||
t.SendMsgToTgbot(chatId, msg, ReplyMarkup)
|
||||
}
|
||||
|
||||
const telegramPageLimit = 2000
|
||||
|
||||
func pageMessage(message string, limit int) []string {
|
||||
@@ -107,8 +42,18 @@ func splitMessageLines(block string, limit int) []string {
|
||||
}
|
||||
|
||||
lines := strings.Split(block, "\r\n")
|
||||
pages := []string{lines[0]}
|
||||
for _, line := range lines[1:] {
|
||||
pages := []string{}
|
||||
for _, line := range lines {
|
||||
// A single link can exceed a caption on its own; Telegram counts bytes,
|
||||
// so such a line is split rather than kept whole and rejected.
|
||||
for len(line) > limit {
|
||||
pages = append(pages, line[:limit])
|
||||
line = line[limit:]
|
||||
}
|
||||
if len(pages) == 0 {
|
||||
pages = append(pages, line)
|
||||
continue
|
||||
}
|
||||
last := len(pages) - 1
|
||||
if len(pages[last])+len("\r\n")+len(line) > limit {
|
||||
pages = append(pages, line)
|
||||
@@ -116,19 +61,24 @@ func splitMessageLines(block string, limit int) []string {
|
||||
}
|
||||
pages[last] += "\r\n" + line
|
||||
}
|
||||
if len(pages) == 0 {
|
||||
pages = append(pages, "")
|
||||
}
|
||||
return pages
|
||||
}
|
||||
|
||||
// SendMsgToTgbot sends a message to the Telegram bot with optional reply markup.
|
||||
func (t *Tgbot) SendMsgToTgbot(chatId int64, msg string, replyMarkup ...telego.ReplyMarkup) {
|
||||
// SendMsgToTgbot sends a message with optional reply markup and returns the id
|
||||
// of the last one delivered, or 0; only the broadcast prompt needs it.
|
||||
func (t *Tgbot) SendMsgToTgbot(chatId int64, msg string, replyMarkup ...telego.ReplyMarkup) int {
|
||||
if !t.IsRunning() {
|
||||
return
|
||||
return 0
|
||||
}
|
||||
|
||||
if msg == "" {
|
||||
logger.Info("[tgbot] message is empty!")
|
||||
return
|
||||
return 0
|
||||
}
|
||||
lastID := 0
|
||||
|
||||
allMessages := pageMessage(msg, telegramPageLimit)
|
||||
for n, message := range allMessages {
|
||||
@@ -147,10 +97,13 @@ func (t *Tgbot) SendMsgToTgbot(chatId int64, msg string, replyMarkup ...telego.R
|
||||
maxRetries := 3
|
||||
for attempt := range maxRetries {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
_, err := bot.SendMessage(ctx, ¶ms)
|
||||
sent, err := liveBot().SendMessage(ctx, ¶ms)
|
||||
cancel()
|
||||
|
||||
if err == nil {
|
||||
if sent != nil {
|
||||
lastID = sent.MessageID
|
||||
}
|
||||
break // Success
|
||||
}
|
||||
|
||||
@@ -177,6 +130,7 @@ func (t *Tgbot) SendMsgToTgbot(chatId int64, msg string, replyMarkup ...telego.R
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
return lastID
|
||||
}
|
||||
|
||||
// SendMsgToTgbotAdmins sends a message to all admin Telegram chats.
|
||||
@@ -199,7 +153,7 @@ func (t *Tgbot) sendCallbackAnswerTgBot(id string, message string) {
|
||||
CallbackQueryID: id,
|
||||
Text: message,
|
||||
}
|
||||
if err := bot.AnswerCallbackQuery(context.Background(), ¶ms); err != nil {
|
||||
if err := liveBot().AnswerCallbackQuery(context.Background(), ¶ms); err != nil {
|
||||
logger.Warning(err)
|
||||
}
|
||||
}
|
||||
@@ -211,7 +165,7 @@ func (t *Tgbot) editMessageCallbackTgBot(chatId int64, messageID int, inlineKeyb
|
||||
MessageID: messageID,
|
||||
ReplyMarkup: inlineKeyboard,
|
||||
}
|
||||
if _, err := bot.EditMessageReplyMarkup(context.Background(), ¶ms); err != nil {
|
||||
if _, err := liveBot().EditMessageReplyMarkup(context.Background(), ¶ms); err != nil {
|
||||
if isTelegramNotModifiedError(err) {
|
||||
logger.Debug("Telegram reply markup unchanged, skipping edit")
|
||||
return
|
||||
@@ -231,7 +185,7 @@ func (t *Tgbot) editMessageTgBot(chatId int64, messageID int, text string, inlin
|
||||
if len(inlineKeyboard) > 0 {
|
||||
params.ReplyMarkup = inlineKeyboard[0]
|
||||
}
|
||||
if _, err := bot.EditMessageText(context.Background(), ¶ms); err != nil {
|
||||
if _, err := liveBot().EditMessageText(context.Background(), ¶ms); err != nil {
|
||||
if isTelegramNotModifiedError(err) {
|
||||
logger.Debug("Telegram message text unchanged, skipping edit")
|
||||
return
|
||||
@@ -251,50 +205,24 @@ func isTelegramNotModifiedError(err error) bool {
|
||||
strings.Contains(errStr, "No fields to modify")
|
||||
}
|
||||
|
||||
// SendMsgToTgbotDeleteAfter sends a message and deletes it after a specified delay.
|
||||
func (t *Tgbot) SendMsgToTgbotDeleteAfter(chatId int64, msg string, delayInSeconds int, replyMarkup ...telego.ReplyMarkup) {
|
||||
// Determine if replyMarkup was passed; otherwise, set it to nil
|
||||
var replyMarkupParam telego.ReplyMarkup
|
||||
if len(replyMarkup) > 0 {
|
||||
replyMarkupParam = replyMarkup[0] // Use the first element
|
||||
// sendPagedNotice posts one notice per page, each dismissable. Telegram rejects
|
||||
// a message over 4096 characters whole, so a per-client report has to be split.
|
||||
func (t *Tgbot) sendPagedNotice(chatID int64, text string) {
|
||||
for _, chunk := range pageMessage(text, telegramPageLimit) {
|
||||
t.sendNoticeReturningID(chatID, chunk)
|
||||
}
|
||||
|
||||
// Send the message
|
||||
sentMsg, err := bot.SendMessage(context.Background(), &telego.SendMessageParams{
|
||||
ChatID: tu.ID(chatId),
|
||||
MessageThreadID: adminTopic(chatId),
|
||||
Text: msg,
|
||||
ReplyMarkup: replyMarkupParam, // Use the correct replyMarkup value
|
||||
})
|
||||
if err != nil {
|
||||
logger.Warning("Failed to send message:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete the sent message after the specified number of seconds.
|
||||
go t.deleteMessageAfterDelay(chatId, sentMsg.MessageID, delayInSeconds)
|
||||
}
|
||||
|
||||
// deleteMessageAfterDelay waits delayInSeconds and then removes the message. It
|
||||
// deliberately does not touch the conversation state: every caller that ends a
|
||||
// wizard step already clears the state synchronously, and clearing it here — up
|
||||
// to several seconds later — would wipe a state the user set for the next step
|
||||
// in the meantime, silently dropping their following input.
|
||||
func (t *Tgbot) deleteMessageAfterDelay(chatId int64, messageID, delayInSeconds int) {
|
||||
time.Sleep(time.Duration(delayInSeconds) * time.Second)
|
||||
t.deleteMessageTgBot(chatId, messageID)
|
||||
}
|
||||
|
||||
// deleteMessageTgBot deletes a message from the chat.
|
||||
func (t *Tgbot) deleteMessageTgBot(chatId int64, messageID int) {
|
||||
if bot == nil {
|
||||
if liveBot() == nil {
|
||||
return
|
||||
}
|
||||
params := telego.DeleteMessageParams{
|
||||
ChatID: tu.ID(chatId),
|
||||
MessageID: messageID,
|
||||
}
|
||||
if err := bot.DeleteMessage(context.Background(), ¶ms); err != nil {
|
||||
if err := liveBot().DeleteMessage(context.Background(), ¶ms); err != nil {
|
||||
logger.Warning("Failed to delete message:", err)
|
||||
} else {
|
||||
logger.Info("Message deleted successfully")
|
||||
@@ -303,9 +231,7 @@ func (t *Tgbot) deleteMessageTgBot(chatId int64, messageID int) {
|
||||
|
||||
// TestConnection verifies the bot token is valid and the API is reachable.
|
||||
func (t *Tgbot) TestConnection() error {
|
||||
tgBotMutex.Lock()
|
||||
b := bot
|
||||
tgBotMutex.Unlock()
|
||||
b := liveBot()
|
||||
if b == nil {
|
||||
return fmt.Errorf("bot not initialized")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user