PaulSonOfLars / gotgbot

Autogenerated Go wrapper for the telegram API. Inspired by the python-telegram-bot library.
MIT License
469 stars 107 forks source link

The chatId parameter of GetChat method does not accept string #95

Closed amirhosss closed 1 year ago

amirhosss commented 1 year ago

Hi, the official Telegram docs accept strings, but in this implementation, we can't pass a string. https://core.telegram.org/bots/api#getchat

func (bot *Bot) GetChat(chatId int64, opts *GetChatOpts) (*Chat, error) {
    v := map[string]string{}
    v["chat_id"] = strconv.FormatInt(chatId, 10)

    var reqOpts *RequestOpts
    if opts != nil {
        reqOpts = opts.RequestOpts
    }

    r, err := bot.Request("getChat", v, nil, reqOpts)
    if err != nil {
        return nil, err
    }

    var c Chat
    return &c, json.Unmarshal(r, &c)
}
PaulSonOfLars commented 1 year ago

Hi, thanks for raising an issue!

This is a known limitation of the library, caused by go's static type system. It's not possible to have a method which accepts both a string and an int for the same parameter.

However, this limitation was found to be quite benign, as it is safer to write your bots using fixed chatids rather than usernames, which could change at any point.

If you really want to use a string, I recommend you use the bot.Request method to craft your own request. The example you've provided just needs a few tweaks to use strings instead of ints

amirhosss commented 1 year ago

Thanks