sendMediaGroupRaw method

FutureOr<Map> sendMediaGroupRaw({
  1. required Map parameters,
  2. required TelegramClientCallApiInvoke callApiInvoke,
  3. required TelegramClientData telegramClientData,
})

Implementation

FutureOr<Map> sendMediaGroupRaw({
  required Map parameters,
  required TelegramClientCallApiInvoke callApiInvoke,
  required TelegramClientData telegramClientData,
}) async {
  final dynamic target_chat_id =
      TgUtils.parse_all_chat_id(parameters: parameters);
  if (target_chat_id is String &&
      RegExp(r"^((@)[a-z0-9_]+)$", caseSensitive: false)
          .hashData(target_chat_id)) {
    final Map search_public_chat = await callApiInvoke(
      parameters: {
        "@type": "searchPublicChat",
        "username": (target_chat_id)
            .replaceAll(RegExp(r"@", caseSensitive: false), ""),
      },
      is_invoke_no_relevance: true,
    );
    if (search_public_chat["@type"] == "chat") {
      parameters["chat_id"] = search_public_chat["id"];
    } else {
      return search_public_chat;
    }
  }
  final Map request_parameters = <dynamic, dynamic>{
    "@type": "sendMessageAlbum",
    "chat_id": parameters["chat_id"],
    "options": <String, dynamic>{
      "@type": "messageSendOptions",
    },
    "input_message_contents": <dynamic>[],
  };

  final List<String> options_parameters = [
    "protect_content",
    "disable_notification",
  ];
  for (var option_parameter in options_parameters) {
    if (parameters[option_parameter] is bool) {
      request_parameters["options"][option_parameter] =
          parameters[option_parameter];
    }
  }

  if (parameters["only_preview"] is bool) {
    request_parameters["only_preview"] = parameters["only_preview"];
  }
  final List<String> input_message_content_parameters = [
    "has_spoiler",
    "disable_web_page_preview",
  ];
  for (var option_parameter in input_message_content_parameters) {
    if (parameters[option_parameter] is bool) {
      request_parameters["input_message_content"][option_parameter] =
          parameters[option_parameter];
    }
  }

  if (parameters.containsKey("message_thread_id")) {
    request_parameters["message_thread_id"] =
        (parameters["message_thread_id"]);
  }

  if (parameters.containsKey("message_id")) {
    request_parameters["message_id"] =
        TgUtils.messageApiToTdlib(parameters["message_id"]);
  }
  if (parameters.containsKey("inline_message_id")) {
    request_parameters["inline_message_id"] = parameters["inline_message_id"];
  }
  if (parameters["reply_to_message_id"] is int) {
    request_parameters["reply_to"] = {
      "@type": "inputMessageReplyToMessage",
      "chat_id": request_parameters["chat_id"],
      "message_id":
          TgUtils.messageApiToTdlib(request_parameters["message_id"]),
    };
  } else if (parameters["reply_to_story_id"] is int) {}
  Map? formatted_text = {
    "@type": "formattedText",
  };

  if (RegExp(r"^(sendMessage|editMessageText)$", caseSensitive: false)
      .hashData(parameters["@type"])) {
    formatted_text["text"] = parameters["text"];
  } else {
    if (parameters["caption"] is String) {
      formatted_text["text"] = parameters["caption"];
      if ((formatted_text["text"] as String).isEmpty) {
        formatted_text = null;
      }
    } else {
      formatted_text = null;
    }
  }

  final String parse_mode = () {
    if (parameters["parse_mode"] is String) {
      return parameters["parse_mode"];
    }
    return "";
  }();
  if (formatted_text != null) {
    if (parse_mode == "html") {
      formatted_text = tdlib.td_execute(
        {
          "@type": 'parseTextEntities',
          "parse_mode": {
            "@type": "textParseModeHTML",
          },
          "text": formatted_text["text"],
        },
      );
    } else if (parse_mode == "markdown") {
      formatted_text = tdlib.td_execute(
        {
          "@type": 'parseTextEntities',
          "parse_mode": {
            "@type": "textParseModeMarkdown",
          },
          "text": formatted_text["text"],
        },
      );
    }
  }

  if (RegExp(r"^(sendMessage|editMessageText)$", caseSensitive: false)
      .hashData(parameters["@type"])) {
    request_parameters["input_message_content"]["@type"] = "inputMessageText";

    if (formatted_text != null) {
      request_parameters["input_message_content"]["text"] = formatted_text;
    }
  } else {
    if (formatted_text != null) {
      request_parameters["input_message_content"]["caption"] = formatted_text;
    }
    if (RegExp(r"^(sendAudio)$", caseSensitive: false)
        .hashData(parameters["@type"])) {
      request_parameters["input_message_content"]["@type"] =
          "inputMessageAudio";
      request_parameters["input_message_content"]["audio"] =
          (await TgUtils.typeFile(
        content: parameters["audio"],
        directory_temp: directory_temp,
      ));
    }
    if (RegExp(r"^(sendPhoto)$", caseSensitive: false)
        .hashData(parameters["@type"])) {
      request_parameters["input_message_content"]["@type"] =
          "inputMessagePhoto";
      request_parameters["input_message_content"]["photo"] =
          (await TgUtils.typeFile(
        content: parameters["photo"],
        directory_temp: directory_temp,
      ));
    }
    if (RegExp(r"^(sendAnimation)$", caseSensitive: false)
        .hashData(parameters["@type"])) {
      request_parameters["input_message_content"]["@type"] =
          "inputMessageAnimation";
      request_parameters["input_message_content"]["animation"] =
          (await TgUtils.typeFile(
        content: parameters["animation"],
        directory_temp: directory_temp,
      ));
    }
    if (RegExp(r"^(sendDocument)$", caseSensitive: false)
        .hashData(parameters["@type"])) {
      request_parameters["input_message_content"]["@type"] =
          "inputMessageDocument";
      request_parameters["input_message_content"]["document"] =
          (await TgUtils.typeFile(
        content: parameters["document"],
        directory_temp: directory_temp,
      ));
    }
    if (RegExp(r"^(sendSticker)$", caseSensitive: false)
        .hashData(parameters["@type"])) {
      request_parameters["input_message_content"]["@type"] =
          "inputMessageSticker";
      request_parameters["input_message_content"]["sticker"] =
          (await TgUtils.typeFile(
        content: parameters["sticker"],
        directory_temp: directory_temp,
      ));
    }
    if (RegExp(r"^(sendVideo)$", caseSensitive: false)
        .hashData(parameters["@type"])) {
      request_parameters["input_message_content"]["@type"] =
          "inputMessageVideo";
      request_parameters["input_message_content"]["video"] =
          (await TgUtils.typeFile(
        content: parameters["video"],
        directory_temp: directory_temp,
      ));
    }

    if (RegExp(r"^(sendVoice)$", caseSensitive: false)
        .hashData(parameters["@type"])) {
      request_parameters["input_message_content"]["@type"] =
          "inputMessageVoiceNote";
      request_parameters["input_message_content"]["voice_note"] =
          (await TgUtils.typeFile(
        content: parameters["voice"],
        directory_temp: directory_temp,
      ));
    }
  }
  if (parameters["reply_markup"] is Map) {
    final Map? reply_markup = TgUtils.replyMarkupTgApiToTdlib(
        replyMarkup: parameters["reply_markup"]);
    if (reply_markup != null) {
      request_parameters["reply_markup"] = reply_markup;
    }
  }

  final Map message_send = await callApiInvoke(
    parameters: request_parameters,
  );
  if (message_send["@type"] == "message") {
    if (message_send["sending_state"] is Map == false) {
      final Map json_message = await message_Message(
        message: message_send,
        is_lite: false,
        telegramClientData: telegramClientData,
        isUseCache: false,
        durationCacheExpire: null,
      );

      return (json_message);
    }
  }
  if (message_send["@type"] == "error") {
    return message_send;
  }
  final Completer<Map> completer = Completer<Map>();

  final EventEmitterListener listen = on(
    event_name: event_update,
    onUpdate: (UpdateTelegramClient updateTelegramClient) async {
      if (updateTelegramClient.telegramClientData.telegramClientType ==
          TelegramClientType.tdlib) {
        if (updateTelegramClient.telegramClientData.tdlib_client_id !=
            telegramClientData.tdlib_client_id) {
          return;
        }
        Map updateOrigin = updateTelegramClient.rawData;
        if (!["updateMessageSendSucceeded", "updateMessageSendFailed"]
            .contains(updateOrigin["@type"])) {
          return;
        }

        if (updateOrigin["message"] is Map &&
            updateOrigin["message"]["chat_id"] == message_send["chat_id"] &&
            updateOrigin["old_message_id"] == message_send["id"]) {
          // result = updateOrigin;
          completer.complete(updateOrigin);
        }
      }
    },
    onError: (Object error, StackTrace stackTrace) {},
  );
  final Map result = await completer.future.timeout(
    tdlib.invoke_time_out,
    onTimeout: () {
      return {
        "@type": "error",
        "invoke_request": request_parameters,
      };
    },
  );
  event_emitter.off(listener: listen);

  if (result["@type"] is String) {
    // tdlib.task_decrease();
    event_emitter.off(listener: listen);

    if (result["@type"] == "error") {
      throw result;
    }
    result.remove("@type");
    if (result["message"] is Map) {
      final Map json_message = await message_Message(
        message: result["message"],
        is_lite: false,
        telegramClientData: telegramClientData,
        isUseCache: false,
        durationCacheExpire: null,
      );

      return (json_message);
    }
  }
  throw result;
}