From c22ee1ac600f96e7ba63e68277db9873dc9d321c Mon Sep 17 00:00:00 2001 From: mjef Date: Fri, 7 Apr 2023 15:40:42 +0200 Subject: [PATCH] Do not show renew btn in the bot if CRM is disabled The renew button under usage message in the bot should be hidden if the admin has disabled the CRM. --- web/job/stats_notify_job.go | 5 +---- web/service/setting.go | 8 ++++++-- web/service/telegram.go | 19 ++++++++++--------- web/service/telegram_fsm.go | 12 +++--------- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/web/job/stats_notify_job.go b/web/job/stats_notify_job.go index 3f751d5ea8..b339c374a8 100644 --- a/web/job/stats_notify_job.go +++ b/web/job/stats_notify_job.go @@ -159,10 +159,7 @@ func (j *StatsNotifyJob) OnReceive() *StatsNotifyJob { updates := bot.GetUpdatesChan(u) - crmEnabled, err := j.settingService.GetTgCrmEnabled() - if err != nil { - crmEnabled = false - } + crmEnabled := j.settingService.GetTgCrmEnabled() config := tgbotapi.NewSetMyCommands(service.CreateChatMenu(crmEnabled)...) if _, err := bot.Request(config); err != nil { diff --git a/web/service/setting.go b/web/service/setting.go index 22335ece92..9b786ff394 100644 --- a/web/service/setting.go +++ b/web/service/setting.go @@ -292,8 +292,12 @@ func (s *SettingService) GetTimeLocation() (*time.Location, error) { * Telegram CRM *********************************************************/ -func (s *SettingService) GetTgCrmEnabled() (bool, error) { - return s.getBool("tgCrmEnabled") +func (s *SettingService) GetTgCrmEnabled() bool { + crmEnabled, err := s.getBool("tgCrmEnabled") + if err != nil { + return false + } + return crmEnabled } func (s *SettingService) GetTgCrmRegFinalMsg() (string, error) { diff --git a/web/service/telegram.go b/web/service/telegram.go index 86bd041743..0ccd4cebc6 100644 --- a/web/service/telegram.go +++ b/web/service/telegram.go @@ -38,13 +38,14 @@ func (j *TelegramService) GetAllClientUsages(chatId int64) { uuids := strings.Split(client.Uid, ",") + crmEnabled := j.settingService.GetTgCrmEnabled() for _, uuid := range uuids { - resp := j.GetClientUsage(chatId, uuid) + resp := j.GetClientUsage(chatId, uuid, crmEnabled) bot.Send(resp) } } -func (j *TelegramService) GetClientUsage(chatId int64, uuid string) *tgbotapi.MessageConfig { +func (j *TelegramService) GetClientUsage(chatId int64, uuid string, showRenewBtn bool) *tgbotapi.MessageConfig { resp := tgbotapi.NewMessage(chatId, "") @@ -69,12 +70,12 @@ func (j *TelegramService) GetClientUsage(chatId int64, uuid string) *tgbotapi.Me resp.Text += fmt.Sprintf("šŸ’” Active: %t\r\nšŸ“§ Name: %s\r\nšŸ”„ Total: %s / %s\r\nšŸ“… Expires on: %s\r\n\r\n", traffic.Enable, traffic.Email, common.FormatTraffic((traffic.Up + traffic.Down)), total, expiryTime) - resp.ReplyMarkup = tgbotapi.NewInlineKeyboardMarkup( - tgbotapi.NewInlineKeyboardRow( - tgbotapi.NewInlineKeyboardButtonData(Tr("update"), "update:"+uuid), - tgbotapi.NewInlineKeyboardButtonData(Tr("renew"), "renew:"+uuid), - ), - ) + + buttons := tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData(Tr("update"), "update:"+uuid)) + if showRenewBtn { + buttons = append(buttons, tgbotapi.NewInlineKeyboardButtonData(Tr("renew"), "renew:"+uuid)) + } + resp.ReplyMarkup = tgbotapi.NewInlineKeyboardMarkup(buttons) return &resp } @@ -186,7 +187,7 @@ func (t *TelegramService) HandleCallback(callback *tgbotapi.CallbackQuery) (resp chatId := callback.Message.Chat.ID if strings.HasPrefix(callback.Data, "update:") { - resp = t.GetClientUsage(chatId, strings.TrimPrefix(callback.Data, "update:")) + resp = t.GetClientUsage(chatId, strings.TrimPrefix(callback.Data, "update:"), t.settingService.GetTgCrmEnabled()) delete = false update = true return diff --git a/web/service/telegram_fsm.go b/web/service/telegram_fsm.go index bd650e3da1..c49a2edcbf 100644 --- a/web/service/telegram_fsm.go +++ b/web/service/telegram_fsm.go @@ -101,10 +101,7 @@ func IdleState(s *TgSession, msg *tgbotapi.Message) *tgbotapi.MessageConfig { return &resp } - crmEnabled, err := s.telegramService.settingService.GetTgCrmEnabled() - if err != nil { - crmEnabled = false - } + crmEnabled := s.telegramService.settingService.GetTgCrmEnabled() // Extract the command from the Message. switch msg.Command() { @@ -126,7 +123,7 @@ func IdleState(s *TgSession, msg *tgbotapi.Message) *tgbotapi.MessageConfig { } } } else { - resp = *s.telegramService.GetClientUsage(msg.Chat.ID, msg.CommandArguments()) + resp = *s.telegramService.GetClientUsage(msg.Chat.ID, msg.CommandArguments(), s.telegramService.settingService.GetTgCrmEnabled()) if client == nil { name := msg.Chat.FirstName + " " + msg.Chat.LastName + " @" + msg.Chat.UserName @@ -417,10 +414,7 @@ func (s *TgSession) showAccListKeyboard(resp *tgbotapi.MessageConfig) { } func (s *TgSession) RenewAccount(chatId int64, uuid string) *tgbotapi.MessageConfig { - crmEnabled, err := s.telegramService.settingService.GetTgCrmEnabled() - if err != nil { - crmEnabled = false - } + crmEnabled := s.telegramService.settingService.GetTgCrmEnabled() resp := tgbotapi.NewMessage(chatId, "") if !crmEnabled {