package bot import ( "context" "github.com/mymmrac/telego" "github.com/noderunners/dlbot/internal/groups" "github.com/noderunners/dlbot/internal/i18n" "github.com/noderunners/dlbot/internal/service" ) // /help is a short introduction with buttons that turn the same message // into one section at a time: commands, account, groups. // cbHelp switches the help message to a section: hp:
. const cbHelp = "hp" // Help sections. const ( helpHome = "home" helpCommands = "cmds" helpAccount = "acct" helpGroups = "groups" ) // helpView renders one section of the help for a chat. func (b *Bot) helpView(ctx context.Context, act actor, chat int64, chatType, section string) (string, *telego.InlineKeyboardMarkup, error) { lang := act.lang private := isPrivate(chatType) back := keyboard([]telego.InlineKeyboardButton{button(b.tr.T(lang, "btn.back", nil), cbData(cbHelp, helpHome))}) switch section { case helpCommands: if private { return b.tr.HTML(lang, "help.commands", nil), back, nil } return b.tr.HTML(lang, "help.commands_group", nil), back, nil case helpAccount: return b.tr.HTML(lang, "help.account", nil), back, nil case helpGroups: if private { // Group commands mean nothing here: how to get the bot into a // group instead. return b.tr.HTML(lang, "help.groups_private", nil), keyboard( []telego.InlineKeyboardButton{b.addToGroupButton(lang)}, back.InlineKeyboard[0], ), nil } return b.tr.HTML(lang, "help.groups_group", nil), back, nil } dec, err := b.svc.Entitlement(ctx, service.Who{UserID: act.user.ID, ChatID: chat, ChatType: chatType}, false) if err != nil { return "", nil, err } l := dec.Limits var format string if private { format = b.tr.HTML(lang, "help.format", i18n.Args{"Quality": b.userQualityName(lang, act.user.Quality)}) } else { q := "" if act.group != nil { q = act.group.Quality } format = b.tr.HTML(lang, "help.format_group", i18n.Args{"Quality": b.qualityName(lang, q)}) } text := b.tr.HTML(lang, "help.intro", nil) + "\n\n" + format + "\n\n" + b.tr.HTML(lang, "help.limits", i18n.Args{ "MaxSize": b.capSize(lang, min64(l.MaxSize, b.cfg.MaxUploadBytes())), "MaxDuration": b.tr.Span(lang, secondsDuration(l.MaxDuration)), "Daily": b.capNumber(lang, int64(l.DailyDownloads)), }) rows := [][]telego.InlineKeyboardButton{ { button(b.tr.T(lang, "btn.help_commands", nil), cbData(cbHelp, helpCommands)), button(b.tr.T(lang, "btn.help_account", nil), cbData(cbHelp, helpAccount)), }, {button(b.tr.T(lang, "btn.help_groups", nil), cbData(cbHelp, helpGroups))}, } var last []telego.InlineKeyboardButton if btn, ok := b.appButton(lang, private, ""); ok { last = append(last, btn) } if !dec.Source.Paid() { last = append(last, b.upgradeButton(lang, private)...) } rows = append(rows, last) return text, keyboard(rows...), nil } func (b *Bot) cmdHelp(ctx context.Context, m *telego.Message, act actor) { text, kb, err := b.helpView(ctx, act, m.Chat.ID, m.Chat.Type, helpHome) if err != nil { b.internalError(ctx, m.Chat.ID, act, err) return } _, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), text, kb) } // cbHelpSection turns the help message into another section. func (b *Bot) cbHelpSection(ctx context.Context, q *telego.CallbackQuery, act actor, section string) { if q.Message == nil { b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false) return } chat := q.Message.GetChat() if groups.IsGroup(chat.Type) && act.group == nil { b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false) return } text, kb, err := b.helpView(ctx, act, chat.ID, chat.Type, section) if err != nil { b.answer(ctx, q, b.tr.T(act.lang, "error.internal", nil), true) return } b.answer(ctx, q, "", false) _ = b.edit(ctx, chat.ID, int64(q.Message.GetMessageID()), false, text, kb) }