|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/gotd/td/fileid" |
| 5 | + "github.com/gotd/td/tg" |
| 6 | +) |
| 7 | + |
| 8 | +// encodeFileID encodes a parsed file location into the Bot API file_id and |
| 9 | +// file_unique_id strings. The unique-id derivation lives in file.go. |
| 10 | +func encodeFileID(f fileid.FileID) (fileID, uniqueID string) { |
| 11 | + fileID, _ = fileid.EncodeFileID(f) |
| 12 | + return fileID, fileUniqueID(f) |
| 13 | +} |
| 14 | + |
| 15 | +// convertMessageMedia maps an incoming tg media attachment onto the Bot API |
| 16 | +// Message media fields. Only the attachment types a bot can receive are mapped; |
| 17 | +// web pages and unsupported kinds are ignored. |
| 18 | +func convertMessageMedia(media tg.MessageMediaClass, r *Message) { |
| 19 | + switch media := media.(type) { |
| 20 | + case *tg.MessageMediaPhoto: |
| 21 | + if p, ok := media.Photo.(*tg.Photo); ok { |
| 22 | + r.Photo = photoSizesFromTg(p) |
| 23 | + } |
| 24 | + case *tg.MessageMediaDocument: |
| 25 | + if d, ok := media.Document.(*tg.Document); ok { |
| 26 | + setDocumentMedia(d, r) |
| 27 | + } |
| 28 | + case *tg.MessageMediaContact: |
| 29 | + r.Contact = &Contact{ |
| 30 | + PhoneNumber: media.PhoneNumber, |
| 31 | + FirstName: media.FirstName, |
| 32 | + LastName: media.LastName, |
| 33 | + UserID: media.UserID, |
| 34 | + VCard: media.Vcard, |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// photoSizesFromTg converts a tg.Photo into the Bot API list of photo sizes, |
| 40 | +// each carrying a usable file_id. |
| 41 | +func photoSizesFromTg(photo *tg.Photo) []PhotoSize { |
| 42 | + type sized interface { |
| 43 | + GetW() int |
| 44 | + GetH() int |
| 45 | + GetType() string |
| 46 | + } |
| 47 | + |
| 48 | + var out []PhotoSize |
| 49 | + for _, sz := range photo.Sizes { |
| 50 | + size, ok := sz.(sized) |
| 51 | + if !ok { |
| 52 | + continue |
| 53 | + } |
| 54 | + t := size.GetType() |
| 55 | + if t == "" { |
| 56 | + continue |
| 57 | + } |
| 58 | + |
| 59 | + var fileSize int |
| 60 | + switch s := sz.(type) { |
| 61 | + case *tg.PhotoSize: |
| 62 | + fileSize = s.Size |
| 63 | + case *tg.PhotoCachedSize: |
| 64 | + fileSize = len(s.Bytes) |
| 65 | + } |
| 66 | + |
| 67 | + fileID, uniqueID := encodeFileID(fileid.FromPhoto(photo, rune(t[0]))) |
| 68 | + out = append(out, PhotoSize{ |
| 69 | + FileID: fileID, |
| 70 | + FileUniqueID: uniqueID, |
| 71 | + Width: size.GetW(), |
| 72 | + Height: size.GetH(), |
| 73 | + FileSize: fileSize, |
| 74 | + }) |
| 75 | + } |
| 76 | + return out |
| 77 | +} |
| 78 | + |
| 79 | +// setDocumentMedia inspects a document's attributes and fills the matching Bot |
| 80 | +// API media field (sticker, video, video note, voice, audio, animation, or a |
| 81 | +// generic document). |
| 82 | +func setDocumentMedia(d *tg.Document, r *Message) { |
| 83 | + fileID, uniqueID := encodeFileID(fileid.FromDocument(d)) |
| 84 | + |
| 85 | + var ( |
| 86 | + fileName string |
| 87 | + width, height int |
| 88 | + duration int |
| 89 | + animated, round bool |
| 90 | + ) |
| 91 | + for _, attr := range d.Attributes { |
| 92 | + switch a := attr.(type) { |
| 93 | + case *tg.DocumentAttributeFilename: |
| 94 | + fileName = a.FileName |
| 95 | + case *tg.DocumentAttributeImageSize: |
| 96 | + width, height = a.W, a.H |
| 97 | + case *tg.DocumentAttributeVideo: |
| 98 | + width, height, duration, round = a.W, a.H, int(a.Duration), a.RoundMessage |
| 99 | + case *tg.DocumentAttributeAnimated: |
| 100 | + animated = true |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // The attribute set determines the concrete Bot API type. Order matters: |
| 105 | + // stickers, then animations, then video/voice/audio, else a plain document. |
| 106 | + for _, attr := range d.Attributes { |
| 107 | + switch a := attr.(type) { |
| 108 | + case *tg.DocumentAttributeSticker: |
| 109 | + r.Sticker = &Sticker{ |
| 110 | + FileID: fileID, |
| 111 | + FileUniqueID: uniqueID, |
| 112 | + Type: StickerRegular, |
| 113 | + Width: width, |
| 114 | + Height: height, |
| 115 | + Emoji: a.Alt, |
| 116 | + FileSize: int(d.Size), |
| 117 | + } |
| 118 | + return |
| 119 | + case *tg.DocumentAttributeAudio: |
| 120 | + if a.Voice { |
| 121 | + r.Voice = &Voice{ |
| 122 | + FileID: fileID, |
| 123 | + FileUniqueID: uniqueID, |
| 124 | + Duration: a.Duration, |
| 125 | + MIMEType: d.MimeType, |
| 126 | + FileSize: d.Size, |
| 127 | + } |
| 128 | + } else { |
| 129 | + r.Audio = &Audio{ |
| 130 | + FileID: fileID, |
| 131 | + FileUniqueID: uniqueID, |
| 132 | + Duration: a.Duration, |
| 133 | + Performer: a.Performer, |
| 134 | + Title: a.Title, |
| 135 | + FileName: fileName, |
| 136 | + MIMEType: d.MimeType, |
| 137 | + FileSize: d.Size, |
| 138 | + } |
| 139 | + } |
| 140 | + return |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + switch { |
| 145 | + case animated: |
| 146 | + r.Animation = &Animation{ |
| 147 | + FileID: fileID, |
| 148 | + FileUniqueID: uniqueID, |
| 149 | + Width: width, |
| 150 | + Height: height, |
| 151 | + Duration: duration, |
| 152 | + FileName: fileName, |
| 153 | + MIMEType: d.MimeType, |
| 154 | + FileSize: d.Size, |
| 155 | + } |
| 156 | + case round: |
| 157 | + r.VideoNote = &VideoNote{ |
| 158 | + FileID: fileID, |
| 159 | + FileUniqueID: uniqueID, |
| 160 | + Length: width, |
| 161 | + Duration: duration, |
| 162 | + FileSize: int(d.Size), |
| 163 | + } |
| 164 | + case width != 0 || height != 0: |
| 165 | + r.Video = &Video{ |
| 166 | + FileID: fileID, |
| 167 | + FileUniqueID: uniqueID, |
| 168 | + Width: width, |
| 169 | + Height: height, |
| 170 | + Duration: duration, |
| 171 | + FileName: fileName, |
| 172 | + MIMEType: d.MimeType, |
| 173 | + FileSize: d.Size, |
| 174 | + } |
| 175 | + default: |
| 176 | + r.Document = &Document{ |
| 177 | + FileID: fileID, |
| 178 | + FileUniqueID: uniqueID, |
| 179 | + FileName: fileName, |
| 180 | + MIMEType: d.MimeType, |
| 181 | + FileSize: d.Size, |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments