16 // telegramTestToken is the bot token of the tests. It has the form of a
18 telegramTestToken = "123456:TEST-secret_token"
20 // telegramTestOwner is the user ID of the owner of the test bot.
21 telegramTestOwner = 4242
24 // TelegramFakeCall is one call of the fake Bot API. OK tells if the call got a
26 type TelegramFakeCall struct {
32 // TelegramFake is a fake Bot API server for telegramTestToken. getUpdates
33 // returns the added updates from the offset on, or waits a short time when
34 // there are none. The other methods succeed, unless fail gives an error answer
35 // for a call. An answer with HTTP 429 asks for a wait of one second. When stall
36 // is true for a call, the call gets no answer until the client gives it up or
37 // the test ends. Fail and stall run with the mutex held.
38 type TelegramFake struct {
42 updates []TelegramUpdate
43 calls []TelegramFakeCall
44 fail func(call TelegramFakeCall) (int, string)
45 stall func(call TelegramFakeCall) bool
50 // Add makes update available to getUpdates.
51 func (f *TelegramFake) Add(update TelegramUpdate) {
54 f.updates = append(f.updates, update)
58 case f.added <- struct{}{}:
63 // Calls returns the calls so far.
64 func (f *TelegramFake) Calls() []TelegramFakeCall {
66 defer f.mutex.Unlock()
68 return append([]TelegramFakeCall(nil), f.calls...)
71 // Sent returns the texts of the sendMessage calls and the Markdown of the
72 // sendRichMessage calls that succeeded, in order.
73 func (f *TelegramFake) Sent() []string {
76 for _, call := range f.Calls() {
79 case call.method == "sendMessage":
80 texts = append(texts, call.params["text"].(string))
81 case call.method == "sendRichMessage":
82 texts = append(texts, call.params["rich_message"].(map[string]any)["markdown"].(string))
89 func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) {
90 method, found := strings.CutPrefix(r.URL.Path, "/bot"+telegramTestToken+"/")
92 http.Error(w, `{"ok":false,"error_code":404,"description":"Not Found"}`, http.StatusNotFound)
97 var params map[string]any
99 err := json.NewDecoder(r.Body).Decode(¶ms)
101 http.Error(w, err.Error(), http.StatusBadRequest)
106 call := TelegramFakeCall{
113 status, description := 0, ""
115 status, description = f.fail(call)
118 stall := f.stall != nil && f.stall(call)
125 case <-r.Context().Done():
134 w.WriteHeader(status)
137 if status == http.StatusTooManyRequests {
141 _, _ = fmt.Fprintf(w, `{"ok":false,"error_code":%d,"description":%q,"parameters":{"retry_after":%d}}`, status, description, wait)
149 if method != "getUpdates" {
150 _, _ = io.WriteString(w, `{"ok":true,"result":true}`)
154 offset := int64(params["offset"].(float64))
155 updates := f.from(offset)
156 if len(updates) == 0 {
159 case <-time.After(50 * time.Millisecond):
160 case <-r.Context().Done():
163 updates = f.from(offset)
166 _ = json.NewEncoder(w).Encode(map[string]any{
172 // record keeps call.
173 func (f *TelegramFake) record(call TelegramFakeCall) {
175 defer f.mutex.Unlock()
177 f.calls = append(f.calls, call)
180 // from returns the updates from offset on, and forgets the ones before it, as
182 func (f *TelegramFake) from(offset int64) []TelegramUpdate {
184 defer f.mutex.Unlock()
186 var updates []TelegramUpdate
188 kept := f.updates[:0]
189 for _, update := range f.updates {
190 if update.UpdateID >= offset {
191 updates = append(updates, update)
192 kept = append(kept, update)
201 // newTelegramFake starts a fake Bot API server that stops at the end of the
203 func newTelegramFake(t *testing.T) *TelegramFake {
207 added: make(chan struct{}, 1),
208 stopped: make(chan struct{}),
211 f.Server = httptest.NewServer(http.HandlerFunc(f.serve))
220 // telegramOwnerMessage returns a text message of the owner in the private chat.
221 func telegramOwnerMessage(text string) *TelegramMessage {
222 return &TelegramMessage{
224 ID: telegramTestOwner,
228 ID: telegramTestOwner,