|
| 1 | +# Attachments |
| 2 | + |
| 3 | +Incoming messages carry attachments as raw dicts in `message.attachments`. |
| 4 | +FastVK also parses them into **typed models**, so you get autocomplete and |
| 5 | +helpers instead of dictionary digging. |
| 6 | + |
| 7 | +## Content type |
| 8 | + |
| 9 | +```python |
| 10 | +@bot.message() |
| 11 | +async def any_message(message: Message) -> None: |
| 12 | + print(message.content_type) # "text", "photo", "audio_message", ... |
| 13 | + print(message.content_types) # {"text", "photo"} — everything present |
| 14 | +``` |
| 15 | + |
| 16 | +## Filtering by content type |
| 17 | + |
| 18 | +```python |
| 19 | +from fastvk.filters import ContentType, HasAttachment |
| 20 | + |
| 21 | +@bot.message(ContentType("photo")) |
| 22 | +async def on_photo(message: Message) -> None: |
| 23 | + await message.answer("Nice photo!") |
| 24 | + |
| 25 | +@bot.message(ContentType("audio_message", "doc")) |
| 26 | +async def on_media(message: Message) -> None: |
| 27 | + ... |
| 28 | + |
| 29 | +# any attachment at all |
| 30 | +@bot.message(HasAttachment()) |
| 31 | +async def has_something(message: Message) -> None: |
| 32 | + ... |
| 33 | + |
| 34 | +# any of the given types |
| 35 | +@bot.message(HasAttachment("video", "wall")) |
| 36 | +async def video_or_repost(message: Message) -> None: |
| 37 | + ... |
| 38 | +``` |
| 39 | + |
| 40 | +Pass `"text"` to `ContentType` to match plain-text messages. |
| 41 | + |
| 42 | +## Typed accessors |
| 43 | + |
| 44 | +```python |
| 45 | +@bot.message(ContentType("photo")) |
| 46 | +async def on_photo(message: Message) -> None: |
| 47 | + photo = message.photos[0] |
| 48 | + await message.answer(f"{photo.largest.width}×{photo.largest.height}\n{photo.url}") |
| 49 | +``` |
| 50 | + |
| 51 | +| Accessor | Returns | |
| 52 | +|---|---| |
| 53 | +| `message.typed_attachments` | `list` of typed models (`dict` for unknown types) | |
| 54 | +| `message.photos` | `list[Photo]` | |
| 55 | +| `message.docs` | `list[Document]` | |
| 56 | +| `message.videos` | `list[Video]` | |
| 57 | +| `message.audio_messages` | `list[AudioMessage]` | |
| 58 | +| `message.sticker` | `Sticker | None` | |
| 59 | + |
| 60 | +### Photo |
| 61 | + |
| 62 | +```python |
| 63 | +photo.attachment_string # "photo-1_2" — ready for the attachment= param |
| 64 | +photo.sizes # list[PhotoSize] |
| 65 | +photo.largest # PhotoSize with the most pixels (or None) |
| 66 | +photo.url # URL of the largest size |
| 67 | +``` |
| 68 | + |
| 69 | +All models expose `attachment_string` (e.g. `doc-1_2_abcdef`), the fields VK |
| 70 | +returns, plus `raw` with the untouched dict. |
| 71 | + |
| 72 | +Available models (import from `fastvk.types`): `Photo`, `Video`, `Audio`, |
| 73 | +`Document`, `AudioMessage`, `Sticker`, `Graffiti`, `Link`, `Poll`, `WallPost`. |
| 74 | + |
| 75 | +## Sending several attachments |
| 76 | + |
| 77 | +```python |
| 78 | +await message.answer_media_group(["photo1_2", "doc1_3"], caption="Files") |
| 79 | +``` |
0 commit comments