2023-05-26 09:43:51 +08:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2023-05-29 13:29:21 +08:00
|
|
|
"strings"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-05-26 09:43:51 +08:00
|
|
|
)
|
|
|
|
|
2023-07-14 00:18:44 +08:00
|
|
|
func (b *Bot) downloadAttachment(ctx context.Context, message *Message) (*Attachment, error) {
|
|
|
|
var fileID, fileName, mimeType string
|
|
|
|
switch {
|
|
|
|
case len(message.Photo) > 0:
|
|
|
|
fileID = message.GetMaxPhotoFileID()
|
|
|
|
case message.Animation != nil:
|
|
|
|
fileID = message.Animation.FileID
|
|
|
|
fileName = message.Animation.FileName
|
|
|
|
mimeType = message.Animation.MimeType
|
|
|
|
case message.Audio != nil:
|
|
|
|
fileID = message.Audio.FileID
|
|
|
|
fileName = message.Audio.FileName
|
|
|
|
mimeType = message.Audio.MimeType
|
|
|
|
case message.Document != nil:
|
|
|
|
fileID = message.Document.FileID
|
|
|
|
fileName = message.Document.FileName
|
|
|
|
mimeType = message.Document.MimeType
|
|
|
|
case message.Video != nil:
|
|
|
|
fileID = message.Video.FileID
|
|
|
|
fileName = message.Video.FileName
|
|
|
|
mimeType = message.Video.MimeType
|
|
|
|
case message.VideoNote != nil:
|
|
|
|
fileID = message.VideoNote.FileID
|
|
|
|
case message.Voice != nil:
|
|
|
|
fileID = message.Voice.FileID
|
|
|
|
mimeType = message.Voice.MimeType
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileID == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
attachment, err := b.downloadFileID(ctx, fileID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileName != "" {
|
|
|
|
attachment.FileName = fileName
|
|
|
|
}
|
|
|
|
|
|
|
|
if mimeType != "" {
|
|
|
|
attachment.MimeType = mimeType
|
|
|
|
}
|
|
|
|
|
|
|
|
return attachment, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// downloadFileId download file with fileID, return Blob struct.
|
|
|
|
func (b *Bot) downloadFileID(ctx context.Context, fileID string) (*Attachment, error) {
|
2023-05-29 19:49:05 +08:00
|
|
|
file, err := b.GetFile(ctx, fileID)
|
2023-05-26 09:43:51 +08:00
|
|
|
if err != nil {
|
2023-07-14 00:18:44 +08:00
|
|
|
return nil, err
|
2023-05-26 09:43:51 +08:00
|
|
|
}
|
2023-07-14 00:18:44 +08:00
|
|
|
data, err := b.downloadFilepath(ctx, file.FilePath)
|
2023-05-26 09:43:51 +08:00
|
|
|
if err != nil {
|
2023-07-14 00:18:44 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blob := &Attachment{
|
|
|
|
FileName: file.FilePath,
|
|
|
|
Data: data,
|
|
|
|
FileSize: file.FileSize,
|
2023-05-26 09:43:51 +08:00
|
|
|
}
|
|
|
|
|
2023-07-14 00:18:44 +08:00
|
|
|
return blob, nil
|
2023-05-26 09:43:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// downloadFilepath download file with filepath, you can get filepath by calling GetFile.
|
2023-05-29 19:49:05 +08:00
|
|
|
func (b *Bot) downloadFilepath(ctx context.Context, filePath string) ([]byte, error) {
|
|
|
|
apiURL, err := b.apiURL(ctx)
|
2023-05-29 13:29:21 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := strings.LastIndex(apiURL, "/bot")
|
|
|
|
if idx < 0 {
|
|
|
|
return nil, ErrInvalidToken
|
2023-05-26 09:43:51 +08:00
|
|
|
}
|
|
|
|
|
2023-05-29 13:29:21 +08:00
|
|
|
fileURL := apiURL[:idx] + "/file" + apiURL[idx:]
|
|
|
|
|
|
|
|
resp, err := http.Get(fileURL + "/" + filePath)
|
2023-05-26 09:43:51 +08:00
|
|
|
if err != nil {
|
2023-09-17 22:55:13 +08:00
|
|
|
return nil, errors.Wrap(err, "fail to http.Get")
|
2023-05-26 09:43:51 +08:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2023-09-17 22:55:13 +08:00
|
|
|
return nil, errors.Wrap(err, "fail to io.ReadAll")
|
2023-05-26 09:43:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return body, nil
|
|
|
|
}
|