// 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) }