Phase 1: core bot
Link in, card, download, send, in private chats and groups. - cmd/core with an embedded download worker - Telegram front end on telego v1.13.0 (Bot API 10.4): link card with thumbnail and format buttons, one live status message, /dl /audio /thumb /info /queue /cancel, group support with requester-only buttons - Database backed queue: paid and free lanes, leases with heartbeats, one automatic retry, cancel, queue position and wait estimate - yt-dlp and ffprobe behind a Runner interface with classified, redacted errors mapped to friendly messages - SQLite via modernc with goose migrations, portable schema - Local file storage behind a Storage interface - go-i18n with an English catalog, no literal text in handlers - Free tier limits enforced before queueing and again on the real file - Bot token and webhook secret are stripped from all log output - Tests: URL parsing, job lifecycle, worker, yt-dlp wrapper, and end to end flows against a fake Bot API server; opt-in test with the real yt-dlp Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
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 "<action>:<id>[:<arg>]". It carries identifiers only,
|
||||
// never translated text, so buttons keep working when the language changes.
|
||||
const (
|
||||
cbDownload = "dl" // dl:<request id>:<profile>
|
||||
cbCancelCard = "cx" // cx:<request id>
|
||||
cbCancelJob = "jc" // jc:<delivery id>
|
||||
)
|
||||
|
||||
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)),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user