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) {
53 f.updates = append(f.updates, update)
57 case f.added <- struct{}{}:
62 // Calls returns the calls so far.
63 func (f *TelegramFake) Calls() []TelegramFakeCall {
65 defer f.mutex.Unlock()
67 return append([]TelegramFakeCall(nil), f.calls...)
70 // Sent returns the texts of the sendMessage calls and the Markdown of the
71 // sendRichMessage calls that succeeded, in order.
72 func (f *TelegramFake) Sent() []string {
74 for _, call := range f.Calls() {
77 case call.method == "sendMessage":
78 texts = append(texts, call.params["text"].(string))
79 case call.method == "sendRichMessage":
80 texts = append(texts, call.params["rich_message"].(map[string]any)["markdown"].(string))
87 func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) {
88 method, found := strings.CutPrefix(r.URL.Path, "/bot"+telegramTestToken+"/")
90 http.Error(w, `{"ok":false,"error_code":404,"description":"Not Found"}`, http.StatusNotFound)
94 var params map[string]any
95 err := json.NewDecoder(r.Body).Decode(¶ms)
97 http.Error(w, err.Error(), http.StatusBadRequest)
101 call := TelegramFakeCall{
107 status, description := 0, ""
109 status, description = f.fail(call)
112 stall := f.stall != nil && f.stall(call)
118 case <-r.Context().Done():
127 w.WriteHeader(status)
129 if status == http.StatusTooManyRequests {
133 _, _ = fmt.Fprintf(w, `{"ok":false,"error_code":%d,"description":%q,"parameters":{"retry_after":%d}}`, status, description, wait)
140 if method != "getUpdates" {
141 _, _ = io.WriteString(w, `{"ok":true,"result":true}`)
145 offset := int64(params["offset"].(float64))
146 updates := f.from(offset)
147 if len(updates) == 0 {
150 case <-time.After(50 * time.Millisecond):
151 case <-r.Context().Done():
154 updates = f.from(offset)
157 _ = json.NewEncoder(w).Encode(map[string]any{
163 // record keeps call.
164 func (f *TelegramFake) record(call TelegramFakeCall) {
166 defer f.mutex.Unlock()
168 f.calls = append(f.calls, call)
171 // from returns the updates from offset on, and forgets the ones before it, as
173 func (f *TelegramFake) from(offset int64) []TelegramUpdate {
175 defer f.mutex.Unlock()
177 var updates []TelegramUpdate
178 kept := f.updates[:0]
179 for _, update := range f.updates {
180 if update.UpdateID >= offset {
181 updates = append(updates, update)
182 kept = append(kept, update)
191 // newTelegramFake starts a fake Bot API server that stops at the end of the
193 func newTelegramFake(t *testing.T) *TelegramFake {
197 added: make(chan struct{}, 1),
198 stopped: make(chan struct{}),
201 f.Server = httptest.NewServer(http.HandlerFunc(f.serve))
210 // telegramOwnerMessage returns a text message of the owner in the private chat.
211 func telegramOwnerMessage(text string) *TelegramMessage {
212 return &TelegramMessage{
214 ID: telegramTestOwner,
218 ID: telegramTestOwner,