Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
￴￴ committed Nov 24, 2024
1 parent 0f2a14b commit 8061e51
Show file tree
Hide file tree
Showing 17 changed files with 208 additions and 205 deletions.
25 changes: 7 additions & 18 deletions package/telegram_client/example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ void main(List<String> 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;
Expand All @@ -74,8 +73,7 @@ void main(List<String> 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 :");
Expand All @@ -88,8 +86,7 @@ void main(List<String> 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();

Expand All @@ -112,8 +109,7 @@ void main(List<String> 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) {
Expand All @@ -124,8 +120,7 @@ void main(List<String> 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",
Expand Down Expand Up @@ -202,21 +197,15 @@ void main(List<String> 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)) {
String command = text_command.replaceFirst(regExpCommand, "");

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,
);
}
Expand Down
22 changes: 7 additions & 15 deletions package/telegram_client/examples/tdlib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,19 @@ void main(List<String> 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: {
Expand Down Expand Up @@ -73,8 +68,7 @@ void main(List<String> 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(
Expand All @@ -84,8 +78,7 @@ void main(List<String> 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();
Expand All @@ -106,8 +99,7 @@ void main(List<String> 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);
}
Expand Down
18 changes: 9 additions & 9 deletions package/telegram_client/lib/tdlib/tdlib_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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",
Expand All @@ -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<void> closeClients() async {
Expand All @@ -181,7 +181,7 @@ class Tdlib extends TdlibNative {

/// set up authorizationStateWaitTdlibParameters new client without more code
Future<Map?> initClient(
UpdateTd update, {
UpdateTelegramClientTdlib update, {
Map? tdlibParameters,
required int clientId,
bool isVoid = false,
Expand Down Expand Up @@ -950,7 +950,7 @@ class Tdlib extends TdlibNative {

Completer<Map> completer = Completer<Map>();

var listen = on(event_update, (UpdateTd update) async {
var listen = on(event_update, (UpdateTelegramClientTdlib update) async {
if (update.client_id != clientId) {
return;
}
Expand Down Expand Up @@ -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") {
Expand Down
70 changes: 35 additions & 35 deletions package/telegram_client/lib/tdlib/tdlib_library/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ import 'package:telegram_client/tdlib/update_td.dart';

import 'package:universal_io/io.dart';

typedef TdlibOnReceiveUpdate = FutureOr<void> Function(dynamic update, TdlibBase libTdJson);
typedef TdlibOnGenerateExtraInvoke = FutureOr<String> Function(int client_id, TdlibBase libTdJson);
typedef TdlibOnGetInvokeData = FutureOr<Map> Function(String extra, int client_id, TdlibBase libTdJson);

abstract class TdlibBaseCore {
Future<bool> is_td_initialized() async{
Future<bool> is_td_initialized() async {
return false;
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<void> Function(dynamic update, TdlibBase libTdJson)? on_receive_update;
FutureOr<String> Function(int client_id, TdlibBase libTdJson)? on_generate_extra_invoke;
FutureOr<Map> 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,
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -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;
Expand Down Expand Up @@ -453,18 +453,18 @@ abstract class TdlibBase implements TdlibBaseCore {
/// receive all update data
EventEmitterListener on(
String type_update,
FutureOr<dynamic> Function(UpdateTd update) callback, {
FutureOr<dynamic> Function(UpdateTelegramClientTdlib update) callback, {
final Map<dynamic, dynamic>? 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: () {
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -664,16 +664,16 @@ abstract class TdlibBase implements TdlibBaseCore {
String? extra,
bool? isAutoGetChat,
bool? isInvokeThrowOnError,
FutureOr<String> Function(int client_id, TdlibBase libTdJson)? onGenerateExtraInvoke,
FutureOr<Map> 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;
Expand Down
Loading

0 comments on commit 8061e51

Please sign in to comment.