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:
co-authored by
Claude Opus 5.5
parent
3c153af97f
commit
6843053582
+60
-13
@@ -1,6 +1,7 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"github.com/noderunners/dlbot/internal/db"
|
||||
"github.com/noderunners/dlbot/internal/media"
|
||||
"github.com/noderunners/dlbot/internal/sched"
|
||||
"github.com/noderunners/dlbot/internal/service"
|
||||
"github.com/noderunners/dlbot/internal/storage"
|
||||
"github.com/noderunners/dlbot/internal/tg"
|
||||
)
|
||||
@@ -98,7 +100,7 @@ func (b *Bot) deliverOne(ctx context.Context, job *db.Job, d *db.Delivery) (stop
|
||||
// same job; that user is refused here, like on a download of their own.
|
||||
if code := overCap(job, d); code != "" {
|
||||
_, _ = b.store.TransitionDelivery(saveCtx, d.ID, db.DeliverySending, db.DeliveryFailed, code)
|
||||
b.finalStatus(ctx, d, titleLine(job.Title)+"\n"+b.failureText(d.Lang, code)+b.release(ctx, d))
|
||||
b.finalStatusKB(ctx, d, titleLine(job.Title)+"\n"+b.failureText(d.Lang, code)+b.release(ctx, d), b.failureKeyboard(ctx, d, job, code))
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -130,7 +132,7 @@ func (b *Bot) deliverOne(ctx context.Context, job *db.Job, d *db.Delivery) (stop
|
||||
case tg.IsFileTooBig(err):
|
||||
log.Warn("telegram refused file size", "size", job.FileSize, "err", err)
|
||||
_, _ = b.store.TransitionDelivery(saveCtx, d.ID, db.DeliverySending, db.DeliveryFailed, "too_large")
|
||||
b.finalStatus(ctx, d, titleLine(job.Title)+"\n"+b.failureText(d.Lang, "too_large")+b.release(ctx, d))
|
||||
b.finalStatusKB(ctx, d, titleLine(job.Title)+"\n"+b.failureText(d.Lang, "too_large")+b.release(ctx, d), b.failureKeyboard(ctx, d, job, "too_large"))
|
||||
case d.Attempts+1 < maxSendAttempts:
|
||||
log.Warn("send failed, will retry", "attempt", d.Attempts+1, "err", err)
|
||||
if wait := tg.RetryAfter(err); wait > 0 && wait <= time.Minute {
|
||||
@@ -140,7 +142,7 @@ func (b *Bot) deliverOne(ctx context.Context, job *db.Job, d *db.Delivery) (stop
|
||||
default:
|
||||
log.Error("send failed, giving up", "err", err)
|
||||
_, _ = b.store.TransitionDelivery(saveCtx, d.ID, db.DeliverySending, db.DeliveryFailed, "send_failed")
|
||||
b.finalStatus(ctx, d, titleLine(job.Title)+"\n"+b.failureText(d.Lang, "send_failed")+b.release(ctx, d))
|
||||
b.finalStatusKB(ctx, d, titleLine(job.Title)+"\n"+b.failureText(d.Lang, "send_failed")+b.release(ctx, d), b.failureKeyboard(ctx, d, job, "send_failed"))
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -166,7 +168,8 @@ func (b *Bot) deliverOne(ctx context.Context, job *db.Job, d *db.Delivery) (stop
|
||||
via = "cache"
|
||||
}
|
||||
b.metrics.Delivered(via)
|
||||
b.finalStatus(ctx, d, titleLine(job.Title)+"\n"+b.tr.HTML(d.Lang, "status.done", nil))
|
||||
// The file is the answer. The status message has done its job.
|
||||
b.retireStatus(ctx, d, sent.MessageID)
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -317,17 +320,27 @@ func (b *Bot) sendAs(ctx context.Context, mode string, job *db.Job, d *db.Delive
|
||||
defer rc.Close()
|
||||
input = telego.InputFile{File: namedReader{Reader: rc, name: fileName(job.Title, job.FileExt)}}
|
||||
}
|
||||
caption := clip(titleLine(job.Title), 1024)
|
||||
out := b.outgoing(ctx, job, d)
|
||||
caption := clip(out.caption, 1024)
|
||||
chat := chatID(d.ChatID)
|
||||
reply := replyParams(d.ReplyToMessageID)
|
||||
reply := replyParams(d.ChatID, d.ReplyToMessageID)
|
||||
// Content is never protected: people forward what they downloaded.
|
||||
|
||||
switch mode {
|
||||
case "video":
|
||||
msg, err := b.api.SendVideo(ctx, &telego.SendVideoParams{
|
||||
p := &telego.SendVideoParams{
|
||||
ChatID: chat, Video: input, Caption: caption, ParseMode: telego.ModeHTML,
|
||||
Duration: int(job.Duration), Width: job.Width, Height: job.Height,
|
||||
SupportsStreaming: true, ReplyParameters: reply,
|
||||
})
|
||||
SupportsStreaming: true, ReplyParameters: reply, ReplyMarkup: out.keyboard,
|
||||
}
|
||||
// A thumbnail only counts with an upload; Telegram ignores it for a
|
||||
// file_id, and it cannot be reused.
|
||||
if reuseFileID == "" && out.thumbnailURL != "" {
|
||||
if jpg := b.thumbs.Fetch(ctx, out.thumbnailURL); jpg != nil {
|
||||
p.Thumbnail = &telego.InputFile{File: namedReader{Reader: bytes.NewReader(jpg), name: "thumb.jpg"}}
|
||||
}
|
||||
}
|
||||
msg, err := b.api.SendVideo(ctx, p)
|
||||
if err != nil {
|
||||
return sentFile{}, err
|
||||
}
|
||||
@@ -341,7 +354,8 @@ func (b *Bot) sendAs(ctx context.Context, mode string, job *db.Job, d *db.Delive
|
||||
case "audio":
|
||||
msg, err := b.api.SendAudio(ctx, &telego.SendAudioParams{
|
||||
ChatID: chat, Audio: input, Title: shorten(job.Title, 120), Performer: shorten(job.Uploader, 60),
|
||||
Duration: int(job.Duration), ReplyParameters: reply,
|
||||
Caption: caption, ParseMode: telego.ModeHTML,
|
||||
Duration: int(job.Duration), ReplyParameters: reply, ReplyMarkup: out.keyboard,
|
||||
})
|
||||
if err != nil {
|
||||
return sentFile{}, err
|
||||
@@ -352,7 +366,8 @@ func (b *Bot) sendAs(ctx context.Context, mode string, job *db.Job, d *db.Delive
|
||||
return sentFile{MessageID: int64(msg.MessageID)}, nil
|
||||
case "photo":
|
||||
msg, err := b.api.SendPhoto(ctx, &telego.SendPhotoParams{
|
||||
ChatID: chat, Photo: input, Caption: caption, ParseMode: telego.ModeHTML, ReplyParameters: reply,
|
||||
ChatID: chat, Photo: input, Caption: caption, ParseMode: telego.ModeHTML,
|
||||
ReplyParameters: reply, ReplyMarkup: out.keyboard,
|
||||
})
|
||||
if err != nil {
|
||||
return sentFile{}, err
|
||||
@@ -363,7 +378,8 @@ func (b *Bot) sendAs(ctx context.Context, mode string, job *db.Job, d *db.Delive
|
||||
return sentFile{MessageID: int64(msg.MessageID)}, nil
|
||||
default:
|
||||
msg, err := b.api.SendDocument(ctx, &telego.SendDocumentParams{
|
||||
ChatID: chat, Document: input, Caption: caption, ParseMode: telego.ModeHTML, ReplyParameters: reply,
|
||||
ChatID: chat, Document: input, Caption: caption, ParseMode: telego.ModeHTML,
|
||||
ReplyParameters: reply, ReplyMarkup: out.keyboard,
|
||||
})
|
||||
if err != nil {
|
||||
return sentFile{}, err
|
||||
@@ -375,6 +391,36 @@ func (b *Bot) sendAs(ctx context.Context, mode string, job *db.Job, d *db.Delive
|
||||
}
|
||||
}
|
||||
|
||||
// outgoingFile is what goes with a file besides the file itself.
|
||||
type outgoingFile struct {
|
||||
caption string
|
||||
keyboard *telego.InlineKeyboardMarkup
|
||||
thumbnailURL string
|
||||
}
|
||||
|
||||
// outgoing builds the caption and the buttons of a delivery's file from
|
||||
// the request it answers.
|
||||
func (b *Bot) outgoing(ctx context.Context, job *db.Job, d *db.Delivery) outgoingFile {
|
||||
info := &service.CardInfo{Title: job.Title, Uploader: job.Uploader, Duration: job.Duration}
|
||||
url := job.URL
|
||||
var req *db.Request
|
||||
if r, err := b.store.GetRequest(ctx, d.RequestID); err == nil {
|
||||
req = r
|
||||
url = r.URL
|
||||
if ci, err := service.ParseCardInfo(r.Info); err == nil {
|
||||
info = ci
|
||||
}
|
||||
}
|
||||
out := outgoingFile{
|
||||
caption: b.caption(d.Lang, info, url, jobProfile(job)),
|
||||
keyboard: b.resultKeyboard(d.Lang, req, info, jobProfile(job)),
|
||||
}
|
||||
if jobProfile(job).Kind() == media.KindVideo {
|
||||
out.thumbnailURL = info.Thumbnail
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// finishIfServed closes a delivering job once nobody waits for it, and
|
||||
// decides what happens to the stored file: it enters the cache when the
|
||||
// result is cacheable, and is deleted otherwise.
|
||||
@@ -448,7 +494,8 @@ func (b *Bot) failJob(ctx context.Context, jobID string) {
|
||||
continue
|
||||
}
|
||||
// The automatic retry is spent: give back what the download cost.
|
||||
b.finalStatus(ctx, d, titleLine(job.Title)+"\n"+b.failureText(d.Lang, job.ErrorCode)+b.release(ctx, d))
|
||||
b.finalStatusKB(ctx, d, titleLine(job.Title)+"\n"+b.failureText(d.Lang, job.ErrorCode)+b.release(ctx, d),
|
||||
b.failureKeyboard(ctx, d, job, job.ErrorCode))
|
||||
}
|
||||
b.removeStored(ctx, job)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user