Browse Source

feat(tgbot): Add refresh button to server usage (#2253)

- Added a refresh button to the server usage interface to allow users to refresh the displayed information.
 - Updated the sendReport function to use the sendServerUsage function instead of getServerUsage to ensure consistency in functionality.

Signed-off-by: Ahmad Thoriq Najahi <[email protected]>
Ahmad Thoriq Najahi 10 months ago
parent
commit
73a19a45d7
1 changed files with 27 additions and 3 deletions
  1. 27 3
      web/service/tgbot.go

+ 27 - 3
web/service/tgbot.go

@@ -770,7 +770,10 @@ func (t *Tgbot) asnwerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
 	switch callbackQuery.Data {
 	case "get_usage":
 		t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.serverUsage"))
-		t.SendMsgToTgbot(chatId, t.getServerUsage())
+		t.getServerUsage(chatId)
+	case "usage_refresh":
+		t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
+		t.getServerUsage(chatId, callbackQuery.Message.GetMessageID())
 	case "inbounds":
 		t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.getInbounds"))
 		t.SendMsgToTgbot(chatId, t.getInboundUsages())
@@ -916,7 +919,7 @@ func (t *Tgbot) SendReport() {
 		t.SendMsgToTgbotAdmins(msg)
 	}
 
-	info := t.getServerUsage()
+	info := t.sendServerUsage()
 	t.SendMsgToTgbotAdmins(info)
 
 	t.sendExhaustedToAdmins()
@@ -946,7 +949,28 @@ func (t *Tgbot) sendExhaustedToAdmins() {
 	}
 }
 
-func (t *Tgbot) getServerUsage() string {
+func (t *Tgbot) getServerUsage(chatId int64, messageID ...int) string {
+	info := t.prepareServerUsageInfo()
+
+	keyboard := tu.InlineKeyboard(tu.InlineKeyboardRow(
+		tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("usage_refresh"))))
+
+	if len(messageID) > 0 {
+		t.editMessageTgBot(chatId, messageID[0], info, keyboard)
+	} else {
+		t.SendMsgToTgbot(chatId, info, keyboard)
+	}
+
+	return info
+}
+
+// Send server usage without an inline keyborad
+func (t *Tgbot) sendServerUsage() string {
+	info := t.prepareServerUsageInfo()
+	return info
+}
+
+func (t *Tgbot) prepareServerUsageInfo() string {
 	info, ipv4, ipv6 := "", "", ""
 
 	// get latest status of server