英文:
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
:
NewSticker
函数接受两个参数:- chatID
int64
- file
RequestFileData
- chatID
- 正如你所看到的,
RequestFileData
接口具有以下约定:
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
约定的结构体,该库提供了一些:FilePath
,FileReader
... 每个结构体都有自己的用途。
希望对你有所帮助
英文:
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:- chatID
int64
- file
RequestFileData
- chatID
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论