From fd426c84ccba477cf1d2ee073bdc99906f598084 Mon Sep 17 00:00:00 2001 From: Valentin Sushkov Date: Thu, 10 Jul 2025 20:18:05 +0300 Subject: [PATCH] Allow to send GET request to /chats/sendActions with empty actions list --- api_mock.go | 14 ++++++++++++++ bot.go | 5 +++++ client.go | 12 ++++++++++++ client_test.go | 15 +++++++++++++++ 4 files changed, 46 insertions(+) diff --git a/api_mock.go b/api_mock.go index f4c40c6..724b437 100644 --- a/api_mock.go +++ b/api_mock.go @@ -295,6 +295,17 @@ func (h *MockHandler) SelfGet(w http.ResponseWriter, r *http.Request) { } } +func (h *MockHandler) HandleActions(w http.ResponseWriter, r *http.Request) { + response := `{ + "ok": true + }` + + _, err := w.Write([]byte(response)) + if err != nil { + h.logger.Fatal("failed to write actions response") + } +} + func (h *MockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch { case r.FormValue("token") == "": @@ -309,6 +320,9 @@ func (h *MockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { case r.URL.Path == "/self/get": h.SelfGet(w, r) return + case r.URL.Path == "/chats/sendActions": + h.HandleActions(w, r) + return default: encoder := json.NewEncoder(w) err := encoder.Encode(&Response{ diff --git a/bot.go b/bot.go index 0971743..37524c9 100644 --- a/bot.go +++ b/bot.go @@ -45,6 +45,11 @@ func (b *Bot) SendChatActions(chatID string, actions ...ChatAction) error { return b.client.SendChatActions(chatID, actions...) } +// SendChatNoAction sends an empty action to signal that all actions are done. +func (b *Bot) SendChatNoAction(chatID string) error { + return b.client.SendChatNoAction(chatID) +} + // GetChatAdmins returns chat admins list with fields: // userID, creator flag func (b *Bot) GetChatAdmins(chatID string) ([]ChatMember, error) { diff --git a/client.go b/client.go index a10e449..fee621d 100644 --- a/client.go +++ b/client.go @@ -171,6 +171,18 @@ func (c *Client) SendChatActions(chatID string, actions ...ChatAction) error { return nil } +func (c *Client) SendChatNoAction(chatID string) error { + params := url.Values{ + "chatId": {chatID}, + "actions": {""}, + } + _, err := c.Do("/chats/sendActions", params, nil) + if err != nil { + return fmt.Errorf("error while sending no action: %s", err) + } + return nil +} + func (c *Client) GetChatAdmins(chatID string) ([]ChatMember, error) { params := url.Values{ "chatId": {chatID}, diff --git a/client_test.go b/client_test.go index ef3e699..634af95 100644 --- a/client_test.go +++ b/client_test.go @@ -312,3 +312,18 @@ func TestClient_GetInfo_Error(t *testing.T) { require.NoError(nil) } + +func TestClient_SendChatNoAction(t *testing.T) { + testServer := httptest.NewServer(&MockHandler{}) + defer func() { testServer.Close() }() + + client := Client{ + baseURL: testServer.URL, + token: "test_token", + client: http.DefaultClient, + logger: &logrus.Logger{}, + } + + err := client.SendChatNoAction("chat_id") + require.NoError(t, err) +}