diff --git a/package/telegram_client/example/example.dart b/package/telegram_client/example/example.dart index 25c7921a..ece2a088 100644 --- a/package/telegram_client/example/example.dart +++ b/package/telegram_client/example/example.dart @@ -54,8 +54,7 @@ void main(List args) async { .listSync() .where((e) { print(basenameWithoutExtension(e.path)); - if (RegExp(r"^(client_)", caseSensitive: false) - .hasMatch(basenameWithoutExtension(e.path))) { + if (RegExp(r"^(client_)", caseSensitive: false).hasMatch(basenameWithoutExtension(e.path))) { return true; } return false; @@ -74,8 +73,7 @@ void main(List args) async { }, ); if (basenameWithoutExtension(result.path) != "create_new") { - return basenameWithoutExtension(result.path) - .replaceAll(RegExp(r"^(client_)", caseSensitive: false), ""); + return basenameWithoutExtension(result.path).replaceAll(RegExp(r"^(client_)", caseSensitive: false), ""); } } String res = logger.prompt("Name :"); @@ -88,8 +86,7 @@ void main(List args) async { .toLowerCase() .replaceAll(RegExp(r"([ \+]+)", caseSensitive: false), ""); - Directory database_directory = - Directory(join(directory_tg.path, "client_${name}")); + Directory database_directory = Directory(join(directory_tg.path, "client_${name}")); TelegramClient tg = TelegramClient(); @@ -112,8 +109,7 @@ void main(List args) async { if (update["@type"] == "updateAuthorizationState") { if (update["authorization_state"] is Map) { Map authorization_state = update["authorization_state"]; - if (authorization_state["@type"] == - "authorizationStateWaitPhoneNumber") { + if (authorization_state["@type"] == "authorizationStateWaitPhoneNumber") { String phone_number_or_token_bot = () { String res = logger.prompt("Phone Number / Token Bot:"); while (true) { @@ -124,8 +120,7 @@ void main(List args) async { }() .replaceAll(RegExp(r"([ \+]+)", caseSensitive: false), ""); - if (RegExp(r"^([0-9]+:AA[a-z0-9_-]+)$", caseSensitive: false) - .hashData(phone_number_or_token_bot)) { + if (RegExp(r"^([0-9]+:AA[a-z0-9_-]+)$", caseSensitive: false).hashData(phone_number_or_token_bot)) { Map res = await tg.invoke( parameters: { "@type": "checkAuthenticationBotToken", @@ -202,9 +197,7 @@ void main(List args) async { }(); // ignore: unused_local_variable - Map parameters_request = { - "@type": (is_outgoing) ? "editMessageText" : "sendMessage" - }; + Map parameters_request = {"@type": (is_outgoing) ? "editMessageText" : "sendMessage"}; RegExp regExpCommand = RegExp(r"^(/|\.|!)", caseSensitive: false); if (regExpCommand.hashData(text_command)) { @@ -212,11 +205,7 @@ void main(List args) async { if (RegExp(r"^(ping)$", caseSensitive: false).hasMatch(command)) { return await tg.request( - parameters: { - "@type": "sendMessage", - "chat_id": chat_id, - "text": "PONG" - }, + parameters: {"@type": "sendMessage", "chat_id": chat_id, "text": "PONG"}, telegramClientData: updateTelegramClient.telegramClientData, ); } diff --git a/package/telegram_client/examples/tdlib.dart b/package/telegram_client/examples/tdlib.dart index b56f0cc1..1d8c3e71 100644 --- a/package/telegram_client/examples/tdlib.dart +++ b/package/telegram_client/examples/tdlib.dart @@ -17,24 +17,19 @@ void main(List args) async { clientOption: TelegramClientLibraryTdlibOptionParameter.create( // api_id: 0, // api_hash: "", - database_directory: - Directory(path.join(Directory.current.uri.toFilePath(), "temp", "db")) - .path, - files_directory: Directory( - path.join(Directory.current.uri.toFilePath(), "temp", "file")) - .path, + database_directory: Directory(path.join(Directory.current.uri.toFilePath(), "temp", "db")).path, + files_directory: Directory(path.join(Directory.current.uri.toFilePath(), "temp", "file")).path, ), ); await tdlib.ensureInitialized(); - tdlib.on(tdlib.event_update, (UpdateTd updateTd) async { + tdlib.on(tdlib.event_update, (UpdateTelegramClientTdlib updateTd) async { Map update = updateTd.update; // update.printPretty(); if (update["@type"] == "updateAuthorizationState") { if (update["authorization_state"] is Map) { Map authorizationState = update["authorization_state"]; - if (authorizationState["@type"] == - "authorizationStateWaitPhoneNumber") { + if (authorizationState["@type"] == "authorizationStateWaitPhoneNumber") { Map res = await tdlib.invoke( "setAuthenticationPhoneNumber", parameters: { @@ -73,8 +68,7 @@ void main(List args) async { for (var i = 1; i <= 1000; i++) { print(i); await Future.delayed(Duration(microseconds: 10)); - Directory directory = - Directory(path.join(Directory.current.path, "temp", "lp_${i}")); + Directory directory = Directory(path.join(Directory.current.path, "temp", "lp_${i}")); Map res = await tdlib.createclient( clientId: tdlib.td_create_client_id(), clientOption: TelegramClientLibraryTdlibOptionParameter.create( @@ -84,8 +78,7 @@ void main(List args) async { ); res.printPretty(); } - print(DateTime.now() - .extension_general_lib_countAgoFromDateTime(dateTime: dateTime)); + print(DateTime.now().extension_general_lib_countAgoFromDateTime(dateTime: dateTime)); stdin.listen((e) async { try { DateTime dateTime = DateTime.now(); @@ -106,8 +99,7 @@ void main(List args) async { } else { return; } - print(DateTime.now() - .extension_general_lib_countAgoFromDateTime(dateTime: dateTime)); + print(DateTime.now().extension_general_lib_countAgoFromDateTime(dateTime: dateTime)); } catch (e) { print(e); } diff --git a/package/telegram_client/lib/tdlib/tdlib_core.dart b/package/telegram_client/lib/tdlib/tdlib_core.dart index 833cbfa1..008f9a30 100644 --- a/package/telegram_client/lib/tdlib/tdlib_core.dart +++ b/package/telegram_client/lib/tdlib/tdlib_core.dart @@ -62,7 +62,7 @@ import 'update_td.dart'; /// "database_key": "", /// }, /// ); -/// tg.on(tg.event_update, (UpdateTd update) async { +/// tg.on(tg.event_update, (UpdateTelegramClientTdlib update) async { /// print(update.raw); /// }); /// tg.ensureInitializedIsolate(); @@ -148,8 +148,8 @@ class Tdlib extends TdlibNative { Tdlib({ super.pathTdl, super.clientOption, - super.task_max_count, - super.task_min_cooldown, + super.taskMaxCount, + super.taskMinCooldown, super.invokeTimeOut, super.event_invoke = "invoke", super.event_update = "update", @@ -158,9 +158,9 @@ class Tdlib extends TdlibNative { super.eventEmitter, super.delayInvoke, super.isAutoGetChat = false, - super.on_get_invoke_data, - super.on_receive_update, - super.on_generate_extra_invoke, + super.onGenerateExtraInvoke, + super.onGetInvokeData, + super.onReceiveUpdate, super.isInvokeThrowOnError, }); FutureOr closeClients() async { @@ -181,7 +181,7 @@ class Tdlib extends TdlibNative { /// set up authorizationStateWaitTdlibParameters new client without more code Future initClient( - UpdateTd update, { + UpdateTelegramClientTdlib update, { Map? tdlibParameters, required int clientId, bool isVoid = false, @@ -950,7 +950,7 @@ class Tdlib extends TdlibNative { Completer completer = Completer(); - var listen = on(event_update, (UpdateTd update) async { + var listen = on(event_update, (UpdateTelegramClientTdlib update) async { if (update.client_id != clientId) { return; } @@ -995,7 +995,7 @@ class Tdlib extends TdlibNative { }, ); - event_emitter.off(listener: listen); + eventEmitter.off(listener: listen); if (result["@type"] is String) { // task_decrease(); if (result["@type"] == "error") { diff --git a/package/telegram_client/lib/tdlib/tdlib_library/base.dart b/package/telegram_client/lib/tdlib/tdlib_library/base.dart index 30b6d685..8905e60d 100644 --- a/package/telegram_client/lib/tdlib/tdlib_library/base.dart +++ b/package/telegram_client/lib/tdlib/tdlib_library/base.dart @@ -48,8 +48,12 @@ import 'package:telegram_client/tdlib/update_td.dart'; import 'package:universal_io/io.dart'; +typedef TdlibOnReceiveUpdate = FutureOr Function(dynamic update, TdlibBase libTdJson); +typedef TdlibOnGenerateExtraInvoke = FutureOr Function(int client_id, TdlibBase libTdJson); +typedef TdlibOnGetInvokeData = FutureOr Function(String extra, int client_id, TdlibBase libTdJson); + abstract class TdlibBaseCore { - Future is_td_initialized() async{ + Future is_td_initialized() async { return false; } @@ -110,7 +114,7 @@ abstract class TdlibBaseCore { /// "api_hash": "saskaspasad" /// }, /// ); -/// tg.on("update", (UpdateTd update) async { +/// tg.on("update", (UpdateTelegramClientTdlib update) async { /// print(update.raw); /// }); /// tg.initIsolate(); @@ -151,23 +155,23 @@ abstract class TdlibBase implements TdlibBaseCore { int task_count = 0; final String event_invoke; final String event_update; - late final EventEmitter event_emitter; + final EventEmitter eventEmitter; Duration? delay_update; Duration delay_invoke = Duration(milliseconds: 1); bool is_auto_get_chat = false; Duration invoke_time_out = Duration(minutes: 10); double timeOutUpdate; bool is_invoke_throw_on_error = false; - FutureOr Function(dynamic update, TdlibBase libTdJson)? on_receive_update; - FutureOr Function(int client_id, TdlibBase libTdJson)? on_generate_extra_invoke; - FutureOr Function(String extra, int client_id, TdlibBase libTdJson)? on_get_invoke_data; - int task_max_count; - int task_min_cooldown; + TdlibOnReceiveUpdate? onReceiveUpdate; + TdlibOnGenerateExtraInvoke? onGenerateExtraInvoke; + TdlibOnGetInvokeData? onGetInvokeData; + int taskMaxCount; + int taskMinCooldown; TdlibBase({ String? pathTdl, TelegramClientLibraryTdlibOptionParameter? clientOption, - this.task_max_count = 10000, - this.task_min_cooldown = 10, + this.taskMaxCount = 10000, + this.taskMinCooldown = 10, this.event_invoke = "invoke", this.event_update = "update", EventEmitter? eventEmitter, @@ -177,10 +181,10 @@ abstract class TdlibBase implements TdlibBaseCore { Duration? invokeTimeOut, bool isAutoGetChat = false, bool isInvokeThrowOnError = true, - this.on_generate_extra_invoke, - this.on_get_invoke_data, - this.on_receive_update, - }) { + this.onGenerateExtraInvoke, + this.onGetInvokeData, + this.onReceiveUpdate, + }) : eventEmitter = eventEmitter ?? EventEmitter() { if (delayInvoke != null) { delay_invoke = delayInvoke; } @@ -193,11 +197,6 @@ abstract class TdlibBase implements TdlibBaseCore { is_auto_get_chat = isAutoGetChat; invokeTimeOut ??= Duration(minutes: 5); invoke_time_out = invokeTimeOut; - if (eventEmitter != null) { - event_emitter = eventEmitter; - } else { - event_emitter = EventEmitter(); - } if (clientOption != null) { client_option.rawData.addAll(clientOption.rawData); } @@ -208,18 +207,19 @@ abstract class TdlibBase implements TdlibBaseCore { // is_init_send_port = true; return; } - if (on_receive_update != null) { - await on_receive_update!(update, this); + final onReceiveUpdate = this.onReceiveUpdate; + if (onReceiveUpdate != null) { + await onReceiveUpdate(update, this); } else if (update is TdlibIsolateReceiveData) { TdlibIsolateReceiveData tdlibIsolateReceiveData = update; try { if (tdlibIsolateReceiveData.updateData["@extra"] is String) { - event_emitter.emit(eventName: event_invoke, value: tdlibIsolateReceiveData); + this.eventEmitter.emit(eventName: event_invoke, value: tdlibIsolateReceiveData); } else { - event_emitter.emit(eventName: event_update, value: tdlibIsolateReceiveData); + this.eventEmitter.emit(eventName: event_update, value: tdlibIsolateReceiveData); } } catch (e) { - event_emitter.emit(eventName: event_update, value: tdlibIsolateReceiveData); + this.eventEmitter.emit(eventName: event_update, value: tdlibIsolateReceiveData); } } else if (update is TdlibIsolateReceiveDataError) { is_init_isolate = false; @@ -453,18 +453,18 @@ abstract class TdlibBase implements TdlibBaseCore { /// receive all update data EventEmitterListener on( String type_update, - FutureOr Function(UpdateTd update) callback, { + FutureOr Function(UpdateTelegramClientTdlib update) callback, { final Map? stateData, void Function(Object data)? onError, }) { - return event_emitter.on( + return eventEmitter.on( eventName: type_update, stateData: stateData ?? {}, onCallback: (listener, update) async { try { if (update is TdlibIsolateReceiveData) { // final TdlibIsolateReceiveData tdlibIsolateReceiveData = update; - await callback(UpdateTd( + await callback(UpdateTelegramClientTdlib( update: update.updateData, client_id: update.clientId, client_option: () { @@ -520,10 +520,10 @@ abstract class TdlibBase implements TdlibBaseCore { final Map result = await Future(() async { try { if (isVoid == false) { - if (task_count >= task_max_count) { + if (task_count >= taskMaxCount) { while (true) { await Future.delayed(Duration(microseconds: 1)); - if (task_count < task_min_cooldown) { + if (task_count < taskMinCooldown) { break; } } @@ -578,7 +578,7 @@ abstract class TdlibBase implements TdlibBaseCore { ); return await onGetInvokeData(extra_id, clientId, this); } - listener = on(event_invoke, (UpdateTd update) async { + listener = on(event_invoke, (UpdateTelegramClientTdlib update) async { try { if (update.client_id == clientId) { final Map updateOrigin = update.raw; @@ -628,7 +628,7 @@ abstract class TdlibBase implements TdlibBaseCore { completer.complete({}); } catch (e) {} try { - event_emitter.off(listener: listener); + eventEmitter.off(listener: listener); } catch (e) {} if (result.isEmpty || result["@type"] is String == false || result["@type"] == "error") { @@ -664,16 +664,16 @@ abstract class TdlibBase implements TdlibBaseCore { String? extra, bool? isAutoGetChat, bool? isInvokeThrowOnError, - FutureOr Function(int client_id, TdlibBase libTdJson)? onGenerateExtraInvoke, - FutureOr Function(String extra, int client_id, TdlibBase libTdJson)? onGetInvokeData, + TdlibOnGenerateExtraInvoke? onGenerateExtraInvoke, + TdlibOnGetInvokeData? onGetInvokeData, }) async { isUseCache ??= false; durationCacheExpire ??= Duration( minutes: 1, ); isInvokeThrowOnError ??= is_invoke_throw_on_error; - onGetInvokeData ??= on_get_invoke_data; - onGenerateExtraInvoke ??= on_generate_extra_invoke; + onGetInvokeData ??= this.onGetInvokeData; + onGenerateExtraInvoke ??= this.onGenerateExtraInvoke; isAutoGetChat ??= is_auto_get_chat; // invokeTimeOut ??= invoke_time_out; diff --git a/package/telegram_client/lib/tdlib/tdlib_library/io.dart b/package/telegram_client/lib/tdlib/tdlib_library/io.dart index e9ae4fc5..81dd7ba3 100644 --- a/package/telegram_client/lib/tdlib/tdlib_library/io.dart +++ b/package/telegram_client/lib/tdlib/tdlib_library/io.dart @@ -77,7 +77,7 @@ typedef TdExecuteNative = TdCharNative Function(TdCharNative parameters); /// "api_hash": "saskaspasad" /// }, /// ); -/// tg.on("update", (UpdateTd update) async { +/// tg.on("update", (UpdateTelegramClientTdlib update) async { /// print(update.raw); /// }); /// tg.initIsolate(); @@ -94,12 +94,12 @@ class TdlibNative extends TdlibBase { super.invokeTimeOut, super.isAutoGetChat, super.isInvokeThrowOnError, - super.on_generate_extra_invoke, - super.on_get_invoke_data, - super.on_receive_update, + super.onGenerateExtraInvoke, + super.onGetInvokeData, + super.onReceiveUpdate, super.pathTdl, - super.task_max_count, - super.task_min_cooldown, + super.taskMaxCount, + super.taskMinCooldown, super.timeOutUpdate, }) { opentdLib(pathTdlib: path_tdlib); @@ -189,7 +189,7 @@ class TdlibNative extends TdlibBase { } @override - Future is_td_initialized() async{ + Future is_td_initialized() async { return true; } } diff --git a/package/telegram_client/lib/tdlib/tdlib_library/none.dart b/package/telegram_client/lib/tdlib/tdlib_library/none.dart index 5a5f2dbf..9f9dd157 100644 --- a/package/telegram_client/lib/tdlib/tdlib_library/none.dart +++ b/package/telegram_client/lib/tdlib/tdlib_library/none.dart @@ -49,7 +49,7 @@ import 'package:telegram_client/tdlib/tdlib_library/base.dart'; /// "api_hash": "saskaspasad" /// }, /// ); -/// tg.on("update", (UpdateTd update) async { +/// tg.on("update", (UpdateTelegramClientTdlib update) async { /// print(update.raw); /// }); /// tg.initIsolate(); @@ -66,12 +66,12 @@ class TdlibNative extends TdlibBase { super.invokeTimeOut, super.isAutoGetChat, super.isInvokeThrowOnError, - super.on_generate_extra_invoke, - super.on_get_invoke_data, - super.on_receive_update, + super.onGenerateExtraInvoke, + super.onGetInvokeData, + super.onReceiveUpdate, super.pathTdl, - super.task_max_count, - super.task_min_cooldown, + super.taskMaxCount, + super.taskMinCooldown, super.timeOutUpdate, }); @override @@ -96,7 +96,7 @@ class TdlibNative extends TdlibBase { } @override - Future is_td_initialized() async{ + Future is_td_initialized() async { return true; } } diff --git a/package/telegram_client/lib/telegram_client/telegram_client_tdlib_option.dart b/package/telegram_client/lib/tdlib/tdlib_library/option.dart similarity index 70% rename from package/telegram_client/lib/telegram_client/telegram_client_tdlib_option.dart rename to package/telegram_client/lib/tdlib/tdlib_library/option.dart index e58b403f..a61c661c 100644 --- a/package/telegram_client/lib/telegram_client/telegram_client_tdlib_option.dart +++ b/package/telegram_client/lib/tdlib/tdlib_library/option.dart @@ -32,15 +32,12 @@ Bukan maksud kami menipu itu karena harga yang sudah di kalkulasi + bantuan tiba */ // ignore_for_file: non_constant_identifier_names, camel_case_extensions -import 'dart:async'; - import 'package:general_lib/event_emitter/event_emitter.dart'; import 'package:telegram_client/scheme/telegram_client_library_tdlib_option_parameter.dart'; -import 'package:telegram_client/tdlib/tdlib_library/base.dart'; +import 'base.dart'; class TelegramClientTdlibOption { final TelegramClientLibraryTdlibOptionParameter? clientOption; - final bool is_cli; final Duration? invokeTimeOut; final Duration? delayUpdate; @@ -48,28 +45,44 @@ class TelegramClientTdlibOption { final EventEmitter? eventEmitter; final bool isAutoGetChat; - final FutureOr> Function(String, int, TdlibBase td)? - on_get_invoke_data; - final FutureOr Function(dynamic, TdlibBase)? on_receive_update; - final FutureOr Function(int, TdlibBase)? on_generate_extra_invoke; + final TdlibOnGetInvokeData? onGetInvokeData; + final TdlibOnReceiveUpdate? onReceiveUpdate; + final TdlibOnGenerateExtraInvoke? onGenerateExtraInvoke; final bool isInvokeThrowOnError; final Duration? delayInvoke; - final int task_max_count; - final int task_min_cooldown; + final int taskMaxCount; + final int taskMinCooldown; const TelegramClientTdlibOption({ this.isAutoGetChat = false, - this.task_max_count = 10000, - this.task_min_cooldown = 10, + this.taskMaxCount = 10000, + this.taskMinCooldown = 10, this.clientOption, - this.is_cli = false, this.invokeTimeOut, this.timeOutUpdate = 1.0, this.delayInvoke, this.delayUpdate, - this.on_generate_extra_invoke, - this.on_get_invoke_data, + this.onGenerateExtraInvoke, + this.onGetInvokeData, this.eventEmitter, - this.on_receive_update, + this.onReceiveUpdate, this.isInvokeThrowOnError = true, }); + + TelegramClientTdlibOption copyWith({ + TelegramClientLibraryTdlibOptionParameter? clientOption, + Duration? invokeTimeOut, + Duration? delayUpdate, + double? timeOutUpdate, + EventEmitter? eventEmitter, + bool? isAutoGetChat, + TdlibOnGetInvokeData? onGetInvokeData, + TdlibOnReceiveUpdate? onReceiveUpdate, + TdlibOnGenerateExtraInvoke? onGenerateExtraInvoke, + bool? isInvokeThrowOnError, + Duration? delayInvoke, + int? taskMaxCount, + int? taskMinCooldown, + }) { + return TelegramClientTdlibOption(); + } } diff --git a/package/telegram_client/lib/tdlib/tdlib_library/tdlib.dart b/package/telegram_client/lib/tdlib/tdlib_library/tdlib.dart index 09b5693d..53cbedf1 100644 --- a/package/telegram_client/lib/tdlib/tdlib_library/tdlib.dart +++ b/package/telegram_client/lib/tdlib/tdlib_library/tdlib.dart @@ -32,6 +32,7 @@ Bukan maksud kami menipu itu karena harga yang sudah di kalkulasi + bantuan tiba */ export "base.dart"; +export "option.dart"; export "none.dart" if (dart.library.io) 'io.dart' if (dart.library.js_interop) 'web.dart'; diff --git a/package/telegram_client/lib/tdlib/tdlib_library/web.dart b/package/telegram_client/lib/tdlib/tdlib_library/web.dart index 435b7ee8..f59ec58b 100644 --- a/package/telegram_client/lib/tdlib/tdlib_library/web.dart +++ b/package/telegram_client/lib/tdlib/tdlib_library/web.dart @@ -40,7 +40,7 @@ Bukan maksud kami menipu itu karena harga yang sudah di kalkulasi + bantuan tiba import 'dart:async'; import 'package:telegram_client/tdlib/tdlib_library/base.dart'; -import 'package:wasm_ffi/ffi.dart'; +import 'package:wasm_ffi/ffi.dart'; typedef TdCharNative = Pointer; typedef TdReceiveNative = TdCharNative Function(Double timout); @@ -62,7 +62,7 @@ typedef TdExecuteNative = TdCharNative Function(TdCharNative parameters); /// "api_hash": "saskaspasad" /// }, /// ); -/// tg.on("update", (UpdateTd update) async { +/// tg.on("update", (UpdateTelegramClientTdlib update) async { /// print(update.raw); /// }); /// tg.initIsolate(); @@ -80,12 +80,12 @@ class TdlibNative extends TdlibBase { super.invokeTimeOut, super.isAutoGetChat, super.isInvokeThrowOnError, - super.on_generate_extra_invoke, - super.on_get_invoke_data, - super.on_receive_update, + super.onGenerateExtraInvoke, + super.onGetInvokeData, + super.onReceiveUpdate, super.pathTdl, - super.task_max_count, - super.task_min_cooldown, + super.taskMaxCount, + super.taskMinCooldown, super.timeOutUpdate, }) { opentdLib(pathTdlib: path_tdlib).then((a) { @@ -142,7 +142,7 @@ Future opentdLib({ if (TdlibNative.is_open_tdlib) { return; } - + TdlibNative.tdLib = await DynamicLibrary.open(pathTdlib); TdlibNative.is_open_tdlib = true; TdlibNative.td_execute_native_function = TdlibNative.tdLib.lookupFunction('td_execute'); diff --git a/package/telegram_client/lib/tdlib/update_td.dart b/package/telegram_client/lib/tdlib/update_td.dart index 76c77e62..6cb74b4f 100644 --- a/package/telegram_client/lib/tdlib/update_td.dart +++ b/package/telegram_client/lib/tdlib/update_td.dart @@ -33,11 +33,11 @@ Bukan maksud kami menipu itu karena harga yang sudah di kalkulasi + bantuan tiba // ignore_for_file: non_constant_identifier_names, camel_case_extensions /// Update td for make update support raw, raw api, raw api light -class UpdateTd { +class UpdateTelegramClientTdlib { final Map update; final int client_id; final Map client_option; - const UpdateTd({ + const UpdateTelegramClientTdlib({ required this.update, required this.client_id, required this.client_option, diff --git a/package/telegram_client/lib/telegram_client/telegram_client_telegram_bot_api_option.dart b/package/telegram_client/lib/telegram_bot_api/option.dart similarity index 76% rename from package/telegram_client/lib/telegram_client/telegram_client_telegram_bot_api_option.dart rename to package/telegram_client/lib/telegram_bot_api/option.dart index cd8cc4a5..f55245b9 100644 --- a/package/telegram_client/lib/telegram_client/telegram_client_telegram_bot_api_option.dart +++ b/package/telegram_client/lib/telegram_bot_api/option.dart @@ -51,4 +51,22 @@ class TelegramClientTelegramBotApiOption { this.telegramUrlWebhook, this.httpClient, }); + + TelegramClientTelegramBotApiOption copyWith({ + String? tokenBot, + Map? clientOption, + ServerUniverseNative? serverUniverseNative, + Crypto? crypto, + Uri? telegramUrlWebhook, + Client? httpClient, + }) { + return TelegramClientTelegramBotApiOption( + tokenBot: tokenBot ?? this.tokenBot, + clientOption: clientOption ?? this.clientOption, + serverUniverseNative: serverUniverseNative ?? this.serverUniverseNative, + crypto: crypto ?? this.crypto, + telegramUrlWebhook: telegramUrlWebhook ?? this.telegramUrlWebhook, + httpClient: httpClient ?? this.httpClient, + ); + } } diff --git a/package/telegram_client/lib/telegram_bot_api/telegram_bot_api.dart b/package/telegram_client/lib/telegram_bot_api/telegram_bot_api.dart index 9f305a8f..cb3b1e73 100644 --- a/package/telegram_client/lib/telegram_bot_api/telegram_bot_api.dart +++ b/package/telegram_client/lib/telegram_bot_api/telegram_bot_api.dart @@ -30,4 +30,5 @@ Bukan maksud kami menipu itu karena harga yang sudah di kalkulasi + bantuan tiba */ +export "option.dart"; export "telegram_bot_api_core.dart"; diff --git a/package/telegram_client/lib/telegram_bot_api/telegram_bot_api_core.dart b/package/telegram_client/lib/telegram_bot_api/telegram_bot_api_core.dart index b5b29c57..c5a0b4cc 100644 --- a/package/telegram_client/lib/telegram_bot_api/telegram_bot_api_core.dart +++ b/package/telegram_client/lib/telegram_bot_api/telegram_bot_api_core.dart @@ -63,10 +63,8 @@ import "package:server_universe/native/native.dart"; class TelegramBotApi { late final String token_bot; ServerUniverseNative? serverUniverseNative; - bool is_init_server = false; - Uri telegram_url_webhook = Uri.parse("http://0.0.0.0:8080/telegram/webhook"); - final Crypto telegram_crypto; - final Client http_client; + final Crypto crypto; + final Client httpClient; final Map client_option = { "api_id": 0, "api_hash": "", @@ -94,10 +92,11 @@ class TelegramBotApi { ], }; - final EventEmitter event_emitter; - final List state_data = []; - final String event_invoke; - final String event_update; + final EventEmitter eventEmitter; + final String eventInvoke; + final String eventUpdate; + + Uri telegramUrlWebhook; /// list methods: /// api: @@ -111,51 +110,53 @@ class TelegramBotApi { this.serverUniverseNative, Crypto? crypto, EventEmitter? eventEmitter, - this.event_invoke = "invoke", - this.event_update = "update", + this.eventInvoke = "invoke", + this.eventUpdate = "update", Uri? telegramUrlWebhook, Client? httpClient, - }) : telegram_crypto = crypto ?? Crypto(key: "aeatmlvodkm9ii37l2p0WGkaAAF3BWCh"), - http_client = httpClient ?? Client(), - event_emitter = eventEmitter ?? EventEmitter() { - if (telegramUrlWebhook != null) { - telegram_url_webhook = telegramUrlWebhook; - } - + }) : crypto = crypto ?? Crypto(key: "aeatmlvodkm9ii37l2p0WGkaAAF3BWCh"), + httpClient = httpClient ?? Client(), + eventEmitter = eventEmitter ?? EventEmitter(), + telegramUrlWebhook = telegramUrlWebhook ?? Uri.parse("http://0.0.0.0:8080/telegram/webhook") { if (clientOption != null) { client_option.addAll(clientOption); } } + bool is_init_server = false; void initServer() { - if (serverUniverseNative != null) { - if (Dart.isWeb == false) { - if (is_init_server == false) { - is_init_server = true; - - serverUniverseNative!.post(telegram_url_webhook.path, (HttpRequest req, HttpResponse res) async { - try { - Map query = (req.uri.queryParameters).clone(); - Map body = await req.bodyAsJsonMap; - event_emitter.emit( - eventName: event_update, - value: UpdateBot( - uri: req.uri, - body: body, - query: query, - type: "glx", - ), - ); - return {"@type": "ok"}; - } catch (e) { - return { - "@type": "ok", - }; - } - }); - } - } + final serverUniverseNative = this.serverUniverseNative; + if (serverUniverseNative == null) { + return; + } + if (Dart.isWeb) { + return; + } + if (is_init_server) { + return; } + is_init_server = true; + + serverUniverseNative.post(telegramUrlWebhook.path, (HttpRequest req, HttpResponse res) async { + try { + Map query = (req.uri.queryParameters).clone(); + Map body = await req.bodyAsJsonMap; + eventEmitter.emit( + eventName: eventUpdate, + value: UpdateTelegramClientTelegramBotApi( + uri: req.uri, + body: body, + query: query, + type: "glx", + ), + ); + return {"@type": "ok"}; + } catch (e) { + return { + "@type": "ok", + }; + } + }); } /// Parse Query Http To TgClientClientData @@ -166,7 +167,7 @@ class TelegramBotApi { if (query["tg"] is String == false) { query["tg"] = ""; } - Map decyprt = convert.json.decode(telegram_crypto.decrypt(data_base64: query["tg"])); + Map decyprt = convert.json.decode(crypto.decrypt(data_base64: query["tg"])); if (decyprt["client_tg_user_id"] == null || decyprt["client_tg_user_id"] == 0) { decyprt["client_tg_user_id"] = TgUtils.parserBotUserIdFromToken(decyprt["client_token"]); @@ -200,7 +201,7 @@ class TelegramBotApi { "expire_date": expire_date, "version": version, }; - final String query_telegram_webhook = telegram_crypto.encryptMapToBase64(data: client_data); + final String query_telegram_webhook = crypto.encryptMapToBase64(data: client_data); final get_webhook_info_old_procces = await request( "getWebhookInfo", @@ -208,7 +209,7 @@ class TelegramBotApi { ); final String url_webhook_old = get_webhook_info_old_procces["result"]["url"]; - final String url_webhook_new = telegram_url_webhook.replace( + final String url_webhook_new = telegramUrlWebhook.replace( path: path, pathSegments: pathSegments, queryParameters: { @@ -269,12 +270,12 @@ class TelegramBotApi { /// }); /// ``` /// add this for handle update api - EventEmitterListener on(String type_update, FutureOr Function(UpdateBot updateBot) callback) { - return event_emitter.on( + EventEmitterListener on(String type_update, FutureOr Function(UpdateTelegramClientTelegramBotApi updateBot) callback) { + return eventEmitter.on( eventName: type_update, onCallback: (listener, update) async { try { - if (update is UpdateBot) { + if (update is UpdateTelegramClientTelegramBotApi) { await callback(update); return; } @@ -290,8 +291,8 @@ class TelegramBotApi { /// tg.emit(tg.event_update, ""); /// ``` /// add this for handle update api - void emit(String type_update, UpdateBot updateBot) { - return event_emitter.emit(eventName: type_update, value: updateBot); + void emit(String type_update, UpdateTelegramClientTelegramBotApi updateBot) { + return eventEmitter.emit(eventName: type_update, value: updateBot); // return event_emitter.emit(type_update, null, updateBot); } @@ -330,9 +331,7 @@ class TelegramBotApi { String? clientType, bool isThrowOnError = true, void Function(int bytesCount, int totalBytes)? onUploadProgress, - Client? httpClient, }) async { - httpClient ??= http_client; parameters ??= {}; clientType ??= client_option["type"]; urlApi ??= client_option["api"]; @@ -666,7 +665,6 @@ class TelegramBotApi { clientType: clientType, onUploadProgress: onUploadProgress, isThrowOnError: isThrowOnError, - httpClient: httpClient, ); result.add(res); } catch (e) { @@ -702,7 +700,6 @@ class TelegramBotApi { clientType: clientType, onUploadProgress: onUploadProgress, isThrowOnError: isThrowOnError, - httpClient: httpClient, ); result.add(res); } catch (e) { @@ -723,7 +720,6 @@ class TelegramBotApi { clientType: clientType, onUploadProgress: onUploadProgress, isThrowOnError: isThrowOnError, - httpClient: httpClient, ); } diff --git a/package/telegram_client/lib/telegram_bot_api/update_bot.dart b/package/telegram_client/lib/telegram_bot_api/update_bot.dart index ec9a33e9..916f5c95 100644 --- a/package/telegram_client/lib/telegram_bot_api/update_bot.dart +++ b/package/telegram_client/lib/telegram_bot_api/update_bot.dart @@ -30,13 +30,13 @@ Bukan maksud kami menipu itu karena harga yang sudah di kalkulasi + bantuan tiba */ -class UpdateBot { +class UpdateTelegramClientTelegramBotApi { final Map body; final Map query; final String type; final Uri uri; - const UpdateBot({ + const UpdateTelegramClientTelegramBotApi({ required this.uri, required this.body, required this.query, diff --git a/package/telegram_client/lib/telegram_client/telegram_client.dart b/package/telegram_client/lib/telegram_client/telegram_client.dart index c813af7a..098f6e28 100644 --- a/package/telegram_client/lib/telegram_client/telegram_client.dart +++ b/package/telegram_client/lib/telegram_client/telegram_client.dart @@ -35,8 +35,8 @@ export "telegram_client_core.dart"; export "telegram_client_type.dart"; export 'package:telegram_client/telegram_client/telegram_client_data.dart'; -export 'package:telegram_client/telegram_client/telegram_client_tdlib_option.dart'; -export 'package:telegram_client/telegram_client/telegram_client_telegram_bot_api_option.dart'; +export 'package:telegram_client/tdlib/tdlib_library/option.dart'; +export 'package:telegram_client/telegram_bot_api/option.dart'; export 'package:telegram_client/telegram_client/update_telegram_client.dart'; diff --git a/package/telegram_client/lib/telegram_client/telegram_client_core.dart b/package/telegram_client/lib/telegram_client/telegram_client_core.dart index 65e583d2..0f2f80ea 100644 --- a/package/telegram_client/lib/telegram_client/telegram_client_core.dart +++ b/package/telegram_client/lib/telegram_client/telegram_client_core.dart @@ -86,8 +86,8 @@ import 'package:telegram_client/telegram_client/function/un_pin_all_chat_message import 'package:telegram_client/telegram_client/function/un_pin_chat_message.dart'; import 'package:telegram_client/telegram_client/function/view_message.dart'; import 'package:telegram_client/telegram_client/function/view_messages.dart'; -import 'package:telegram_client/telegram_client/telegram_client_tdlib_option.dart'; -import 'package:telegram_client/telegram_client/telegram_client_telegram_bot_api_option.dart'; +import 'package:telegram_client/tdlib/tdlib_library/option.dart'; +import 'package:telegram_client/telegram_bot_api/option.dart'; import 'package:telegram_client/telegram_client/update_telegram_client.dart'; import 'package:universal_io/io.dart'; @@ -127,12 +127,11 @@ class TelegramClient { }) { telegramClientTdlibOption ??= TelegramClientTdlibOption( isAutoGetChat: false, - is_cli: false, timeOutUpdate: 1.0, delayInvoke: Duration(milliseconds: 1), delayUpdate: Duration(milliseconds: 1), - task_max_count: 10000, - task_min_cooldown: 10, + taskMaxCount: 10000, + taskMinCooldown: 10, isInvokeThrowOnError: true, ); telegramClientTelegramBotApiOption ??= TelegramClientTelegramBotApiOption( @@ -142,8 +141,8 @@ class TelegramClient { if (is_init_telegram_bot_api) { telegramBotApi = TelegramBotApi( token_bot: telegramClientTelegramBotApiOption.tokenBot, - event_invoke: event_invoke, - event_update: event_update, + eventInvoke: event_invoke, + eventUpdate: event_update, clientOption: telegramClientTelegramBotApiOption.clientOption, eventEmitter: event_emitter, serverUniverseNative: telegramClientTelegramBotApiOption.serverUniverseNative, @@ -164,13 +163,13 @@ class TelegramClient { delayUpdate: telegramClientTdlibOption.delayUpdate, delayInvoke: telegramClientTdlibOption.delayInvoke, isAutoGetChat: telegramClientTdlibOption.isAutoGetChat, - on_generate_extra_invoke: telegramClientTdlibOption.on_generate_extra_invoke, - on_get_invoke_data: telegramClientTdlibOption.on_get_invoke_data, - on_receive_update: telegramClientTdlibOption.on_receive_update, + onGenerateExtraInvoke: telegramClientTdlibOption.onGenerateExtraInvoke, + onGetInvokeData: telegramClientTdlibOption.onGetInvokeData, + onReceiveUpdate: telegramClientTdlibOption.onReceiveUpdate, isInvokeThrowOnError: telegramClientTdlibOption.isInvokeThrowOnError, eventEmitter: event_emitter, - task_max_count: telegramClientTdlibOption.task_max_count, - task_min_cooldown: telegramClientTdlibOption.task_min_cooldown, + taskMaxCount: telegramClientTdlibOption.taskMaxCount, + taskMinCooldown: telegramClientTdlibOption.taskMinCooldown, ); } } @@ -332,7 +331,7 @@ class TelegramClient { eventName: event_name, onCallback: (listener, update) async { try { - if (update is UpdateBot) { + if (update is UpdateTelegramClientTelegramBotApi) { await onUpdate( UpdateTelegramClient( uri: update.uri, @@ -352,7 +351,7 @@ class TelegramClient { if (update is TdlibIsolateReceiveData) { final TdlibClient? tdlibClient = tdlib.clients[update.clientId]; - final UpdateTd updateTd = UpdateTd( + final UpdateTelegramClientTdlib updateTd = UpdateTelegramClientTdlib( update: update.updateData, client_id: update.clientId, client_option: () { diff --git a/package/telegram_client/lib/telegram_client/update_telegram_client.dart b/package/telegram_client/lib/telegram_client/update_telegram_client.dart index 97199ac6..f599615a 100644 --- a/package/telegram_client/lib/telegram_client/update_telegram_client.dart +++ b/package/telegram_client/lib/telegram_client/update_telegram_client.dart @@ -60,15 +60,11 @@ class UpdateTelegramClient { }); Map tgClientData() { - if (telegramClientData.telegramClientType == - TelegramClientType.telegam_bot_api) { - final Map decyprt = json.decode( - tg.telegramBotApi.telegram_crypto.decrypt(data_base64: query["tg"])); - - if (decyprt["client_tg_user_id"] == null || - decyprt["client_tg_user_id"] == 0) { - decyprt["client_tg_user_id"] = - TgUtils.parserBotUserIdFromToken(decyprt["client_token"]); + if (telegramClientData.telegramClientType == TelegramClientType.telegam_bot_api) { + final Map decyprt = json.decode(tg.telegramBotApi.crypto.decrypt(data_base64: query["tg"])); + + if (decyprt["client_tg_user_id"] == null || decyprt["client_tg_user_id"] == 0) { + decyprt["client_tg_user_id"] = TgUtils.parserBotUserIdFromToken(decyprt["client_token"]); } return decyprt; } @@ -80,16 +76,14 @@ class UpdateTelegramClient { required bool is_lite, required UpdataOptionTelegramClient updataOptionTelegramClient, }) async { - if (telegramClientData.telegramClientType == - TelegramClientType.telegam_bot_api) { + if (telegramClientData.telegramClientType == TelegramClientType.telegam_bot_api) { return rawData; } if (rawData["@type"] == "updateAuthorizationState") { return rawData; } - if (rawData["@type"] == "updateNewCallbackQuery" || - rawData["@type"] == "updateNewInlineCallbackQuery") { + if (rawData["@type"] == "updateNewCallbackQuery" || rawData["@type"] == "updateNewInlineCallbackQuery") { return await tg.callbackQuery_toJson( update: rawData, telegramClientData: telegramClientData,