mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-09-12 21:20:07 +00:00
add a new button to telegram bot >> Sorted Traffic Usage Report
This commit is contained in:
parent
bf0f1aabc9
commit
0cebf714da
15 changed files with 87 additions and 1 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"sort"
|
||||||
|
|
||||||
"x-ui/database"
|
"x-ui/database"
|
||||||
"x-ui/database/model"
|
"x-ui/database/model"
|
||||||
|
@ -2025,3 +2026,38 @@ func (s *InboundService) MigrateDB() {
|
||||||
func (s *InboundService) GetOnlineClients() []string {
|
func (s *InboundService) GetOnlineClients() []string {
|
||||||
return p.GetOnlineClients()
|
return p.GetOnlineClients()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (s *InboundService) GetValidEmails(emails []string) ([]string, []string, error) {
|
||||||
|
db := database.GetDB()
|
||||||
|
|
||||||
|
// Step 1: Get ClientTraffic records for emails in the input list
|
||||||
|
var clients []xray.ClientTraffic
|
||||||
|
err := db.Where("email IN ?", emails).Find(&clients).Error
|
||||||
|
if err != nil && err != gorm.ErrRecordNotFound {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: Sort clients by (Up + Down) descending
|
||||||
|
sort.Slice(clients, func(i, j int) bool {
|
||||||
|
return (clients[i].Up + clients[i].Down) > (clients[j].Up + clients[j].Down)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Step 3: Extract sorted valid emails and track found ones
|
||||||
|
validEmails := make([]string, 0, len(clients))
|
||||||
|
found := make(map[string]bool)
|
||||||
|
for _, client := range clients {
|
||||||
|
validEmails = append(validEmails, client.Email)
|
||||||
|
found[client.Email] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 4: Identify emails that were not found in the database
|
||||||
|
extraEmails := make([]string, 0)
|
||||||
|
for _, email := range emails {
|
||||||
|
if !found[email] {
|
||||||
|
extraEmails = append(extraEmails, email)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return validEmails, extraEmails, nil
|
||||||
|
}
|
||||||
|
|
|
@ -1456,7 +1456,38 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
|
||||||
}
|
}
|
||||||
|
|
||||||
t.SendMsgToTgbot(chatId,t.I18nBot("tgbot.messages.FinishProcess"), tu.ReplyKeyboardRemove())
|
t.SendMsgToTgbot(chatId,t.I18nBot("tgbot.messages.FinishProcess"), tu.ReplyKeyboardRemove())
|
||||||
|
case "get_sorted_traffic_usage_report":
|
||||||
|
t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
|
||||||
|
emails, err := t.inboundService.getAllEmails()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation"), tu.ReplyKeyboardRemove())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
valid_emails ,extra_emails, err := t.inboundService.GetValidEmails(emails)
|
||||||
|
|
||||||
|
for _, valid_emails := range valid_emails {
|
||||||
|
traffic, err := t.inboundService.GetClientTrafficByEmail(valid_emails)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warning(err)
|
||||||
|
msg := t.I18nBot("tgbot.wentWrong")
|
||||||
|
t.SendMsgToTgbot(chatId, msg)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if traffic == nil {
|
||||||
|
msg := t.I18nBot("tgbot.noResult")
|
||||||
|
t.SendMsgToTgbot(chatId, msg)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
output := t.clientInfoMsg(traffic, false, false, false, false, true, false)
|
||||||
|
t.SendMsgToTgbot(chatId, output, tu.ReplyKeyboardRemove())
|
||||||
|
}
|
||||||
|
for _, extra_emails := range extra_emails {
|
||||||
|
msg := fmt.Sprintf("📧 %s\n%s", extra_emails, t.I18nBot("tgbot.noResult"))
|
||||||
|
t.SendMsgToTgbot(chatId, msg, tu.ReplyKeyboardRemove())
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1609,6 +1640,9 @@ func checkAdmin(tgId int64) bool {
|
||||||
|
|
||||||
func (t *Tgbot) SendAnswer(chatId int64, msg string, isAdmin bool) {
|
func (t *Tgbot) SendAnswer(chatId int64, msg string, isAdmin bool) {
|
||||||
numericKeyboard := tu.InlineKeyboard(
|
numericKeyboard := tu.InlineKeyboard(
|
||||||
|
tu.InlineKeyboardRow(
|
||||||
|
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.SortedTrafficUsageReport")).WithCallbackData(t.encodeQuery("get_sorted_traffic_usage_report")),
|
||||||
|
),
|
||||||
tu.InlineKeyboardRow(
|
tu.InlineKeyboardRow(
|
||||||
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.serverUsage")).WithCallbackData(t.encodeQuery("get_usage")),
|
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.InlineKeyboardButton(t.I18nBot("tgbot.buttons.ResetAllTraffics")).WithCallbackData(t.encodeQuery("reset_all_traffics")),
|
||||||
|
|
|
@ -655,6 +655,9 @@
|
||||||
"change_password" = "⚙️🔑 كلمة السر"
|
"change_password" = "⚙️🔑 كلمة السر"
|
||||||
"change_email" = "⚙️📧 البريد الإلكتروني"
|
"change_email" = "⚙️📧 البريد الإلكتروني"
|
||||||
"change_comment" = "⚙️💬 تعليق"
|
"change_comment" = "⚙️💬 تعليق"
|
||||||
|
"ResetAllTraffics" = "إعادة ضبط جميع الترافيك"
|
||||||
|
"SortedTrafficUsageReport" = "تقرير استخدام الترافيك المرتب"
|
||||||
|
|
||||||
|
|
||||||
[tgbot.answers]
|
[tgbot.answers]
|
||||||
"successfulOperation" = "✅ العملية نجحت!"
|
"successfulOperation" = "✅ العملية نجحت!"
|
||||||
|
|
|
@ -612,6 +612,7 @@
|
||||||
"FailedResetTraffic" = "📧 Email: {{ .ClientEmail }}\n🏁 Result: ❌ Failed \n\n🛠️ Error: [ {{ .ErrorMessage }} ]"
|
"FailedResetTraffic" = "📧 Email: {{ .ClientEmail }}\n🏁 Result: ❌ Failed \n\n🛠️ Error: [ {{ .ErrorMessage }} ]"
|
||||||
"FinishProcess" = "🔚 Traffic reset process finished for all clients."
|
"FinishProcess" = "🔚 Traffic reset process finished for all clients."
|
||||||
|
|
||||||
|
|
||||||
[tgbot.buttons]
|
[tgbot.buttons]
|
||||||
"closeKeyboard" = "❌ Close Keyboard"
|
"closeKeyboard" = "❌ Close Keyboard"
|
||||||
"cancel" = "❌ Cancel"
|
"cancel" = "❌ Cancel"
|
||||||
|
@ -655,6 +656,7 @@
|
||||||
"change_email" = "⚙️📧 Email"
|
"change_email" = "⚙️📧 Email"
|
||||||
"change_comment" = "⚙️💬 Comment"
|
"change_comment" = "⚙️💬 Comment"
|
||||||
"ResetAllTraffics" = "Reset All Traffics"
|
"ResetAllTraffics" = "Reset All Traffics"
|
||||||
|
"SortedTrafficUsageReport" = "Sorted Traffic Usage Report"
|
||||||
|
|
||||||
[tgbot.answers]
|
[tgbot.answers]
|
||||||
"successfulOperation" = "✅ Operation successful!"
|
"successfulOperation" = "✅ Operation successful!"
|
||||||
|
|
|
@ -658,7 +658,7 @@
|
||||||
"change_email" = "⚙️📧 Correo electrónico"
|
"change_email" = "⚙️📧 Correo electrónico"
|
||||||
"change_comment" = "⚙️💬 Comentario"
|
"change_comment" = "⚙️💬 Comentario"
|
||||||
"ResetAllTraffics" = "Reiniciar todo el tráfico"
|
"ResetAllTraffics" = "Reiniciar todo el tráfico"
|
||||||
|
"SortedTrafficUsageReport" = "Informe de uso de tráfico ordenado"
|
||||||
|
|
||||||
|
|
||||||
[tgbot.answers]
|
[tgbot.answers]
|
||||||
|
|
|
@ -658,6 +658,8 @@
|
||||||
"change_email" = "⚙️📧 ایمیل"
|
"change_email" = "⚙️📧 ایمیل"
|
||||||
"change_comment" = "⚙️💬 نظر"
|
"change_comment" = "⚙️💬 نظر"
|
||||||
"ResetAllTraffics" = "بازنشانی همه ترافیکها"
|
"ResetAllTraffics" = "بازنشانی همه ترافیکها"
|
||||||
|
"SortedTrafficUsageReport" = "گزارش استفاده از ترافیک مرتبشده"
|
||||||
|
|
||||||
|
|
||||||
[tgbot.answers]
|
[tgbot.answers]
|
||||||
"successfulOperation" = "✅ انجام شد!"
|
"successfulOperation" = "✅ انجام شد!"
|
||||||
|
|
|
@ -658,6 +658,7 @@
|
||||||
"change_email" = "⚙️📧 Email"
|
"change_email" = "⚙️📧 Email"
|
||||||
"change_comment" = "⚙️💬 Komentar"
|
"change_comment" = "⚙️💬 Komentar"
|
||||||
"ResetAllTraffics" = "Reset Semua Lalu Lintas"
|
"ResetAllTraffics" = "Reset Semua Lalu Lintas"
|
||||||
|
"SortedTrafficUsageReport" = "Laporan Penggunaan Lalu Lintas yang Terurut"
|
||||||
|
|
||||||
|
|
||||||
[tgbot.answers]
|
[tgbot.answers]
|
||||||
|
|
|
@ -658,6 +658,7 @@
|
||||||
"change_email" = "⚙️📧 メールアドレス"
|
"change_email" = "⚙️📧 メールアドレス"
|
||||||
"change_comment" = "⚙️💬 コメント"
|
"change_comment" = "⚙️💬 コメント"
|
||||||
"ResetAllTraffics" = "すべてのトラフィックをリセット"
|
"ResetAllTraffics" = "すべてのトラフィックをリセット"
|
||||||
|
"SortedTrafficUsageReport" = "ソートされたトラフィック使用レポート"
|
||||||
|
|
||||||
|
|
||||||
[tgbot.answers]
|
[tgbot.answers]
|
||||||
|
|
|
@ -658,6 +658,7 @@
|
||||||
"change_email" = "⚙️📧 E-mail"
|
"change_email" = "⚙️📧 E-mail"
|
||||||
"change_comment" = "⚙️💬 Comentário"
|
"change_comment" = "⚙️💬 Comentário"
|
||||||
"ResetAllTraffics" = "Redefinir Todo o Tráfego"
|
"ResetAllTraffics" = "Redefinir Todo o Tráfego"
|
||||||
|
"SortedTrafficUsageReport" = "Relatório de Uso de Tráfego Ordenado"
|
||||||
|
|
||||||
|
|
||||||
[tgbot.answers]
|
[tgbot.answers]
|
||||||
|
|
|
@ -658,6 +658,7 @@
|
||||||
"change_email" = "⚙️📧 Электронная почта"
|
"change_email" = "⚙️📧 Электронная почта"
|
||||||
"change_comment" = "⚙️💬 Комментарий"
|
"change_comment" = "⚙️💬 Комментарий"
|
||||||
"ResetAllTraffics" = "Сбросить весь трафик"
|
"ResetAllTraffics" = "Сбросить весь трафик"
|
||||||
|
"SortedTrafficUsageReport" = "Отсортированный отчет об использовании трафика"
|
||||||
|
|
||||||
|
|
||||||
[tgbot.answers]
|
[tgbot.answers]
|
||||||
|
|
|
@ -658,6 +658,7 @@
|
||||||
"change_email" = "⚙️📧 E-posta"
|
"change_email" = "⚙️📧 E-posta"
|
||||||
"change_comment" = "⚙️💬 Yorum"
|
"change_comment" = "⚙️💬 Yorum"
|
||||||
"ResetAllTraffics" = "Tüm Trafikleri Sıfırla"
|
"ResetAllTraffics" = "Tüm Trafikleri Sıfırla"
|
||||||
|
"SortedTrafficUsageReport" = "Sıralı Trafik Kullanım Raporu"
|
||||||
|
|
||||||
|
|
||||||
[tgbot.answers]
|
[tgbot.answers]
|
||||||
|
|
|
@ -658,6 +658,7 @@
|
||||||
"change_email" = "⚙️📧 Електронна пошта"
|
"change_email" = "⚙️📧 Електронна пошта"
|
||||||
"change_comment" = "⚙️💬 Коментар"
|
"change_comment" = "⚙️💬 Коментар"
|
||||||
"ResetAllTraffics" = "Скинути весь трафік"
|
"ResetAllTraffics" = "Скинути весь трафік"
|
||||||
|
"SortedTrafficUsageReport" = "Відсортований звіт про використання трафіку"
|
||||||
|
|
||||||
|
|
||||||
[tgbot.answers]
|
[tgbot.answers]
|
||||||
|
|
|
@ -658,6 +658,7 @@
|
||||||
"change_email" = "⚙️📧 Email"
|
"change_email" = "⚙️📧 Email"
|
||||||
"change_comment" = "⚙️💬 Bình Luận"
|
"change_comment" = "⚙️💬 Bình Luận"
|
||||||
"ResetAllTraffics" = "Đặt lại tất cả lưu lượng"
|
"ResetAllTraffics" = "Đặt lại tất cả lưu lượng"
|
||||||
|
"SortedTrafficUsageReport" = "Báo cáo sử dụng lưu lượng đã sắp xếp"
|
||||||
|
|
||||||
|
|
||||||
[tgbot.answers]
|
[tgbot.answers]
|
||||||
|
|
|
@ -658,6 +658,7 @@
|
||||||
"change_email" = "⚙️📧 邮箱"
|
"change_email" = "⚙️📧 邮箱"
|
||||||
"change_comment" = "⚙️💬 评论"
|
"change_comment" = "⚙️💬 评论"
|
||||||
"ResetAllTraffics" = "重置所有流量"
|
"ResetAllTraffics" = "重置所有流量"
|
||||||
|
"SortedTrafficUsageReport" = "排序的流量使用报告"
|
||||||
|
|
||||||
|
|
||||||
[tgbot.answers]
|
[tgbot.answers]
|
||||||
|
|
|
@ -658,6 +658,7 @@
|
||||||
"change_email" = "⚙️📧 電子郵件"
|
"change_email" = "⚙️📧 電子郵件"
|
||||||
"change_comment" = "⚙️💬 評論"
|
"change_comment" = "⚙️💬 評論"
|
||||||
"ResetAllTraffics" = "重設所有流量"
|
"ResetAllTraffics" = "重設所有流量"
|
||||||
|
"SortedTrafficUsageReport" = "排序過的流量使用報告"
|
||||||
|
|
||||||
|
|
||||||
[tgbot.answers]
|
[tgbot.answers]
|
||||||
|
|
Loading…
Reference in a new issue