Remote workers, Postgres and S3, all chosen by configuration. - Pull-based worker protocol over HTTP: claim (long poll), heartbeat, complete, fail, upload, source, probe. Workers need no inbound ports - cmd/worker: standalone stateless worker with health endpoint and scheduled yt-dlp self-update - Per-worker bearer tokens, stored hashed, revocable; identity always taken from the token; actions only on jobs the worker holds a lease for - Tags: site_tags routes a site's jobs and lookups to workers that carry the required tags - Drain, /workers, /ytdlp update, and "core worker add|revoke|list" - Link lookups can run on remote workers (PROBE_MODE), so the core needs no yt-dlp and no residential address - Postgres backend with pgx, same schema and queries; per-user advisory lock and job row lock where SQLite's serial writes were relied on - The whole test suite runs on SQLite and on Postgres - S3 storage backend with presigned worker uploads, sharing a conformance suite with the local backend - Dockerfile with core, worker and faketelegram targets, docker-compose with optional Postgres and local Bot API server, systemd units - deploy/acceptance/kill-worker.sh: kills a worker mid-job and checks that the other one delivers the file - Fixes found on the way: /cancel could let the next playlist item start; a refund note could be missing when the sweep refunded first Docker was not available on the build machine: the compose files were validated with "docker compose config" only. The acceptance scenario was run with real processes. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
111 lines
3.5 KiB
Go
111 lines
3.5 KiB
Go
// Command faketelegram is a stand-in for the Telegram Bot API, for trying
|
|
// the bot without a real bot token: local development, the acceptance
|
|
// scripts and load tests.
|
|
//
|
|
// It serves the Bot API methods the bot uses on -listen, for the token
|
|
// printed at startup, plus a small control API to play the user:
|
|
//
|
|
// POST /control/message {"chat_id":1,"user_id":1,"text":"https://..."}
|
|
// POST /control/callback {"chat_id":1,"user_id":1,"message_id":7,"data":"dl:..."}
|
|
// GET /control/calls?method=sendVideo&after=0
|
|
//
|
|
// It must never face the internet: it accepts anything from anyone.
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/noderunners/dlbot/internal/tg/fakeserver"
|
|
)
|
|
|
|
func main() {
|
|
listen := flag.String("listen", "127.0.0.1:8090", "address to listen on")
|
|
flag.Parse()
|
|
|
|
fake := fakeserver.NewHandler()
|
|
mux := http.NewServeMux()
|
|
mux.Handle("/bot"+fakeserver.Token+"/", fake)
|
|
|
|
mux.HandleFunc("POST /control/message", func(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
ChatID int64 `json:"chat_id"`
|
|
UserID int64 `json:"user_id"`
|
|
Text string `json:"text"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil || in.Text == "" {
|
|
http.Error(w, "need chat_id, user_id and text", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if in.UserID == 0 {
|
|
in.UserID = in.ChatID
|
|
}
|
|
id := fake.PushMessage(in.ChatID, in.UserID, in.Text)
|
|
writeJSON(w, map[string]int{"message_id": id})
|
|
})
|
|
|
|
mux.HandleFunc("POST /control/callback", func(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
ChatID int64 `json:"chat_id"`
|
|
UserID int64 `json:"user_id"`
|
|
MessageID int `json:"message_id"`
|
|
Data string `json:"data"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil || in.Data == "" {
|
|
http.Error(w, "need chat_id, user_id, message_id and data", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if in.UserID == 0 {
|
|
in.UserID = in.ChatID
|
|
}
|
|
writeJSON(w, map[string]string{"callback_id": fake.PushCallback(in.ChatID, in.UserID, in.MessageID, in.Data)})
|
|
})
|
|
|
|
mux.HandleFunc("GET /control/calls", func(w http.ResponseWriter, r *http.Request) {
|
|
after, _ := strconv.Atoi(r.URL.Query().Get("after"))
|
|
type call struct {
|
|
Seq int `json:"seq"`
|
|
Method string `json:"method"`
|
|
MessageID int `json:"message_id,omitempty"`
|
|
Text string `json:"text,omitempty"`
|
|
Params map[string]string `json:"params"`
|
|
Files map[string]int64 `json:"files,omitempty"`
|
|
Buttons []string `json:"buttons,omitempty"`
|
|
Failed bool `json:"failed,omitempty"`
|
|
}
|
|
out := []call{}
|
|
for _, c := range fake.Calls(r.URL.Query().Get("method")) {
|
|
if c.Seq <= after {
|
|
continue
|
|
}
|
|
item := call{Seq: c.Seq, Method: c.Method, MessageID: c.MessageID, Text: c.Text(), Params: c.Params, Failed: c.Failed}
|
|
if len(c.Files) > 0 {
|
|
item.Files = map[string]int64{}
|
|
for field, f := range c.Files {
|
|
item.Files[field] = f.Size
|
|
}
|
|
}
|
|
for _, b := range c.Buttons() {
|
|
if b.Data != "" {
|
|
item.Buttons = append(item.Buttons, b.Data)
|
|
}
|
|
}
|
|
out = append(out, item)
|
|
}
|
|
writeJSON(w, out)
|
|
})
|
|
|
|
fmt.Printf("fake Telegram Bot API on http://%s\n", *listen)
|
|
fmt.Printf(" BOT_TOKEN=%s\n TELEGRAM_API_URL=http://%s\n", fakeserver.Token, *listen)
|
|
log.Fatal(http.ListenAndServe(*listen, mux))
|
|
}
|
|
|
|
func writeJSON(w http.ResponseWriter, v any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(v)
|
|
}
|