go-telegram-bot-api / telegram-bot-api

Golang bindings for the Telegram Bot API
https://go-telegram-bot-api.dev
MIT License
5.76k stars 889 forks source link

Delete message error #615

Closed g4s8 closed 1 year ago

g4s8 commented 1 year ago

I'm using delete message API:

import (
    telegram "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

// api *telegram.BotAPI
msg := telegram.NewDeleteMessage(int64(chatID), msgID)
if _, err := api.Send(msg); err != nil {
    return errors.Wrap(err, "delete message")
}

When I call this API, Telegram deletes this message successfully, but telegram-bot library returns error: json: cannot unmarshal bool into Go value of type tgbotapi.Message.

Debug logs for this call:

2022/12/03 23:05:37 Endpoint: deleteMessage, params: map[chat_id:****** message_id:****]
2022/12/03 23:05:38 Endpoint: deleteMessage, response: {"ok":true,"result":true}

It looks like this library is trying to decode "result": true response from Telegram API into message struct.

It's not a very critical issue, but because of it, I need to ignore errors from delete.

Version is: github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1

xeptore commented 1 year ago

It's already mentioned here: https://github.com/go-telegram-bot-api/telegram-bot-api/issues/438#issuecomment-805036458.

The updated version of your snippet (with some improvements on error handling):

if resp, err := api.Request(telegram.NewDeleteMessage(int64(chatID), msgID)); nil != err || !resp.Ok {
    return fmt.Errorf("failed to delete message id %d (%s): %v", msgID, string(resp.Result), err)
}

@g4s8, please close the issue if it works.