Home > Mobile >  golang tgbotapi v5 send sticker
golang tgbotapi v5 send sticker

Time:08-29

I'm writing a small go bot. I took this library https://pkg.go.dev/github.com/go-telegram-bot-api/telegram-bot-api/[email protected] and while it was version 4 everything worked fine for sending stickers the following code worked:

bot.Send(tgbotapi.NewStickerShare(update.Message.Chat.ID, "hereIsTheIDOfTheSticker"))

But when I decided to upgrade to version 5, the stickers broke. And now I can't figure out how to send them. Can someone help with this question?

CodePudding user response:

I think the way that the library are using it has changed.

The function NewStickerShare doesn't exists any more. instead, you need to use the new function called NewSticker:

  • The NewSticker function accepts two parameters:
  • As you can see, the RequestFileData interface has the following contract:
type RequestFileData interface {
    NeedsUpload() bool
    UploadData() (string, io.Reader, error)
    SendData() string
}
  • We need to implement this contract for your Sticker OR find an already structure implemented by the library. Probably, the library provide something to us, and we have now the FileID structure implementing these methods.

From that, once you want to send the ID of the Sticker, you need to develop something like that:

stickerID := tgbotapi.FileID("hereIsTheIDOfTheSticker")
msg := tgbotapi.NewSticker(update.Message.Chat.ID, stickerID)
bot.Send(msg)

If you need more structures that implements the RequestFileData contract, the library provide some of them: FilePath, FileReader ... Each one has your own goal.

I hope it can help you :)

  •  Tags:  
  • go
  • Related