download.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package telegram
  2. import (
  3. "context"
  4. "io"
  5. "net/http"
  6. "strings"
  7. "github.com/pkg/errors"
  8. )
  9. func (b *Bot) downloadAttachment(ctx context.Context, message *Message) (*Attachment, error) {
  10. var fileID, fileName, mimeType string
  11. switch {
  12. case len(message.Photo) > 0:
  13. fileID = message.GetMaxPhotoFileID()
  14. case message.Animation != nil:
  15. fileID = message.Animation.FileID
  16. fileName = message.Animation.FileName
  17. mimeType = message.Animation.MimeType
  18. case message.Audio != nil:
  19. fileID = message.Audio.FileID
  20. fileName = message.Audio.FileName
  21. mimeType = message.Audio.MimeType
  22. case message.Document != nil:
  23. fileID = message.Document.FileID
  24. fileName = message.Document.FileName
  25. mimeType = message.Document.MimeType
  26. case message.Video != nil:
  27. fileID = message.Video.FileID
  28. fileName = message.Video.FileName
  29. mimeType = message.Video.MimeType
  30. case message.VideoNote != nil:
  31. fileID = message.VideoNote.FileID
  32. case message.Voice != nil:
  33. fileID = message.Voice.FileID
  34. mimeType = message.Voice.MimeType
  35. }
  36. if fileID == "" {
  37. return nil, nil
  38. }
  39. attachment, err := b.downloadFileID(ctx, fileID)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if fileName != "" {
  44. attachment.FileName = fileName
  45. }
  46. if mimeType != "" {
  47. attachment.MimeType = mimeType
  48. }
  49. return attachment, nil
  50. }
  51. // downloadFileId download file with fileID, return Blob struct.
  52. func (b *Bot) downloadFileID(ctx context.Context, fileID string) (*Attachment, error) {
  53. file, err := b.GetFile(ctx, fileID)
  54. if err != nil {
  55. return nil, err
  56. }
  57. data, err := b.downloadFilepath(ctx, file.FilePath)
  58. if err != nil {
  59. return nil, err
  60. }
  61. blob := &Attachment{
  62. FileName: file.FilePath,
  63. Data: data,
  64. FileSize: file.FileSize,
  65. }
  66. return blob, nil
  67. }
  68. // downloadFilepath download file with filepath, you can get filepath by calling GetFile.
  69. func (b *Bot) downloadFilepath(ctx context.Context, filePath string) ([]byte, error) {
  70. apiURL, err := b.apiURL(ctx)
  71. if err != nil {
  72. return nil, err
  73. }
  74. idx := strings.LastIndex(apiURL, "/bot")
  75. if idx < 0 {
  76. return nil, ErrInvalidToken
  77. }
  78. fileURL := apiURL[:idx] + "/file" + apiURL[idx:]
  79. resp, err := http.Get(fileURL + "/" + filePath)
  80. if err != nil {
  81. return nil, errors.Wrap(err, "fail to http.Get")
  82. }
  83. defer resp.Body.Close()
  84. body, err := io.ReadAll(resp.Body)
  85. if err != nil {
  86. return nil, errors.Wrap(err, "fail to io.ReadAll")
  87. }
  88. return body, nil
  89. }