telegram-bot-api icon indicating copy to clipboard operation
telegram-bot-api copied to clipboard

`Message.chat_id` can be a `string` or `int`

Open CedricRaison opened this issue 2 years ago • 4 comments

From the telegram api chat_it can be an int or string (int for username but string for channel "@channelname".

Currently using the library:

msg := tgbotapi.NewMessage("@channelname", "here is a new message on the channel !")
bot.Send(msg)

I have the error

cannot use "@channelname" (untyped string constant) as int64 value in argument to tgbotapi.NewMessage

CedricRaison avatar Mar 16 '22 12:03 CedricRaison

My go skills are not enough but from what I understand, we would need to change this line in the Types.go file.

From what I have read it isn't possible to have 2 types for the same field. But the type json.number could be the solution from this stackoverflow link.

https://stackoverflow.com/questions/24480835/decoding-json-int-into-string/24480906#24480906.

Edit: from the new 1.18 go release, Generics may potentially be the solution.

CedricRaison avatar Mar 16 '22 13:03 CedricRaison

in my practice (6+ years) no one uses channel name as chat_id

temamagic avatar Apr 18 '22 20:04 temamagic

I use it so my bot can post content on my channel to automatically update followers. And I think a lot of people use it

CedricRaison avatar Apr 19 '22 05:04 CedricRaison

All groups and channels can be addressed by their ID, you don't need to use the username. The library is structured in such a way that every request accepts a username field, but there aren't helpers for it. You can construct the MessageConfig with a username yourself if you really want to.

Syfaro avatar Apr 19 '22 14:04 Syfaro