From bb9a12ef1324b1db2485ee5c334c751932a31603 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Tue, 21 Mar 2023 13:38:20 -0400 Subject: [PATCH] Edit message when delete fails --- main.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index abe906f..33b621a 100644 --- a/main.go +++ b/main.go @@ -222,7 +222,26 @@ func deleteMessage(chatID int, messageID int) { log.Println(err) return } - logBody(resp.Body) + body := logBody(resp.Body) + ok := struct { + Ok bool `json:"ok"` + }{} + + if err := json.Unmarshal(body, &ok); err != nil { + log.Println(err) + return + } + //Old messages can't be deleted, so edit text instead + if !ok.Ok { + updateURL := fmt.Sprintf("https://api.telegram.org/bot%s/editMessageText?chat_id=%d&message_id=%d&text=%s", apiToken, chatID, messageID, "") + resp, err := http.Get(updateURL) + if err != nil { + log.Println(err) + return + } + logBody(resp.Body) + } + } func logBody(respBody io.ReadCloser) []byte {