|
@@ -1,6 +1,7 @@
|
|
package service
|
|
package service
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "context"
|
|
"crypto/rand"
|
|
"crypto/rand"
|
|
"embed"
|
|
"embed"
|
|
"encoding/base64"
|
|
"encoding/base64"
|
|
@@ -148,7 +149,7 @@ func (t *Tgbot) Start(i18nFS embed.FS) error {
|
|
}
|
|
}
|
|
|
|
|
|
// After bot initialization, set up bot commands with localized descriptions
|
|
// After bot initialization, set up bot commands with localized descriptions
|
|
- err = bot.SetMyCommands(&telego.SetMyCommandsParams{
|
|
|
|
|
|
+ err = bot.SetMyCommands(context.Background(), &telego.SetMyCommandsParams{
|
|
Commands: []telego.BotCommand{
|
|
Commands: []telego.BotCommand{
|
|
{Command: "start", Description: t.I18nBot("tgbot.commands.startDesc")},
|
|
{Command: "start", Description: t.I18nBot("tgbot.commands.startDesc")},
|
|
{Command: "help", Description: t.I18nBot("tgbot.commands.helpDesc")},
|
|
{Command: "help", Description: t.I18nBot("tgbot.commands.helpDesc")},
|
|
@@ -221,8 +222,9 @@ func (t *Tgbot) SetHostname() {
|
|
}
|
|
}
|
|
|
|
|
|
func (t *Tgbot) Stop() {
|
|
func (t *Tgbot) Stop() {
|
|
- botHandler.Stop()
|
|
|
|
- bot.StopLongPolling()
|
|
|
|
|
|
+ if botHandler != nil {
|
|
|
|
+ botHandler.Stop()
|
|
|
|
+ }
|
|
logger.Info("Stop Telegram receiver ...")
|
|
logger.Info("Stop Telegram receiver ...")
|
|
isRunning = false
|
|
isRunning = false
|
|
adminIds = nil
|
|
adminIds = nil
|
|
@@ -255,26 +257,29 @@ func (t *Tgbot) OnReceive() {
|
|
Timeout: 10,
|
|
Timeout: 10,
|
|
}
|
|
}
|
|
|
|
|
|
- updates, _ := bot.UpdatesViaLongPolling(¶ms)
|
|
|
|
|
|
+ updates, _ := bot.UpdatesViaLongPolling(context.Background(), ¶ms)
|
|
|
|
|
|
botHandler, _ = th.NewBotHandler(bot, updates)
|
|
botHandler, _ = th.NewBotHandler(bot, updates)
|
|
|
|
|
|
- botHandler.HandleMessage(func(_ *telego.Bot, message telego.Message) {
|
|
|
|
|
|
+ botHandler.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
|
delete(userStates, message.Chat.ID)
|
|
delete(userStates, message.Chat.ID)
|
|
t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.keyboardClosed"), tu.ReplyKeyboardRemove())
|
|
t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.keyboardClosed"), tu.ReplyKeyboardRemove())
|
|
|
|
+ return nil
|
|
}, th.TextEqual(t.I18nBot("tgbot.buttons.closeKeyboard")))
|
|
}, th.TextEqual(t.I18nBot("tgbot.buttons.closeKeyboard")))
|
|
|
|
|
|
- botHandler.HandleMessage(func(_ *telego.Bot, message telego.Message) {
|
|
|
|
|
|
+ botHandler.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
|
delete(userStates, message.Chat.ID)
|
|
delete(userStates, message.Chat.ID)
|
|
t.answerCommand(&message, message.Chat.ID, checkAdmin(message.From.ID))
|
|
t.answerCommand(&message, message.Chat.ID, checkAdmin(message.From.ID))
|
|
|
|
+ return nil
|
|
}, th.AnyCommand())
|
|
}, th.AnyCommand())
|
|
|
|
|
|
- botHandler.HandleCallbackQuery(func(_ *telego.Bot, query telego.CallbackQuery) {
|
|
|
|
|
|
+ botHandler.HandleCallbackQuery(func(ctx *th.Context, query telego.CallbackQuery) error {
|
|
delete(userStates, query.Message.GetChat().ID)
|
|
delete(userStates, query.Message.GetChat().ID)
|
|
t.answerCallback(&query, checkAdmin(query.From.ID))
|
|
t.answerCallback(&query, checkAdmin(query.From.ID))
|
|
|
|
+ return nil
|
|
}, th.AnyCallbackQueryWithMessage())
|
|
}, th.AnyCallbackQueryWithMessage())
|
|
|
|
|
|
- botHandler.HandleMessage(func(_ *telego.Bot, message telego.Message) {
|
|
|
|
|
|
+ botHandler.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
|
if userState, exists := userStates[message.Chat.ID]; exists {
|
|
if userState, exists := userStates[message.Chat.ID]; exists {
|
|
switch userState {
|
|
switch userState {
|
|
case "awaiting_id":
|
|
case "awaiting_id":
|
|
@@ -284,7 +289,7 @@ func (t *Tgbot) OnReceive() {
|
|
inbound, _ := t.inboundService.GetInbound(receiver_inbound_ID)
|
|
inbound, _ := t.inboundService.GetInbound(receiver_inbound_ID)
|
|
message_text, _ := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
|
|
message_text, _ := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
|
|
t.addClient(message.Chat.ID, message_text)
|
|
t.addClient(message.Chat.ID, message_text)
|
|
- return
|
|
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
|
|
|
|
client_Id = strings.TrimSpace(message.Text)
|
|
client_Id = strings.TrimSpace(message.Text)
|
|
@@ -309,7 +314,7 @@ func (t *Tgbot) OnReceive() {
|
|
if client_TrPassword == strings.TrimSpace(message.Text) {
|
|
if client_TrPassword == strings.TrimSpace(message.Text) {
|
|
t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
|
|
t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
|
|
delete(userStates, message.Chat.ID)
|
|
delete(userStates, message.Chat.ID)
|
|
- return
|
|
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
|
|
|
|
client_TrPassword = strings.TrimSpace(message.Text)
|
|
client_TrPassword = strings.TrimSpace(message.Text)
|
|
@@ -334,7 +339,7 @@ func (t *Tgbot) OnReceive() {
|
|
if client_ShPassword == strings.TrimSpace(message.Text) {
|
|
if client_ShPassword == strings.TrimSpace(message.Text) {
|
|
t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
|
|
t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
|
|
delete(userStates, message.Chat.ID)
|
|
delete(userStates, message.Chat.ID)
|
|
- return
|
|
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
|
|
|
|
client_ShPassword = strings.TrimSpace(message.Text)
|
|
client_ShPassword = strings.TrimSpace(message.Text)
|
|
@@ -359,7 +364,7 @@ func (t *Tgbot) OnReceive() {
|
|
if client_Email == strings.TrimSpace(message.Text) {
|
|
if client_Email == strings.TrimSpace(message.Text) {
|
|
t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
|
|
t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
|
|
delete(userStates, message.Chat.ID)
|
|
delete(userStates, message.Chat.ID)
|
|
- return
|
|
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
|
|
|
|
client_Email = strings.TrimSpace(message.Text)
|
|
client_Email = strings.TrimSpace(message.Text)
|
|
@@ -384,7 +389,7 @@ func (t *Tgbot) OnReceive() {
|
|
if client_Comment == strings.TrimSpace(message.Text) {
|
|
if client_Comment == strings.TrimSpace(message.Text) {
|
|
t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
|
|
t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
|
|
delete(userStates, message.Chat.ID)
|
|
delete(userStates, message.Chat.ID)
|
|
- return
|
|
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
|
|
|
|
client_Comment = strings.TrimSpace(message.Text)
|
|
client_Comment = strings.TrimSpace(message.Text)
|
|
@@ -417,6 +422,7 @@ func (t *Tgbot) OnReceive() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ return nil
|
|
}, th.AnyMessage())
|
|
}, th.AnyMessage())
|
|
|
|
|
|
botHandler.Start()
|
|
botHandler.Start()
|
|
@@ -699,7 +705,7 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
|
|
}
|
|
}
|
|
message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
|
|
message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
|
|
|
|
|
|
- t.addClient(chatId, message_text, messageId)
|
|
|
|
|
|
+ t.addClient(callbackQuery.Message.GetChat().ID, message_text, messageId)
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
case "add_client_limit_traffic_in":
|
|
case "add_client_limit_traffic_in":
|
|
if len(dataArray) >= 2 {
|
|
if len(dataArray) >= 2 {
|
|
@@ -914,7 +920,7 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
|
|
}
|
|
}
|
|
message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
|
|
message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
|
|
|
|
|
|
- t.addClient(chatId, message_text, messageId)
|
|
|
|
|
|
+ t.addClient(callbackQuery.Message.GetChat().ID, message_text, messageId)
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
case "add_client_reset_exp_in":
|
|
case "add_client_reset_exp_in":
|
|
if len(dataArray) >= 2 {
|
|
if len(dataArray) >= 2 {
|
|
@@ -1096,7 +1102,7 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
|
|
}
|
|
}
|
|
message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
|
|
message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
|
|
|
|
|
|
- t.addClient(chatId, message_text, messageId)
|
|
|
|
|
|
+ t.addClient(callbackQuery.Message.GetChat().ID, message_text, messageId)
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
|
|
case "add_client_ip_limit_in":
|
|
case "add_client_ip_limit_in":
|
|
if len(dataArray) >= 2 {
|
|
if len(dataArray) >= 2 {
|
|
@@ -1157,8 +1163,6 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
|
|
return
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
|
|
|
|
- t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
|
|
|
|
case "clear_ips":
|
|
case "clear_ips":
|
|
inlineKeyboard := tu.InlineKeyboard(
|
|
inlineKeyboard := tu.InlineKeyboard(
|
|
tu.InlineKeyboardRow(
|
|
tu.InlineKeyboardRow(
|
|
@@ -1285,7 +1289,7 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
|
|
|
|
|
|
message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
|
|
message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
|
|
|
|
|
|
- t.addClient(chatId, message_text)
|
|
|
|
|
|
+ t.addClient(callbackQuery.Message.GetChat().ID, message_text)
|
|
}
|
|
}
|
|
return
|
|
return
|
|
} else {
|
|
} else {
|
|
@@ -1859,7 +1863,7 @@ func (t *Tgbot) SendMsgToTgbot(chatId int64, msg string, replyMarkup ...telego.R
|
|
if len(replyMarkup) > 0 && n == (len(allMessages)-1) {
|
|
if len(replyMarkup) > 0 && n == (len(allMessages)-1) {
|
|
params.ReplyMarkup = replyMarkup[0]
|
|
params.ReplyMarkup = replyMarkup[0]
|
|
}
|
|
}
|
|
- _, err := bot.SendMessage(¶ms)
|
|
|
|
|
|
+ _, err := bot.SendMessage(context.Background(), ¶ms)
|
|
if err != nil {
|
|
if err != nil {
|
|
logger.Warning("Error sending telegram message :", err)
|
|
logger.Warning("Error sending telegram message :", err)
|
|
}
|
|
}
|
|
@@ -2765,7 +2769,7 @@ func (t *Tgbot) sendBackup(chatId int64) {
|
|
tu.ID(chatId),
|
|
tu.ID(chatId),
|
|
tu.File(file),
|
|
tu.File(file),
|
|
)
|
|
)
|
|
- _, err = bot.SendDocument(document)
|
|
|
|
|
|
+ _, err = bot.SendDocument(context.Background(), document)
|
|
if err != nil {
|
|
if err != nil {
|
|
logger.Error("Error in uploading backup: ", err)
|
|
logger.Error("Error in uploading backup: ", err)
|
|
}
|
|
}
|
|
@@ -2779,7 +2783,7 @@ func (t *Tgbot) sendBackup(chatId int64) {
|
|
tu.ID(chatId),
|
|
tu.ID(chatId),
|
|
tu.File(file),
|
|
tu.File(file),
|
|
)
|
|
)
|
|
- _, err = bot.SendDocument(document)
|
|
|
|
|
|
+ _, err = bot.SendDocument(context.Background(), document)
|
|
if err != nil {
|
|
if err != nil {
|
|
logger.Error("Error in uploading config.json: ", err)
|
|
logger.Error("Error in uploading config.json: ", err)
|
|
}
|
|
}
|
|
@@ -2803,7 +2807,7 @@ func (t *Tgbot) sendBanLogs(chatId int64, dt bool) {
|
|
tu.ID(chatId),
|
|
tu.ID(chatId),
|
|
tu.File(file),
|
|
tu.File(file),
|
|
)
|
|
)
|
|
- _, err = bot.SendDocument(document)
|
|
|
|
|
|
+ _, err = bot.SendDocument(context.Background(), document)
|
|
if err != nil {
|
|
if err != nil {
|
|
logger.Error("Error in uploading IPLimitBannedPrevLog: ", err)
|
|
logger.Error("Error in uploading IPLimitBannedPrevLog: ", err)
|
|
}
|
|
}
|
|
@@ -2824,7 +2828,7 @@ func (t *Tgbot) sendBanLogs(chatId int64, dt bool) {
|
|
tu.ID(chatId),
|
|
tu.ID(chatId),
|
|
tu.File(file),
|
|
tu.File(file),
|
|
)
|
|
)
|
|
- _, err = bot.SendDocument(document)
|
|
|
|
|
|
+ _, err = bot.SendDocument(context.Background(), document)
|
|
if err != nil {
|
|
if err != nil {
|
|
logger.Error("Error in uploading IPLimitBannedLog: ", err)
|
|
logger.Error("Error in uploading IPLimitBannedLog: ", err)
|
|
}
|
|
}
|
|
@@ -2842,7 +2846,7 @@ func (t *Tgbot) sendCallbackAnswerTgBot(id string, message string) {
|
|
CallbackQueryID: id,
|
|
CallbackQueryID: id,
|
|
Text: message,
|
|
Text: message,
|
|
}
|
|
}
|
|
- if err := bot.AnswerCallbackQuery(¶ms); err != nil {
|
|
|
|
|
|
+ if err := bot.AnswerCallbackQuery(context.Background(), ¶ms); err != nil {
|
|
logger.Warning(err)
|
|
logger.Warning(err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -2853,7 +2857,7 @@ func (t *Tgbot) editMessageCallbackTgBot(chatId int64, messageID int, inlineKeyb
|
|
MessageID: messageID,
|
|
MessageID: messageID,
|
|
ReplyMarkup: inlineKeyboard,
|
|
ReplyMarkup: inlineKeyboard,
|
|
}
|
|
}
|
|
- if _, err := bot.EditMessageReplyMarkup(¶ms); err != nil {
|
|
|
|
|
|
+ if _, err := bot.EditMessageReplyMarkup(context.Background(), ¶ms); err != nil {
|
|
logger.Warning(err)
|
|
logger.Warning(err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -2868,7 +2872,7 @@ func (t *Tgbot) editMessageTgBot(chatId int64, messageID int, text string, inlin
|
|
if len(inlineKeyboard) > 0 {
|
|
if len(inlineKeyboard) > 0 {
|
|
params.ReplyMarkup = inlineKeyboard[0]
|
|
params.ReplyMarkup = inlineKeyboard[0]
|
|
}
|
|
}
|
|
- if _, err := bot.EditMessageText(¶ms); err != nil {
|
|
|
|
|
|
+ if _, err := bot.EditMessageText(context.Background(), ¶ms); err != nil {
|
|
logger.Warning(err)
|
|
logger.Warning(err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -2881,7 +2885,7 @@ func (t *Tgbot) SendMsgToTgbotDeleteAfter(chatId int64, msg string, delayInSecon
|
|
}
|
|
}
|
|
|
|
|
|
// Send the message
|
|
// Send the message
|
|
- sentMsg, err := bot.SendMessage(&telego.SendMessageParams{
|
|
|
|
|
|
+ sentMsg, err := bot.SendMessage(context.Background(), &telego.SendMessageParams{
|
|
ChatID: tu.ID(chatId),
|
|
ChatID: tu.ID(chatId),
|
|
Text: msg,
|
|
Text: msg,
|
|
ReplyMarkup: replyMarkupParam, // Use the correct replyMarkup value
|
|
ReplyMarkup: replyMarkupParam, // Use the correct replyMarkup value
|
|
@@ -2904,7 +2908,7 @@ func (t *Tgbot) deleteMessageTgBot(chatId int64, messageID int) {
|
|
ChatID: tu.ID(chatId),
|
|
ChatID: tu.ID(chatId),
|
|
MessageID: messageID,
|
|
MessageID: messageID,
|
|
}
|
|
}
|
|
- if err := bot.DeleteMessage(¶ms); err != nil {
|
|
|
|
|
|
+ if err := bot.DeleteMessage(context.Background(), ¶ms); err != nil {
|
|
logger.Warning("Failed to delete message:", err)
|
|
logger.Warning("Failed to delete message:", err)
|
|
} else {
|
|
} else {
|
|
logger.Info("Message deleted successfully")
|
|
logger.Info("Message deleted successfully")
|