From de8c80597ffa66382db3726f610c0e19287c87c4 Mon Sep 17 00:00:00 2001 From: Rizvan Nukhtarov <34610650+nukhtarov@users.noreply.github.com> Date: Mon, 19 Aug 2024 00:30:56 +0300 Subject: [PATCH] New - TGbot, "All clients" button (#2493) --- web/service/tgbot.go | 100 +++++++++++++++++++++++++++ web/translation/translate.en_US.toml | 4 ++ web/translation/translate.ru_RU.toml | 4 ++ 3 files changed, 108 insertions(+) diff --git a/web/service/tgbot.go b/web/service/tgbot.go index 640a2032..b8294620 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -2,6 +2,7 @@ package service import ( "embed" + "errors" "fmt" "net" "net/url" @@ -769,8 +770,40 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool } else { t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation")) } + case "get_clients": + inboundId := dataArray[1] + inboundIdInt, err := strconv.Atoi(inboundId) + if err != nil { + t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error()) + return + } + inbound, err := t.inboundService.GetInbound(inboundIdInt) + if err != nil { + t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error()) + return + } + clients, err := t.getInboundClients(inboundIdInt) + if err != nil { + t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error()) + return + } + t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseClient", "Inbound=="+inbound.Remark), clients) + } return + } else { + switch callbackQuery.Data { + case "get_inbounds": + inbounds, err := t.getInbounds() + if err != nil { + t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error()) + return + + } + t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.allClients")) + t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseInbound"), inbounds) + } + } } @@ -837,6 +870,7 @@ func (t *Tgbot) SendAnswer(chatId int64, msg string, isAdmin bool) { 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.InlineKeyboardButton(t.I18nBot("tgbot.buttons.allClients")).WithCallbackData(t.encodeQuery("get_inbounds")), ), ) numericKeyboardClient := tu.InlineKeyboard( @@ -1082,6 +1116,72 @@ func (t *Tgbot) getInboundUsages() string { return info } +func (t *Tgbot) getInbounds() (*telego.InlineKeyboardMarkup, error) { + inbounds, err := t.inboundService.GetAllInbounds() + var buttons []telego.InlineKeyboardButton + + if err != nil { + logger.Warning("GetAllInbounds run failed:", err) + return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed")) + } else { + if len(inbounds) > 0 { + for _, inbound := range inbounds { + status := "❌" + if inbound.Enable { + status = "✅" + } + buttons = append(buttons, tu.InlineKeyboardButton(fmt.Sprintf("%v - %v", inbound.Remark, status)).WithCallbackData(t.encodeQuery("get_clients "+strconv.Itoa(inbound.Id)))) + } + } else { + logger.Warning("GetAllInbounds run failed:", err) + return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed")) + } + + } + cols := 0 + if len(buttons) < 6 { + cols = 3 + } else { + cols = 2 + } + keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...)) + return keyboard, nil +} + +func (t *Tgbot) getInboundClients(id int) (*telego.InlineKeyboardMarkup, error) { + inbound, err := t.inboundService.GetInbound(id) + if err != nil { + logger.Warning("getIboundClients run failed:", err) + return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed")) + } + clients, err := t.inboundService.GetClients(inbound) + var buttons []telego.InlineKeyboardButton + + if err != nil { + logger.Warning("GetInboundClients run failed:", err) + return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed")) + } else { + if len(clients) > 0 { + for _, client := range clients { + buttons = append(buttons, tu.InlineKeyboardButton(client.Email).WithCallbackData(t.encodeQuery("client_get_usage "+client.Email))) + } + + } else { + return nil, errors.New(t.I18nBot("tgbot.answers.getClientsFailed")) + } + + } + cols := 0 + if len(buttons) < 6 { + cols = 3 + } else { + cols = 2 + } + keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...)) + + return keyboard, nil +} + func (t *Tgbot) clientInfoMsg( traffic *xray.ClientTraffic, printEnabled bool, diff --git a/web/translation/translate.en_US.toml b/web/translation/translate.en_US.toml index 34e3d00e..27022405 100644 --- a/web/translation/translate.en_US.toml +++ b/web/translation/translate.en_US.toml @@ -618,11 +618,13 @@ "confirmNumberAdd" = "✅ Confirm adding: {{ .Num }}" "limitTraffic" = "🚧 Traffic Limit" "getBanLogs" = "Get Ban Logs" +"allClients" = "All Clients" [tgbot.answers] "successfulOperation" = "✅ Operation successful!" "errorOperation" = "❗ Error in operation." "getInboundsFailed" = "❌ Failed to get inbounds." +"getClientsFailed" = "❌ Failed to get clients." "canceled" = "❌ {{ .Email }}: Operation canceled." "clientRefreshSuccess" = "✅ {{ .Email }}: Client refreshed successfully." "IpRefreshSuccess" = "✅ {{ .Email }}: IPs refreshed successfully." @@ -638,3 +640,5 @@ "enableSuccess" = "✅ {{ .Email }}: Enabled successfully." "disableSuccess" = "✅ {{ .Email }}: Disabled successfully." "askToAddUserId" = "Your configuration is not found!\r\nPlease ask your admin to use your Telegram ID in your configuration(s).\r\n\r\nYour User ID: {{ .TgUserID }}" +"chooseClient" = "Choose a Client for Inbound {{ .Inbound }}" +"chooseInbound" = "Choose an Inbound" diff --git a/web/translation/translate.ru_RU.toml b/web/translation/translate.ru_RU.toml index 462317bc..4df4b268 100644 --- a/web/translation/translate.ru_RU.toml +++ b/web/translation/translate.ru_RU.toml @@ -618,11 +618,13 @@ "confirmNumberAdd" = "✅ Подтвердить добавление: {{ .Num }}" "limitTraffic" = "🚧 Лимит трафика" "getBanLogs" = "Логи блокировок" +"allClients" = "Все клиенты" [tgbot.answers] "successfulOperation" = "✅ Успешный!" "errorOperation" = "❗ Ошибка в операции." "getInboundsFailed" = "❌ Не удалось получить входящие потоки." +"getClientsFailed" = "❌ Не удалось получить клиентов." "canceled" = "❌ {{ .Email }}: Операция отменена." "clientRefreshSuccess" = "✅ {{ .Email }}: Клиент успешно обновлен." "IpRefreshSuccess" = "✅ {{ .Email }}: IP-адреса успешно обновлены." @@ -638,3 +640,5 @@ "enableSuccess" = "✅ {{ .Email }}: Включено успешно." "disableSuccess" = "✅ {{ .Email }}: Отключено успешно." "askToAddUserId" = "Ваша конфигурация не найдена!\r\nПожалуйста, попросите администратора использовать ваш идентификатор пользователя Telegram в ваших конфигурациях.\r\n\r\nВаш идентификатор пользователя: {{ .TgUserID }}" +"chooseClient" = "Выберите пользователя для подключения {{ .Inbound }}" +"chooseInbound" = "Выберите подключение"