getChat method

Future<Map> getChat(
  1. dynamic chat_id, {
  2. bool is_detail = false,
  3. bool is_skip_reply_message = true,
  4. bool is_super_detail = false,
  5. bool is_more_detail = false,
  6. required int clientId,
  7. bool? isUseCache,
  8. Duration? durationCacheExpire,
  9. String? extra,
})

getChat return result like bot api:

Implementation

Future<Map> getChat(
  dynamic chat_id, {
  bool is_detail = false,
  bool is_skip_reply_message = true,
  // required bool spasa,
  bool is_super_detail = false,
  bool is_more_detail = false,
  required int clientId,
  bool? isUseCache,
  Duration? durationCacheExpire,
  String? extra,
}) async {
  try {
    if (chat_id is String &&
        RegExp(r"^((@)?[a-z0-9_]+)$", caseSensitive: false)
            .hashData(chat_id)) {
      var search_public_chat = await invoke(
        "searchPublicChat",
        parameters: {
          "username":
              chat_id.replaceAll(RegExp(r"@", caseSensitive: false), ""),
        },
        isUseCache: isUseCache,
        durationCacheExpire: durationCacheExpire,
        clientId: clientId,
        extra: extra,
      );
      if (search_public_chat["@type"] == "chat") {
        chat_id = search_public_chat["id"];
      }
    }
    var getchat = await invoke(
      "getChat",
      parameters: {
        "chat_id": chat_id,
      },
      isUseCache: isUseCache,
      durationCacheExpire: durationCacheExpire,
      clientId: clientId,
      extra: extra,
    );
    Map json = {};
    if (RegExp(r"^(chat)$", caseSensitive: false)
        .hashData(getchat["@type"])) {
      var type_chat = getchat["type"]["@type"]
          .toString()
          .toLowerCase()
          .replaceAll(RegExp("chattype", caseSensitive: false), "");
      if (type_chat == "supergroup") {
        var getSupergroup = await invoke(
          "getSupergroup",
          parameters: {
            "supergroup_id": int.parse(
              chat_id
                  .toString()
                  .replaceAll(RegExp("^-100", caseSensitive: false), ""),
            ),
          },
          isUseCache: isUseCache,
          durationCacheExpire: durationCacheExpire,
          clientId: clientId,
          extra: extra,
        );
        json["id"] = chat_id;
        json["title"] = getchat["title"];
        if (getSupergroup["username"] is String) {
          json["username"] = getSupergroup["username"];
        }

        if (getSupergroup["usernames"] is Map) {
          Map get_user_usernames = (getSupergroup["usernames"] as Map);
          json["usernames"] = getSupergroup["usernames"];
          if (get_user_usernames["active_usernames"] is List) {
            if ((get_user_usernames["active_usernames"] as List).isNotEmpty) {
              json["username"] =
                  (get_user_usernames["active_usernames"] as List).first;
            }
          }
        }
        if (getSupergroup["status"] is Map) {
          json["status"] = getSupergroup["status"]["@type"]
              .toString()
              .toLowerCase()
              .replaceAll(
                  RegExp("chatMemberStatus", caseSensitive: false), "");
        }
        json["type"] =
            getchat["type"]["is_channel"] ? "channel" : "supergroup";
        json["detail"] = {
          "member_count": getSupergroup["member_count"],
          "administrator_count": 0,
          "restricted_count": 0,
          "banned_count": 0,
          "has_protected_content": getchat["has_protected_content"] ?? false,
          "is_marked_as_unread": getchat["is_marked_as_unread"] ?? false,
          "is_blocked": getchat["is_blocked"] ?? false,
          "has_scheduled_messages":
              getchat["has_scheduled_messages"] ?? false,
          "can_be_deleted_only_for_self":
              getchat["can_be_deleted_only_for_self"] ?? false,
          "can_be_deleted_for_all_users":
              getchat["can_be_deleted_for_all_users"] ?? false,
          "can_be_reported": getchat["can_be_reported"] ?? false,
          "default_disable_notification":
              getchat["default_disable_notification"] ?? false,
          "unread_count": getchat["unread_count"] ?? 0,
          "last_read_inbox_message_id":
              getchat["last_read_inbox_message_id"] ?? 0,
          "last_read_outbox_message_id":
              getchat["last_read_outbox_message_id"] ?? 0,
          "unread_mention_count": getchat["unread_mention_count"] ?? 0,
          "has_linked_chat": getSupergroup["has_linked_chat"],
          "has_location": getSupergroup["has_location"],
          "sign_messages": getSupergroup["sign_messages"],
          "is_slow_mode_enabled": getSupergroup["is_slow_mode_enabled"],
          "is_broadcast_group": getSupergroup["is_broadcast_group"],
          "is_verified": getSupergroup["is_verified"],
          "is_scam": getSupergroup["is_scam"],
          "is_fake": getSupergroup["is_fake"]
        };
        if (getSupergroup["username"].toString().isEmpty) {
          json.remove("username");
          json["type"] = getchat["type"]["is_channel"] ? "channel" : "group";
        }
        if (is_detail) {
          if (getchat["last_message"] is Map) {
            var last_message = await jsonMessage(
              getchat["last_message"],
              from_data: json,
              chat_data: json,
              clientId: clientId,
              isUseCache: isUseCache,
              durationCacheExpire: durationCacheExpire,
              is_skip_reply_message: true,
            );
            if (last_message["ok"] == true) {
              json["last_message"] = last_message["result"];
            }
          }
        }
        if (is_more_detail) {
          var getSuperGroupFullInfo = {};
          try {
            getSuperGroupFullInfo = await invoke(
              "getSupergroupFullInfo",
              parameters: {
                "supergroup_id": int.parse(chat_id
                    .toString()
                    .replaceAll(RegExp("^-100", caseSensitive: false), "")),
              },
              isUseCache: isUseCache,
              durationCacheExpire: durationCacheExpire,
              clientId: clientId,
              extra: extra,
            );
          } catch (e) {}
          if (getSuperGroupFullInfo["photo"] is Map) {
            json["profile_photo"] = {
              "id": getSuperGroupFullInfo["photo"]["id"],
              "path": "",
              "file_id": ""
            };
            if (getSuperGroupFullInfo["photo"]["@type"] == "chatPhoto") {
              if (getSuperGroupFullInfo["photo"]["sizes"] is List) {
                try {
                  var getPhoto = getSuperGroupFullInfo["photo"]["sizes"]
                      [getSuperGroupFullInfo["photo"]["sizes"].length - 1];
                  var getPathPhoto =
                      getPhoto["photo"]["local"]["path"] as String;
                  json["profile_photo"]["path"] = getPathPhoto;
                  (json["profile_photo"] as Map)
                      .addAll(getPhoto["photo"]["local"]);
                  json["profile_photo"]["file_id"] =
                      getPhoto["photo"]["remote"]["id"];
                  json["profile_photo"]["file_unique_id"] =
                      getPhoto["photo"]["remote"]["unique_id"];
                } catch (e) {}
              }
            }
          }
          if (getSuperGroupFullInfo["description"] is String) {
            json["description"] = getSuperGroupFullInfo["description"];
          }
          try {
            getSuperGroupFullInfo.forEach((key, value) {
              if (key != "@type") {
                try {
                  if (value is String) {
                    json["detail"][key.toString()] = value;
                  }
                  if (value is num) {
                    json["detail"][key.toString()] = value;
                  }
                  if (value is bool) {
                    json["detail"][key.toString()] = value;
                  }
                } catch (e) {}
              }
            });
          } catch (e) {}

          if (json["profile_photo"] is Map &&
              RegExp(r"^([0-9]+)$", caseSensitive: false)
                  .hashData(json["profile_photo"]["id"])) {
            try {
              json["profile_photo"]["id"] =
                  (int.parse(json["profile_photo"]["id"]));
            } catch (e) {}
          }
        }

        return {"ok": true, "result": json};
      } else if (type_chat == "basicgroup") {
        var getBasicGroup = await invoke(
          "getBasicGroup",
          parameters: {
            "basic_group_id": int.parse(chat_id
                .toString()
                .replaceAll(RegExp("^-", caseSensitive: false), "")),
          },
          isUseCache: isUseCache,
          durationCacheExpire: durationCacheExpire,
          clientId: clientId,
          extra: extra,
        );
        json["id"] = chat_id;
        json["title"] = getchat["title"];
        if (getBasicGroup["status"] is Map) {
          json["status"] = getBasicGroup["status"]["@type"]
              .toString()
              .toLowerCase()
              .replaceAll(
                  RegExp("chatMemberStatus", caseSensitive: false), "");
        }
        json["type"] = "group";
        json["detail"] = {
          "member_count": getBasicGroup["member_count"],
          "has_protected_content": getchat["has_protected_content"] ?? false,
          "is_marked_as_unread": getchat["is_marked_as_unread"] ?? false,
          "is_blocked": getchat["is_blocked"] ?? false,
          "has_scheduled_messages":
              getchat["has_scheduled_messages"] ?? false,
          "can_be_deleted_only_for_self":
              getchat["can_be_deleted_only_for_self"] ?? false,
          "can_be_deleted_for_all_users":
              getchat["can_be_deleted_for_all_users"] ?? false,
          "can_be_reported": getchat["can_be_reported"] ?? false,
          "default_disable_notification":
              getchat["default_disable_notification"] ?? false,
          "unread_count": getchat["unread_count"] ?? 0,
          "last_read_inbox_message_id":
              getchat["last_read_inbox_message_id"] ?? 0,
          "last_read_outbox_message_id":
              getchat["last_read_outbox_message_id"] ?? 0,
          "unread_mention_count": getchat["unread_mention_count"] ?? 0,
        };
        if (is_detail) {
          if (getchat["last_message"] is Map) {
            var last_message = await jsonMessage(
              getchat["last_message"],
              from_data: json,
              chat_data: json,
              clientId: clientId,
              isUseCache: isUseCache,
              durationCacheExpire: durationCacheExpire,
              is_skip_reply_message: true,
            );
            if (last_message["ok"] == true) {
              json["last_message"] = last_message["result"];
            }
          }
        }
        return {"ok": true, "result": json};
      } else if (type_chat == "private") {
        var get_user = await invoke(
          "getUser",
          parameters: {
            "user_id": chat_id,
          },
          isUseCache: isUseCache,
          durationCacheExpire: durationCacheExpire,
          clientId: clientId,
          extra: extra,
        );
        if (RegExp("^user\$", caseSensitive: false)
            .hashData(get_user["@type"])) {
          var json = {};
          json["id"] = get_user["id"];
          try {
            if (RegExp("^userTypeBot\$", caseSensitive: false)
                .hashData(get_user["type"]["@type"])) {
              json["is_bot"] = true;
            } else {
              json["is_bot"] = false;
            }
          } catch (e) {
            json["is_bot"] = false;
          }
          json["first_name"] = get_user["first_name"];
          if (TgUtils.getBoolean(get_user["last_name"])) {
            json["last_name"] = get_user["last_name"];
          }
          if (TgUtils.getBoolean(get_user["username"])) {
            json["username"] = get_user["username"];
          }

          if (get_user["usernames"] is Map) {
            Map get_user_usernames = (get_user["usernames"] as Map);
            json["usernames"] = get_user["usernames"];
            if (get_user_usernames["active_usernames"] is List) {
              if ((get_user_usernames["active_usernames"] as List)
                  .isNotEmpty) {
                json["username"] =
                    (get_user_usernames["active_usernames"] as List).first;
              }
            }
          }

          if (TgUtils.getBoolean(get_user["phone_number"])) {
            json["phone_number"] = get_user["phone_number"];
          }
          if (TgUtils.getBoolean(get_user["language_code"])) {
            json["language_code"] = get_user["language_code"];
          }
          json["type"] = 'private';
          json["detail"] = {
            "has_protected_content":
                getchat["has_protected_content"] ?? false,
            "is_marked_as_unread": getchat["is_marked_as_unread"] ?? false,
            "is_blocked": getchat["is_blocked"] ?? false,
            "has_scheduled_messages":
                getchat["has_scheduled_messages"] ?? false,
            "can_be_deleted_only_for_self":
                getchat["can_be_deleted_only_for_self"] ?? false,
            "can_be_deleted_for_all_users":
                getchat["can_be_deleted_for_all_users"] ?? false,
            "can_be_reported": getchat["can_be_reported"] ?? false,
            "default_disable_notification":
                getchat["default_disable_notification"] ?? false,
            "unread_count": getchat["unread_count"] ?? 0,
            "last_read_inbox_message_id":
                getchat["last_read_inbox_message_id"] ?? 0,
            "last_read_outbox_message_id":
                getchat["last_read_outbox_message_id"] ?? 0,
            "unread_mention_count": getchat["unread_mention_count"] ?? 0,
            "is_contact": get_user["is_contact"],
            "is_mutual_contact": get_user["is_mutual_contact"],
            "is_verified": get_user["is_verified"],
            "is_support": get_user["is_support"],
            "is_scam": get_user["is_scam"],
            "is_fake": get_user["is_fake"],
            "have_acces": get_user["have_access"]
          };
          if (is_detail) {
            if (getchat["last_message"] is Map) {
              try {
                var last_message = await jsonMessage(
                  getchat["last_message"],
                  from_data: json["from"],
                  chat_data: json["chat"],
                  clientId: clientId,
                  isUseCache: isUseCache,
                  durationCacheExpire: durationCacheExpire,
                  is_skip_reply_message: true,
                );
                if (last_message["ok"] == true) {
                  json.addAll({"last_message": last_message["result"]});
                }
              } catch (e) {}
            }
          }
          if (is_more_detail) {
            try {
              var getUserFullInfo = {};
              try {
                getUserFullInfo = await invoke(
                  "getUserFullInfo",
                  parameters: {
                    "user_id": chat_id,
                  },
                  isUseCache: isUseCache,
                  durationCacheExpire: durationCacheExpire,
                  clientId: clientId,
                  extra: extra,
                );
              } catch (e) {}
              if (getUserFullInfo["photo"] is Map) {
                json["profile_photo"] = {
                  "id": getUserFullInfo["photo"]["id"],
                  "path": "",
                  "file_id": ""
                };
                if (getUserFullInfo["photo"]["@type"] == "chatPhoto") {
                  if (getUserFullInfo["photo"]["sizes"] is List) {
                    try {
                      var getPhoto = getUserFullInfo["photo"]["sizes"]
                          [getUserFullInfo["photo"]["sizes"].length - 1];
                      var getPathPhoto =
                          getPhoto["photo"]["local"]["path"] as String;
                      json["profile_photo"]["path"] = getPathPhoto;
                      (json["profile_photo"] as Map)
                          .addAll(getPhoto["photo"]["local"]);
                      json["profile_photo"]["id"] =
                          getPhoto["photo"]["local"]["id"];
                      json["profile_photo"]["file_id"] =
                          getPhoto["photo"]["remote"]["id"];
                      json["profile_photo"]["file_unique_id"] =
                          getPhoto["photo"]["remote"]["unique_id"];
                    } catch (e) {}
                  }
                }
              }
              if (getUserFullInfo["description"] is String) {
                json["description"] = getUserFullInfo["description"];
              }
              try {
                getUserFullInfo.forEach((key, value) {
                  if (key != "@type") {
                    try {
                      if (value is String) {
                        json["detail"][key.toString()] = value;
                      }
                      if (value is num) {
                        json["detail"][key.toString()] = value;
                      }
                      if (value is bool) {
                        json["detail"][key.toString()] = value;
                      }
                    } catch (e) {}
                  }
                });
              } catch (e) {}

              if (json["profile_photo"] is Map &&
                  RegExp(r"^([0-9]+)$", caseSensitive: false)
                      .hashData(json["profile_photo"]["id"])) {
                try {
                  json["profile_photo"]["id"] =
                      (int.parse(json["profile_photo"]["id"]));
                } catch (e) {}
              }
            } catch (e) {
              return {"ok": true, "result": json, "error": e};
            }
          }
          return {"ok": true, "result": json};
        }
      }
    }
  } catch (e) {
    if (RegExp("^[0-9]+\$", caseSensitive: false)
        .hashData(chat_id.toString())) {
      try {
        return await getUser(
          chat_id,
          isUseCache: isUseCache,
          durationCacheExpire: durationCacheExpire,
          clientId: clientId,
        );
      } catch (e) {
        return {
          "ok": true,
          "result": {"id": chat_id, "type": "", "detail": {}}
        };
      }
    }
  }
  return {
    "ok": false,
    "result": {"id": chat_id, "detail": {}}
  };
}