Skip to content

Commit

Permalink
Do not show renew btn in the bot if CRM is disabled
Browse files Browse the repository at this point in the history
The renew button under usage message in the bot should be
hidden if the admin has disabled the CRM.
  • Loading branch information
mjef committed Apr 7, 2023
1 parent d545ff8 commit c22ee1a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
5 changes: 1 addition & 4 deletions web/job/stats_notify_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions web/service/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 10 additions & 9 deletions web/service/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")

Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
12 changes: 3 additions & 9 deletions web/service/telegram_fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c22ee1a

Please sign in to comment.