request method

  1. @override
Future<Map> request(
  1. String method, {
  2. Map? 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. TdlibBase libTdJson
    )?,
  10. FutureOr<Map> onGetInvokeData(
    1. String extra,
    2. int client_id,
    3. TdlibBase libTdJson
    )?,
  11. bool? isInvokeThrowOnError,
  12. bool isAutoExtendMessage = false,
  13. bool? isUseCache,
  14. Duration? durationCacheExpire,
})
override

call api more pretty use https://core.telegram.org/bots/api#available-methods not all methods are here example:

await tg.request(
  "sendMessage",
  parameters: {
    "chat_id": 12345,
    "text": "send saskoasok"
  },
  clientId: tg.client_id,
);

//

Implementation

@override
Future<Map> request(
  String method, {
  Map? parameters,
  required int clientId,
  bool isVoid = false,
  Duration? delayDuration,
  Duration? invokeTimeOut,
  String? extra,
  bool? isAutoGetChat,
  FutureOr<String> Function(int client_id, TdlibBase libTdJson)?
      onGenerateExtraInvoke,
  FutureOr<Map> Function(String extra, int client_id, TdlibBase libTdJson)?
      onGetInvokeData,
  bool? isInvokeThrowOnError,
  bool isAutoExtendMessage = false,
  bool? isUseCache,
  Duration? durationCacheExpire,
}) async {
  //
  parameters ??= {};

  if (isAutoExtendMessage) {
    if (parameters["text"] is String) {
      String text = parameters["text"];
      if (text.length >= 4096) {
        List<String> messagesJson = TgUtils.splitByLength(text, 4096);
        List result = [];
        for (var i = 0; i < messagesJson.length; i++) {
          var loopData = messagesJson[i];
          try {
            await Future.delayed(Duration(milliseconds: 500));
            parameters["text"] = loopData;
            if (RegExp("(editMessageText)", caseSensitive: false)
                .hasMatch(method)) {
              if (i != 0) {
                method = "sendMessage";
              }
            }
            var res = await requestInvoke(
              method,
              isUseCache: isUseCache,
              durationCacheExpire: durationCacheExpire,
              parameters: parameters,
              clientId: clientId,
              isVoid: isVoid,
              extra: extra,
              isAutoGetChat: isAutoGetChat,
              delayDuration: delayDuration,
              invokeTimeOut: invokeTimeOut,
              isInvokeThrowOnError: isInvokeThrowOnError ?? false,
              onGenerateExtraInvoke: onGenerateExtraInvoke,
              onGetInvokeData: onGetInvokeData,
            );
            result.add(res);
          } catch (e) {
            result.add(e);
          }
        }
        return {"result": result};
      }
    }

    if (parameters["caption"] is String) {
      String caption = parameters["caption"];
      if (caption.length >= 1024) {
        List<String> messagesJson = TgUtils.splitByLength(caption, 1024);
        List result = [];
        for (var i = 0; i < messagesJson.length; i++) {
          var loopData = messagesJson[i];
          try {
            await Future.delayed(Duration(milliseconds: 500));
            parameters["caption"] = loopData;
            if (RegExp("(editMessageCaption)", caseSensitive: false)
                .hasMatch(method)) {
              if (i != 0) {
                parameters["text"] = loopData;
                method = "sendMessage";
              }
            }
            var res = await requestInvoke(
              method,
              isUseCache: isUseCache,
              durationCacheExpire: durationCacheExpire,
              parameters: parameters,
              clientId: clientId,
              isVoid: isVoid,
              extra: extra,
              isAutoGetChat: isAutoGetChat,
              delayDuration: delayDuration,
              invokeTimeOut: invokeTimeOut,
              isInvokeThrowOnError: isInvokeThrowOnError ?? false,
              onGenerateExtraInvoke: onGenerateExtraInvoke,
              onGetInvokeData: onGetInvokeData,
            );
            result.add(res);
          } catch (e) {
            result.add(e);
          }
        }
        return {"result": result};
      }
    }
  }

  return await requestInvoke(
    method,
    isUseCache: isUseCache,
    durationCacheExpire: durationCacheExpire,
    parameters: parameters,
    clientId: clientId,
    isVoid: isVoid,
    extra: extra,
    isAutoGetChat: isAutoGetChat,
    delayDuration: delayDuration,
    invokeTimeOut: invokeTimeOut,
    isInvokeThrowOnError: isInvokeThrowOnError ?? false,
    onGenerateExtraInvoke: onGenerateExtraInvoke,
    onGetInvokeData: onGetInvokeData,
  );
}