feat(tgbot): Allow restart core via telegram bot (#2581)

This commit is contained in:
Ahmad Thoriq Najahi
2024-10-16 19:39:25 +07:00
committed by GitHub
parent 744583b4e7
commit 2ef5ccc2fd
12 changed files with 99 additions and 24 deletions

View File

@@ -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(