Phase 1: quick flow fixes

- One message per job: the status message is deleted once the file is
  sent, or shortened to "Sent." when Telegram keeps it.
- No reply quotes in private chats; groups keep them.
- Queue status only mentions the position when the wait is above
  QUEUE_NOTICE_THRESHOLD_SECONDS or somebody is ahead. Status edits are
  throttled to one per 2.5 s per message; a state that arrives too soon
  is shown later instead of dropped. The wait estimate is 0 with a free
  slot.
- Caption with title (uploader and date when generic), uploader,
  duration, a short source link without tracking parameters and an
  optional "via @bot" line (CAPTION_ATTRIBUTION). Uploaded videos get a
  320 px JPEG thumbnail; merged MP4s are written with faststart.
- Buttons under the result: Audio (MP3), Other quality, Thumbnail,
  Delete. Requester only in groups.
- Language resolved once per update and passed in the context. A
  fallback to English is logged as a bug; a test walks every screen in
  Spanish and Dutch. Language picker on the first /start.
- Sizes without a zero decimal ("50 MB").
- First link of a new user shows the card once and sets the default to
  "best that fits my limit".
- Errors offer the next step (Try again, Smaller quality, Upgrade, Open
  app); passing lookup failures are retried once.
- Up to MULTI_LINK_MAX links per message; a link sent a moment ago gets
  "already sent" with a Send again button.
- Mini App menu button, labelled per private chat in the user's
  language.
- Personal /settings in private chats with an "Add me to a group"
  button.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 20:08:58 +00:00
co-authored by Claude Opus 5.5
parent 3c153af97f
commit 6843053582
52 changed files with 3369 additions and 271 deletions
+111 -14
View File
@@ -36,6 +36,12 @@ type linkRequest struct {
fresh bool
// subs shows the subtitle languages instead of the format buttons.
subs bool
// checkRecent answers "already sent" for a link this user got a
// moment ago, instead of downloading it again.
checkRecent bool
// messageID, when set, is an existing message to use as the status,
// such as the error message a "Try again" button sits under.
messageID int64
}
// startLink runs the link flow in the background so a slow metadata lookup
@@ -65,16 +71,24 @@ func (b *Bot) linkFlow(ctx context.Context, lr linkRequest) {
lang := lr.act.lang
log := b.log.With("user_id", lr.act.user.ID, "chat_id", lr.chatID, "url", links.Redact(lr.url))
placeholder, err := b.send(ctx, lr.chatID, lr.sourceID, b.tr.HTML(lang, "card.looking", nil), nil)
if err != nil {
if lr.checkRecent && !lr.fresh && b.alreadySent(ctx, lr) {
return
}
phID := int64(placeholder.MessageID)
phID := lr.messageID
if phID != 0 {
_ = b.edit(ctx, lr.chatID, phID, false, b.tr.HTML(lang, "card.looking", nil), nil)
} else {
placeholder, err := b.send(ctx, lr.chatID, lr.sourceID, b.tr.HTML(lang, "card.looking", nil), nil)
if err != nil {
return
}
phID = int64(placeholder.MessageID)
}
probed, err := b.svc.ProbeAny(ctx, lr.url)
if err != nil {
if ctx.Err() == nil {
_ = b.edit(ctx, lr.chatID, phID, false, b.probeErrorText(lang, err), nil)
b.lookupFailed(ctx, lr, phID, err)
}
return
}
@@ -101,7 +115,14 @@ func (b *Bot) linkFlow(ctx context.Context, lr linkRequest) {
Info: info.JSON(),
Lang: lang,
}
if lr.preset != "" {
preset := lr.preset
if lr.subs {
preset = ""
}
if preset == presetFit {
preset = b.fitProfile(ctx, lr, info)
}
if preset != "" {
req.Status = db.RequestChosen
}
if err := b.store.InsertRequest(ctx, req); err != nil {
@@ -111,13 +132,24 @@ func (b *Bot) linkFlow(ctx context.Context, lr linkRequest) {
}
log = log.With("request_id", req.ID)
if lr.preset != "" {
if preset != "" {
job, _, err := b.svc.Start(ctx, service.StartParams{
Request: req, Info: info, Profile: lr.preset,
Request: req, Info: info, Profile: preset,
StatusMessageID: phID, ReplyToMessageID: lr.sourceID,
})
if err != nil {
_ = b.edit(ctx, lr.chatID, phID, false, titleLine(info.Title)+"\n"+html.EscapeString(b.startErrorText(lang, err)), nil)
// Give the request back, so the offer and the buttons under the
// error can still use it.
if _, rerr := b.store.TransitionRequest(ctx, req.ID, db.RequestChosen, db.RequestReady); rerr != nil {
log.Error("release request", "err", rerr)
}
var le *service.LimitError
if errors.As(err, &le) && le.Upgradable {
b.deleteMessage(ctx, lr.chatID, phID)
b.offerUpgrade(ctx, lr.chatID, lr.sourceID, lang, req, preset, le, isPrivate(lr.chatType))
return
}
b.showStartError(ctx, req, info, preset, phID, false, err)
return
}
b.refreshStatus(ctx, job.ID)
@@ -129,28 +161,93 @@ func (b *Bot) linkFlow(ctx context.Context, lr linkRequest) {
return
}
// A first link from someone who never picked a default: show the card
// this once, and make "best that fits my limit" their default.
note := ""
if isPrivate(lr.chatType) && lr.act.user.Quality == qualityUnset && lr.preset == "" {
if err := b.store.SetUserQuality(context.WithoutCancel(ctx), lr.act.user.ID, qualityFit); err != nil {
log.Error("set first default quality", "err", err)
} else {
note = b.tr.HTML(lang, "card.first_run", nil)
}
}
b.showCard(ctx, lang, req, info, phID, note)
}
// showCard shows the format card of a request: a photo with the
// thumbnail when Telegram can fetch it, text otherwise. replaceID is a
// message to turn into the card (or delete, once the photo is out); 0
// sends a new one.
func (b *Bot) showCard(ctx context.Context, lang string, req *db.Request, info *service.CardInfo, replaceID int64, note string) {
text := b.cardText(lang, info, true)
if note != "" {
text += "\n\n" + note
}
kb := b.cardKeyboard(lang, req.ID, info)
if info.Thumbnail != "" {
msg, err := b.api.SendPhoto(ctx, &telego.SendPhotoParams{
ChatID: chatID(lr.chatID),
ChatID: chatID(req.ChatID),
Photo: telego.InputFile{URL: info.Thumbnail},
Caption: clip(text, 1024),
ParseMode: telego.ModeHTML,
ReplyParameters: replyParams(lr.sourceID),
ReplyParameters: replyParams(req.ChatID, req.SourceMessageID),
ReplyMarkup: kb,
})
if err == nil {
if err := b.store.SetRequestCard(context.WithoutCancel(ctx), req.ID, int64(msg.MessageID), true); err != nil {
log.Error("save card", "err", err)
b.log.Error("save card", "request_id", req.ID, "err", err)
}
b.deleteMessage(ctx, lr.chatID, phID)
b.deleteMessage(ctx, req.ChatID, replaceID)
return
}
// Telegram could not fetch the thumbnail. The text card still works.
log.Debug("photo card failed, using text card", "err", err)
b.log.Debug("photo card failed, using text card", "err", err)
}
_ = b.edit(ctx, lr.chatID, phID, false, text, kb)
if replaceID != 0 {
_ = b.edit(ctx, req.ChatID, replaceID, false, text, kb)
return
}
msg, err := b.send(ctx, req.ChatID, req.SourceMessageID, text, kb)
if err == nil {
if err := b.store.SetRequestCard(context.WithoutCancel(ctx), req.ID, int64(msg.MessageID), false); err != nil {
b.log.Error("save card", "request_id", req.ID, "err", err)
}
}
}
// fitProfile picks "the best quality that fits my limit" for a looked up
// link. When nothing fits, the smallest quality is returned and the start
// is refused by the usual limit check, which leads to the over-limit offer.
func (b *Bot) fitProfile(ctx context.Context, lr linkRequest, info *service.CardInfo) media.Profile {
who := service.Who{UserID: lr.act.user.ID, ChatID: lr.chatID, ChatType: lr.chatType}
dec, err := b.svc.Entitlement(ctx, who, false)
if err != nil {
return media.Best
}
p, _ := service.BestFit(info, min64(dec.Limits.MaxSize, b.cfg.MaxUploadBytes()))
return p
}
// lookupFailed explains a link that could not be looked up. The request is
// kept, failed, so that "Try again" knows the link.
func (b *Bot) lookupFailed(ctx context.Context, lr linkRequest, messageID int64, err error) {
lang := lr.act.lang
code := string(ytdlp.CodeOf(err))
if errors.Is(err, service.ErrPlaylist) {
code = "playlist"
}
req := &db.Request{
ID: ids.New(), UserID: lr.act.user.ID, ChatID: lr.chatID, ChatType: lr.chatType,
SourceMessageID: lr.sourceID, CardMessageID: messageID, URL: lr.url,
Status: db.RequestFailed, Lang: lang,
}
var kb *telego.InlineKeyboardMarkup
if ierr := b.store.InsertRequest(ctx, req); ierr == nil {
kb = b.errorKeyboard(lang, req, "", code)
} else {
b.log.Error("insert failed request", "err", ierr)
}
_ = b.edit(ctx, lr.chatID, messageID, false, b.probeErrorText(lang, err), kb)
}
// cardText renders the card: title, a line of facts and, for cards with