Skip to content

# Media Groups

As you know, Telegram have the feature named Media Groups. Media groups have several differences with the common messages:

- Each media group message contains special media group id
- Media group may have special caption which will be visible if only the first message of media group contains caption
- In most cases media groups came with long polling/webhooks in one pack
- Media groups can be one of three types:
  - Visual (image/video)
  - Documents
  - Playlists (audio)

## Specific of media groups in libraries

Row updates

In tgbotapi there is no any additional handling of media groups by default and in case you will use simple bot.getUpdates, you will get the list of row updates and media groups will be included in this list as separated messages with MediaGroupPartContent. In that case you may use convertWithMediaGroupUpdates to be able to work with media groups as will be described below

In case you are using standard long polling (one of alternatives is telegramBotWithBehaviourAndLongPolling) or webhooks updates will be converted uner the hood and as a result, you will take media groups as a content in one message:

    telegramBotWithBehaviourAndLongPolling(
      "token"
    ) {
      onVisualGallery { // it: CommonMessage<MediaGroupContent<VisualMediaGroupPartContent>>
        it.content // MediaGroupContent<VisualMediaGroupPartContent>
        it.content.group // List<MediaGroupCollectionContent.PartWrapper<VisualMediaGroupPartContent>>
        it.content.group.forEach { // it: MediaGroupCollectionContent.PartWrapper<VisualMediaGroupPartContent>
          it.messageId // source message id for current media group part
          it.sourceMessage // source message for current media group part
          it.content // VisualMediaGroupPartContent
          println(it.content) // will print current content part info
        }
      }
    }

KDocs:

- onVisualGallery
- MediaGroupContent
- VisualMediaGroupPartContent
- MediaGroupCollectionContent.PartWrapper

In two words, in difference with row Telegram Bot API, you will take media groups in one message instead of messages list.