editMessageMedia method

Future<Message> editMessageMedia({
  1. dynamic chatId,
  2. int? messageId,
  3. String? inlineMessageId,
  4. InputMedia? media,
  5. String? parseMode,
  6. InlineKeyboardMarkup? replyMarkup,
})

Use this method to edit audio, document, photo, or video messages

If a message is a part of a message album, then it can be edited only to a photo or a video. Otherwise, message type can be changed arbitrarily. When inline message is edited, file can't be uploaded. Use previously uploaded file via its fileId or specify a URL.

On success, if the edited message was sent by the bot, the edited Message is returned, otherwise True is returned.

https://core.telegram.org/bots/api#editMessageMedia

Implementation

Future<Message> editMessageMedia(
    {dynamic chatId,
    int? messageId,
    String? inlineMessageId,
    InputMedia? media,
    String? parseMode,
    InlineKeyboardMarkup? replyMarkup}) async {
  if (inlineMessageId == null && (chatId == null || messageId == null)) {
    return Future.error(TelegramException(
        'Require either \'chatId\' and \'messageId\', or \'inlineMessageId\''));
  }
  if (chatId != null && (chatId is! String && chatId is! int)) {
    return Future.error(TelegramException(
        'Attribute \'chatId\' can only be either type of String or int'));
  }
  var requestUrl = _apiUri('editMessageMedia');
  var body = <String, dynamic>{
    'chat_id': chatId,
    'message_id': messageId,
    'inline_message_id': inlineMessageId,
    'media': media == null ? null : jsonEncode(media),
    'parse_mode': parseMode,
    'reply_markup': replyMarkup == null ? null : jsonEncode(replyMarkup),
  };

  List<MultipartFile> multiPartFiles = media == null
      ? []
      : [
          media.mediaFile,
          media is InputMediaWithThumbnail ? media.thumbnailFile : null
        ].whereType<MultipartFile>().toList();

  var res = multiPartFiles.isEmpty
      ? await HttpClient.httpPost(requestUrl, body: body)
      : await HttpClient.httpMultipartPost(requestUrl, multiPartFiles,
          body: body);

  if (res == true) {
    return Future.error(
        TelegramException('Edited message is NOT sent by the bot'));
  } else {
    return Message.fromJson(res);
  }
}