request method
Future<Map>
request(
- String method, {
- Map? parameters,
- required int clientId,
- bool isVoid = false,
- Duration? delayDuration,
- Duration? invokeTimeOut,
- String? extra,
- bool? isAutoGetChat,
- FutureOr<
String> onGenerateExtraInvoke()?, - FutureOr<
Map> onGetInvokeData()?, - bool? isInvokeThrowOnError,
- bool isAutoExtendMessage = false,
- bool? isUseCache,
- 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,
);
}