invoke method

Future<Map> invoke(
  1. String method, {
  2. Map<String, dynamic>? parameters,
  3. required int clientId,
  4. bool isVoid = false,
  5. Duration? delayDuration,
  6. Duration? invokeTimeOut,
  7. String? extra,
  8. bool? iSAutoGetChat,
  9. FutureOr<String> onGenerateExtraInvoke(
    1. int client_id,
    2. Mtproto Mtproto
    )?,
  10. FutureOr<Map> onGetInvokeData(
    1. String extra,
    2. int client_id,
    3. Mtproto Mtproto
    )?,
  11. bool isThrowOnError = true,
})

call api latest Tdlib-Methods example:

tg.invoke(
 "getChat",
 parameters: {
   "chat_id": 0,
 },
 clientId: tg.client_id,
);

Implementation

Future<Map> invoke(
  String method, {
  Map<String, dynamic>? parameters,
  required int clientId,
  bool isVoid = false,
  Duration? delayDuration,
  Duration? invokeTimeOut,
  String? extra,
  bool? iSAutoGetChat,
  FutureOr<String> Function(int client_id, Mtproto Mtproto)?
      onGenerateExtraInvoke,
  FutureOr<Map> Function(String extra, int client_id, Mtproto Mtproto)?
      onGetInvokeData,
  bool isThrowOnError = true,
}) async {
  onGetInvokeData ??= on_get_invoke_data;
  onGenerateExtraInvoke ??= on_generate_extra_invoke;
  iSAutoGetChat ??= is_auto_get_chat;

  invokeTimeOut ??= invoke_time_out;
  parameters ??= {};
  if (clientId == 0) {
    clientId = client_id;
  }

  String extra_id = "";

  bool is_set_extra_from_function = false;
  if (parameters["@extra"] is String == false) {
    if (extra != null) {
      extra_id = extra;
    } else if (onGenerateExtraInvoke != null) {
      extra_id = (await onGenerateExtraInvoke(clientId, this));
      is_set_extra_from_function = true;
    } else {
      extra_id = generateUuid(15);
    }
    parameters["@extra"] = extra_id;
  } else {
    extra_id = parameters["@extra"];
  }

  if (extra_id.isEmpty) {
    if (is_set_extra_from_function == false) {
      if (onGenerateExtraInvoke != null) {
        extra_id = (await onGenerateExtraInvoke(clientId, this));
      }
    }
  }
  if (extra_id.isEmpty) {
    parameters["@extra"] = generateUuid(15);
  }

  if (iSAutoGetChat &&
      RegExp(r"^(sendMessage|getChatMember)$", caseSensitive: false)
          .hashData(method)) {
    if (parameters["chat_id"] is int) {
      client_send(
        clientId,
        {
          "@type": "getChat",
          "chat_id": parameters["chat_id"],
        },
      );
    }
    if (parameters["user_id"] is int) {
      client_send(
        clientId,
        {
          "@type": "getUser",
          "user_id": parameters["user_id"],
        },
      );
    }
  }

  Map requestMethod = {
    "@type": method,
    "client_id": clientId,
    ...parameters,
  };

  if (isVoid) {
    client_send(
      clientId,
      requestMethod,
    );
    return {
      "@type": "ok",
      "@extra": extra,
    };
  }
  Map result = {};
  Duration timeOut = invokeTimeOut;
  DateTime.now().add(timeOut);
  if (onGetInvokeData != null) {
    client_send(
      clientId,
      requestMethod,
    );
    return await onGetInvokeData(extra_id, clientId, this);
  }
  on(event_invoke, (UpdateMt update) async {
    try {
      if (update.client_id == clientId) {
        Map updateOrigin = update.raw;
        if (updateOrigin["@extra"] == extra_id) {
          updateOrigin.remove("@extra");
          result = updateOrigin;
        }
      }
    } catch (e) {
      result["@type"] = "error";
    }
  });
  client_send(
    clientId,
    requestMethod,
  );
  throw result;
}