golang tgbotapi v5 send sticker

huangapple go评论77阅读模式
英文:

golang tgbotapi v5 send sticker

问题

我正在编写一个小的Go机器人。我使用了这个库https://pkg.go.dev/github.com/go-telegram-bot-api/telegram-bot-api/v5@v5.5.1,在版本4时,发送贴纸的代码是有效的:

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

但是当我决定升级到版本5时,贴纸就无法发送了。现在我无法弄清楚如何发送它们。有人可以帮忙解决这个问题吗?

英文:

I'm writing a small go bot. I took this library https://pkg.go.dev/github.com/go-telegram-bot-api/telegram-bot-api/v5@v5.5.1 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?

答案1

得分: 1

我认为使用该库的方式已经改变了。

函数NewStickerShare不再存在。相反,你需要使用新的函数NewSticker

type RequestFileData interface {
	NeedsUpload() bool
	UploadData() (string, io.Reader, error)
	SendData() string
}
  • 我们需要为你的Sticker实现这个约定,或者找到库中已经实现的结构体。可能,库已经为我们提供了一些东西,现在我们有了实现这些方法的FileID结构体。

因此,当你想要发送Sticker的ID时,你需要开发类似以下的代码:

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

如果你需要更多实现了RequestFileData约定的结构体,该库提供了一些:FilePathFileReader ... 每个结构体都有自己的用途。

希望对你有所帮助 golang tgbotapi v5 send sticker

英文:

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 golang tgbotapi v5 send sticker

huangapple
  • 本文由 发表于 2022年8月29日 01:56:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/73520979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定