makeParametersApi method

Map<String, dynamic> makeParametersApi(
  1. Map<String, dynamic> parameters
)

getRandom uuid for parameters @ convert api json method bot api to tdlib

Implementation

/// convert api json method [bot api] to tdlib
Map<String, dynamic> makeParametersApi(Map<String, dynamic> parameters) {
  Map<String, dynamic> jsonResult = {"@type": ""};
  try {
    String regexMethodSend =
        r"^(sendMessage|sendPhoto|sendVideo|sendAudio|sendVoice|sendDocument|sendSticker|sendAnimation|editMessage(Text))$";
    if (RegExp(regexMethodSend, caseSensitive: false)
        .hashData(parameters["@type"])) {
      jsonResult["@type"] = "sendMessage";
      jsonResult["options"] = {
        "@type": "messageSendOptions",
      };
      parameters.forEach((key, value) {
        if (value is bool) {
          try {
            jsonResult["options"][key.toString()] = value;
          } catch (e) {}
        }
      });
      if (RegExp("editMessage(Text)", caseSensitive: false)
          .hashData(parameters["@type"])) {
        jsonResult["@type"] = parameters["@type"];
      }

      if (parameters["disable_web_page_preview"] is bool == false) {
        parameters["disable_web_page_preview"] = false;
      }
      if (parameters["clear_draft"] is bool == false) {
        parameters["clear_draft"] = false;
      }

      jsonResult["input_message_content"] = {
        "@type": "inputMessageText",
        "disable_web_page_preview": parameters["disable_web_page_preview"],
        "clear_draft": parameters["clear_draft"],
      };
      jsonResult["chat_id"] = parameters["chat_id"];
      if (parameters["disable_notification"] is bool) {
        jsonResult["disable_notification"] =
            parameters["reply_to_message_id"];
      }
      if (parameters["reply_to_message_id"] is int) {
        jsonResult["reply_to_message_id"] = parameters["reply_to_message_id"];
        jsonResult["reply_to"] = {
          "@type": "inputMessageReplyToMessage",
          "chat_id": parameters["chat_id"],
          "message_id": parameters["reply_to_message_id"],
        };
      }
      if (parameters["reply_markup"] is Map) {
        jsonResult["reply_markup"] = parameters["reply_markup"];
      }
      if (parameters["parse_mode"] is String) {
      } else {
        parameters["parse_mode"] = "";
      }
      if (parameters["entities"] is List == false) {
        parameters["entities"] = [];
      }
      if (parameters.containsKey("message_id")) {
        jsonResult["message_id"] = parameters["message_id"];
      }
      if (parameters.containsKey("reply_markup")) {
        jsonResult["reply_markup"] = replyMarkup(parameters["reply_markup"]);
      }
      if (RegExp(r"^(sendMessage|editMessageText)$", caseSensitive: false)
          .hashData(parameters["@type"])) {
        var text = parseMode(
          parameters["text"].toString(),
          parameters["parse_mode"],
          parameters["entities"],
        );
        jsonResult["input_message_content"]["@type"] = "inputMessageText";
        jsonResult["input_message_content"]["text"] = text;
      }
      if (RegExp(r"^(sendPhoto)$", caseSensitive: false)
          .hashData(parameters["@type"])) {
        var getDetailFile = typeFile(parameters["photo"]);
        jsonResult["input_message_content"]["@type"] = "inputMessagePhoto";
        jsonResult["input_message_content"]["photo"] = getDetailFile;
      }
      if (RegExp(r"^(sendVoice)$", caseSensitive: false)
          .hashData(parameters["@type"])) {
        var getDetailFile = typeFile(parameters["voice"]);
        jsonResult["input_message_content"]["@type"] =
            "inputMessageVoiceNote";
        jsonResult["input_message_content"]["voice_note"] = getDetailFile;
      }
      if (RegExp(r"^(sendSticker)$", caseSensitive: false)
          .hashData(parameters["@type"])) {
        var getDetailFile = typeFile(parameters["sticker"]);
        jsonResult["input_message_content"]["@type"] = "inputMessageSticker";
        jsonResult["input_message_content"]["sticker"] = getDetailFile;
      }
      if (RegExp(r"^(sendAnimation)$", caseSensitive: false)
          .hashData(parameters["@type"])) {
        var getDetailFile = typeFile(parameters["animation"]);
        jsonResult["input_message_content"]["@type"] =
            "inputMessageAnimation";
        jsonResult["input_message_content"]["animation"] = getDetailFile;
      }
      if (RegExp(r"^(sendDocument)$", caseSensitive: false)
          .hashData(parameters["@type"])) {
        var getDetailFile = typeFile(parameters["document"]);
        jsonResult["input_message_content"]["@type"] = "inputMessageDocument";
        jsonResult["input_message_content"]["document"] = getDetailFile;
      }
      if (RegExp(r"^(sendAudio)$", caseSensitive: false)
          .hashData(parameters["@type"])) {
        var getDetailFile = typeFile(parameters["audio"]);
        jsonResult["input_message_content"]["@type"] = "inputMessageAudio";
        jsonResult["input_message_content"]["audio"] = getDetailFile;
      }
      if (RegExp(r"^(sendVideo)$", caseSensitive: false)
          .hashData(parameters["@type"])) {
        var getDetailFile = typeFile(parameters["video"]);
        jsonResult["input_message_content"]["@type"] = "inputMessageVideo";
        jsonResult["input_message_content"]["video"] = getDetailFile;
      }
      if (!RegExp(r"^(sendMessage|sendLocation|sendSticker)$",
              caseSensitive: false)
          .hashData(parameters["@type"])) {
        if (parameters["caption"] != null) {
          var caption = parseMode(
            (parameters["caption"] != null)
                ? parameters["caption"].toString()
                : "",
            parameters["parse_mode"],
            parameters["entities"],
          );
          jsonResult["input_message_content"]["caption"] = caption;
        }
      }
      return jsonResult;
    }

    if (RegExp(r"^answerInlineQuery$", caseSensitive: false)
        .hashData(parameters["@type"])) {
      parameters["@type"] = "answerInlineQuery";

      if (parameters["results"] is List) {
        List array_results = [];
        for (var i = 0; i < parameters["results"].length; i++) {
          Map loop_data = parameters["results"][i];

          if (loop_data["type"] is String) {
            loop_data["@type"] =
                "inputInlineQueryResult${loop_data["type"].toString().replaceAll(RegExp(r"inputInlineQueryResult", caseSensitive: false), "")}";
            loop_data.remove("type");
          }
          if (loop_data["id"] is String == false) {
            loop_data["id"] ??= "$i";
            loop_data["id"] = (loop_data["id"].toString());
          }

          if (loop_data["reply_markup"] is Map) {
            loop_data["reply_markup"] =
                (replyMarkup(loop_data["reply_markup"]));
          }
          array_results.add(loop_data);
        }
        parameters["results"] = array_results;
      }
      return parameters;
    }

    return parameters;
  } catch (e) {
    return parameters;
  }
}