Go Telegram Bot API 从本地文件上传照片

Go Telegram Bot API upload photo from local file

我使用电报 github。com/go-telegram-bot-api/telegram-bot-api 后来我使用外部链接上传了照片: 简化后的代码是这样的:

url := `http://path-to-image/img.jpg`
msg := tgbotapi.NewPhotoUpload(groupID, nil)
msg.FileID = url
msg.Caption = "New photo"
bot.Send(msg)

但是现在,我的照片只能在封闭的本地网络中使用。类似 http://example.loc/img.jpg obviously do not work. So, I download a file and then try to upload it from disk or from memory. There are lots of examples here https://github.com/go-telegram-bot-api/telegram-bot-api/blob/master/bot_test.go 的链接 但是没有人工作。我尝试了所有的例子甚至更多,但我总是会遇到各种错误:

以此类推

有谁知道如何从磁盘或内存上传照片(更好)。提前致谢。

从本地磁盘上传图片的一种方法是读取文件,然后将字节数组传递给 FileBytes, wrap it with a Chattable like PhotoConfig 并通过 bot.send:

发送
photoBytes, err := ioutil.ReadFile("/your/local/path/to/picture.png")
if err != nil {
    panic(err)
}
photoFileBytes := tgbotapi.FileBytes{
    Name:  "picture",
    Bytes: photoBytes,
}
chatID := 12345678
message, err := bot.Send(tgbotapi.NewPhotoUpload(int64(chatID), photoFileBytes))

这里tgbotapi.NewPhotoUpload()为我们创建了一个PhotoConfig。