package bot import ( "strings" "github.com/mymmrac/telego" "github.com/noderunners/dlbot/internal/i18n" "github.com/noderunners/dlbot/internal/media" "github.com/noderunners/dlbot/internal/service" ) // Callback data is ":[:]". It carries identifiers only, // never translated text, so buttons keep working when the language changes. const ( cbDownload = "dl" // dl:: cbCancelCard = "cx" // cx: cbCancelJob = "jc" // jc: cbSkipQueue = "sq" // sq: cbBuy = "by" // by:[:] cbOfferBuy = "ob" // ob::: cbPayCheck = "pc" // pc: cbPayCancel = "px" // px: ) func cbData(parts ...string) string { return strings.Join(parts, ":") } // parseCallback splits callback data into the action, the ID and the rest. func parseCallback(data string) (action, id, arg string) { parts := strings.SplitN(data, ":", 3) action = parts[0] if len(parts) > 1 { id = parts[1] } if len(parts) > 2 { arg = parts[2] } return } func button(text, data string) telego.InlineKeyboardButton { return telego.InlineKeyboardButton{Text: text, CallbackData: data} } func keyboard(rows ...[]telego.InlineKeyboardButton) *telego.InlineKeyboardMarkup { var kept [][]telego.InlineKeyboardButton for _, r := range rows { if len(r) > 0 { kept = append(kept, r) } } return &telego.InlineKeyboardMarkup{InlineKeyboard: kept} } // cardProfiles picks the video buttons worth showing: Best always, then the // lower resolutions the source can actually be scaled down to. func cardProfiles(info *service.CardInfo) []media.Profile { out := []media.Profile{media.Best} for _, p := range []media.Profile{media.P720, media.P480} { if info.MaxHeight == 0 || info.MaxHeight > p.Height() { out = append(out, p) } } return out } // cardKeyboard builds the format buttons of a link card. func (b *Bot) cardKeyboard(lang, requestID string, info *service.CardInfo) *telego.InlineKeyboardMarkup { label := func(p media.Profile) string { name := b.profileLabel(lang, p) if size := info.Sizes[string(p)]; size > 0 { return b.tr.T(lang, "btn.with_size", i18n.Args{"Label": name, "Size": b.tr.Size(lang, size)}) } return name } var video, extra []telego.InlineKeyboardButton for _, p := range cardProfiles(info) { video = append(video, button(label(p), cbData(cbDownload, requestID, string(p)))) } if info.HasAudio { extra = append(extra, button(label(media.MP3), cbData(cbDownload, requestID, string(media.MP3)))) } if info.Thumbnail != "" { extra = append(extra, button(b.profileLabel(lang, media.Thumb), cbData(cbDownload, requestID, string(media.Thumb)))) } cancel := []telego.InlineKeyboardButton{button(b.tr.T(lang, "btn.cancel", nil), cbData(cbCancelCard, requestID))} return keyboard(video, extra, cancel) } // cancelJobKeyboard is the single Cancel button under a live status. func (b *Bot) cancelJobKeyboard(lang, deliveryID string) *telego.InlineKeyboardMarkup { return keyboard([]telego.InlineKeyboardButton{ button(b.tr.T(lang, "btn.cancel", nil), cbData(cbCancelJob, deliveryID)), }) } // queuedKeyboard adds "Skip the queue" to the Cancel button of a job that // waits in the free lane. func (b *Bot) queuedKeyboard(lang, deliveryID string) *telego.InlineKeyboardMarkup { skip := b.tr.T(lang, "btn.with_size", i18n.Args{ "Label": b.tr.T(lang, "btn.skip_queue", nil), "Size": b.tr.Sats(lang, b.cfg.Prices.SkipQueue), }) return keyboard( []telego.InlineKeyboardButton{button(skip, cbData(cbSkipQueue, deliveryID))}, []telego.InlineKeyboardButton{button(b.tr.T(lang, "btn.cancel", nil), cbData(cbCancelJob, deliveryID))}, ) } func urlButton(text, url string) telego.InlineKeyboardButton { return telego.InlineKeyboardButton{Text: text, URL: url} }