Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAhmad Thoriq Najahi <najahi@zephyrus.id>2024-10-16 15:39:25 +0300
committerGitHub <noreply@github.com>2024-10-16 15:39:25 +0300
commit2ef5ccc2fd8bcf8a7056679a22d6097d21d0bad5 (patch)
tree99c00a3805e96162c028d140ac31421275e31b51 /web/service
parent744583b4e7e6458ef6f8b8e6b56a2f9d681598b5 (diff)
feat(tgbot): Allow restart core via telegram bot (#2581)
Diffstat (limited to 'web/service')
-rw-r--r--web/service/tgbot.go55
1 files changed, 43 insertions, 12 deletions
diff --git a/web/service/tgbot.go b/web/service/tgbot.go
index b8294620..449161d3 100644
--- a/web/service/tgbot.go
+++ b/web/service/tgbot.go
@@ -243,7 +243,12 @@ func (t *Tgbot) answerCommand(message *telego.Message, chatId int64, isAdmin boo
command, _, commandArgs := tu.ParseCommand(message.Text)
- // Extract the command from the Message.
+ // Helper function to handle unknown commands.
+ handleUnknownCommand := func() {
+ msg += t.I18nBot("tgbot.commands.unknown")
+ }
+
+ // Handle the command.
switch command {
case "help":
msg += t.I18nBot("tgbot.commands.help")
@@ -266,9 +271,7 @@ func (t *Tgbot) answerCommand(message *telego.Message, chatId int64, isAdmin boo
if isAdmin {
t.searchClient(chatId, commandArgs[0])
} else {
- // Convert message.From.ID to int64
- fromID := int64(message.From.ID)
- t.getClientUsage(chatId, fromID, commandArgs[0])
+ t.getClientUsage(chatId, int64(message.From.ID), commandArgs[0])
}
} else {
msg += t.I18nBot("tgbot.commands.usage")
@@ -278,19 +281,46 @@ func (t *Tgbot) answerCommand(message *telego.Message, chatId int64, isAdmin boo
if isAdmin && len(commandArgs) > 0 {
t.searchInbound(chatId, commandArgs[0])
} else {
- msg += t.I18nBot("tgbot.commands.unknown")
+ handleUnknownCommand()
+ }
+ case "restart":
+ onlyMessage = true
+ if isAdmin {
+ if len(commandArgs) == 0 {
+ msg += t.I18nBot("tgbot.commands.restartUsage")
+ } else if strings.ToLower(commandArgs[0]) == "force" {
+ if t.xrayService.IsXrayRunning() {
+ err := t.xrayService.RestartXray(true)
+ if err != nil {
+ msg += t.I18nBot("tgbot.commands.restartFailed", "Error=="+err.Error())
+ } else {
+ msg += t.I18nBot("tgbot.commands.restartSuccess")
+ }
+ } else {
+ msg += t.I18nBot("tgbot.commands.xrayNotRunning")
+ }
+ } else {
+ handleUnknownCommand()
+ msg += t.I18nBot("tgbot.commands.restartUsage")
+ }
+ } else {
+ handleUnknownCommand()
}
default:
- msg += t.I18nBot("tgbot.commands.unknown")
+ handleUnknownCommand()
}
if msg != "" {
- if onlyMessage {
- t.SendMsgToTgbot(chatId, msg)
- return
- } else {
- t.SendAnswer(chatId, msg, isAdmin)
- }
+ t.sendResponse(chatId, msg, onlyMessage, isAdmin)
+ }
+}
+
+// Helper function to send the message based on 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)
}
}
@@ -872,6 +902,7 @@ func (t *Tgbot) SendAnswer(chatId int64, msg string, isAdmin bool) {
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.onlines")).WithCallbackData(t.encodeQuery("onlines")),
tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.allClients")).WithCallbackData(t.encodeQuery("get_inbounds")),
),
+ // TODOOOOOOOOOOOOOO: Add restart button here.
)
numericKeyboardClient := tu.InlineKeyboard(
tu.InlineKeyboardRow(