Implementation
Future<Map> message_Message({
required Map message,
required TelegramClientData telegramClientData,
bool is_skip_reply_message = false,
required bool is_lite,
bool? isUseCache,
Duration? durationCacheExpire,
}) async {
// message.printPretty(2);
final Map message_thread_json = <dynamic, dynamic>{};
// message.printPretty(2);
if (message["message_thread_id"] is int &&
message["message_thread_id"] > 0) {
if (message["is_topic_message"] == true) {
message_thread_json["message_thread_id"] = message["message_thread_id"];
}
}
final Map message_from_json = <dynamic, dynamic>{};
final Map message_sender_chat_json = <dynamic, dynamic>{};
if (message["sender_id"] is Map) {
if (message["sender_id"]["@type"] == "messageSenderUser") {
message_from_json["from"] = <dynamic, dynamic>{};
if (message["sender_id"]["user_id"] is int) {
message_from_json["from"]["id"] = message["sender_id"]["user_id"];
if (is_lite) {
(message_from_json["from"] as Map).addAll({
"first_name": "",
"last_name": "",
"type": "user",
});
} else {
final Map res = await request(
parameters: {
"@type": "getUser",
"user_id": message_from_json["from"]["id"],
},
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
telegramClientData: telegramClientData,
is_return_as_api: false,
);
(message_from_json["from"] as Map).addAll(res);
}
}
}
if (message["sender_id"]["@type"] == "messageSenderChat") {
message_sender_chat_json["sender_chat"] = <dynamic, dynamic>{};
if (message["sender_id"]["chat_id"] is int) {
message_sender_chat_json["sender_chat"]["id"] =
message["sender_id"]["chat_id"];
if (is_lite) {
} else {
final Map res = await request(
parameters: {
"@type": "getChat",
"chat_id": message_sender_chat_json["sender_chat"]["id"],
},
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
telegramClientData: telegramClientData,
is_return_as_api: false,
);
(message_sender_chat_json["sender_chat"] as Map).addAll(res);
}
}
}
}
final Map message_chat_json = <dynamic, dynamic>{};
if (message["chat_id"] is int) {
message_chat_json["id"] = message["chat_id"];
//
// if (is_lite) {
// } else {
final Map res = await request(
parameters: {
"@type": "getChat",
"chat_id": message_chat_json["id"],
},
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
telegramClientData: telegramClientData,
is_return_as_api: false,
);
message_chat_json.addAll(res);
// }
}
if (message["is_channel_post"] != true) {
if (message_sender_chat_json["sender_chat"] is Map) {
if (message["chat_id"] ==
message_sender_chat_json["sender_chat"]["id"]) {
message_from_json["from"] = <dynamic, dynamic>{
"id": 1087968824,
"is_bot": true,
};
if (is_lite) {
} else {
try {
final Map res = await request(
parameters: {
"@type": "getUser",
"user_id": 1087968824,
},
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
telegramClientData: telegramClientData,
is_return_as_api: false,
);
(message_from_json["from"] as Map).addAll(res);
} catch (e) {}
}
} else {
message_from_json["from"] =
message_sender_chat_json["sender_chat"] as Map;
}
}
}
if (telegramClientData.is_bot == false) {
if ((message_from_json["from"] is Map)) {
(message_from_json["from"] as Map)
.removeWhere((key, value) => key == "phone_number");
}
message_chat_json.removeWhere((key, value) => key == "phone_number");
}
final Map msg = <dynamic, dynamic>{
"@type": "message",
"id": message["id"],
"message_id": TgUtils.messageTdlibToApi(message["id"]),
...message_thread_json,
...message_from_json,
...message_sender_chat_json,
"chat": message_chat_json,
"date": message["date"],
"is_outgoing": (message["is_outgoing"] == true),
"is_lite": is_lite,
"content_type": "",
};
if (message["is_channel_post"] == true) {
msg["chat"]["type"] = "channel";
} else {}
if (message["reply_to"] is Map) {
if (is_skip_reply_message == false) {
if (message["reply_to"]["@type"] == "messageReplyToMessage") {
final int message_reply_to_chat_id = message["reply_to"]["chat_id"];
final int message_reply_to_message_id =
message["reply_to"]["message_id"];
if (message_reply_to_message_id > 0) {
try {
final Map message_reply_to = await invoke(
parameters: {
"@type": "getRepliedMessage",
"chat_id": message_reply_to_chat_id,
"message_id": message["id"],
},
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
telegramClientData: telegramClientData,
);
msg["reply_to_message"] = await message_Message(
message: message_reply_to,
telegramClientData: telegramClientData,
is_skip_reply_message: true,
is_lite: is_lite,
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
);
} catch (e) {}
}
}
}
}
if (message["edit_date"] is int && message["edit_date"] > 0) {
msg["edit_date"] = message["edit_date"];
}
if (message["forward_info"] is Map) {
final Map forwardInfo = message["forward_info"];
forwardInfo.forEach((key, value) {
if (key == "@type") {
return;
}
if (value is Map) {
} else {
msg["forward_${key}"] = value;
}
});
}
if (message["author_signature"] is String &&
(message["author_signature"] as String).isNotEmpty) {
msg["author_signature"] = message["author_signature"];
}
if (message["content"] is Map) {
// ignore:
// List text_entities = [];
// ignore:
// List caption_entities = [];
final Map message_content = message["content"];
if (message_content["@type"] == "messageChatShared") {
msg["user_shared"] = <dynamic, dynamic>{
"chat_id": message_content["chat_id"],
"request_id": message_content["button_id"],
};
}
if (message_content["@type"] == "messageUserShared") {
msg["user_shared"] = <dynamic, dynamic>{
"user_id": message_content["user_id"],
"request_id": message_content["button_id"],
};
}
if (message_content["@type"] == "messageLocation") {
msg["location"] = <dynamic, dynamic>{};
message_content.forEach((key, value) {
if (["@type"].contains(key)) {
return;
}
if (value is Map) {
value.forEach((key_data, value_data) {
if (["@type"].contains(key_data)) {
return;
}
msg["location"][key_data] = value_data;
});
} else {
msg["location"][key] = value;
}
});
}
if (message_content["@type"] == "messageText") {
msg["content_type"] = "text";
if (message_content["text"] is Map) {
msg["text"] = message_content["text"]["text"];
if (message["content"]["text"]["entities"] is List) {
List text_entities = message["content"]["text"]["entities"];
if (text_entities.isNotEmpty) {
msg["entities"] = await entitiesToApi(
oldEntities: text_entities,
telegramClientData: telegramClientData,
is_lite: is_lite,
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
);
}
}
}
}
if (message_content["caption"] is Map) {
if (message_content["caption"]["text"] is String &&
(message_content["caption"]["text"] as String).isNotEmpty) {
msg["caption"] = message_content["caption"]["text"];
}
if (message["content"]["caption"]["entities"] is List) {
List caption_entities = message["content"]["caption"]["entities"];
if (caption_entities.isNotEmpty) {
msg["caption_entities"] = await entitiesToApi(
oldEntities: caption_entities,
telegramClientData: telegramClientData,
is_lite: is_lite,
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
);
}
}
}
if (message_content["@type"] == "messageAnimation") {
msg["content_type"] = "animation";
/// http://0.0.0.0:8080/classtd_1_1td__api_1_1animation.html
final Map animation = <dynamic, dynamic>{};
if (message["animation"] is Map) {
if (message["animation"]["animation"] is Map) {
final Map message_animation = message["animation"]["animation"];
animation["id"] = message_animation["id"];
if (message_animation["local"] is Map) {
animation["file_path"] = message_animation["local"]["path"];
animation["is_downloading_active"] =
message_animation["local"]["is_downloading_active"];
animation["is_downloading_completed"] =
message_animation["local"]["is_downloading_completed"];
}
if (message_animation["remote"] is Map) {
animation["file_id"] = message_animation["remote"]["id"];
animation["file_unique_id"] =
message_animation["remote"]["unique_id"];
animation["file_size"] = message_animation["remote"]["size"];
}
}
animation["width"] = message["animation"]["width"];
animation["height"] = message["animation"]["height"];
animation["duration"] = message["animation"]["duration"];
// Map msg_content_thumbnail = <dynamic, dynamic>{};
animation["file_name"] = message["animation"]["file_name"];
animation["mime_type"] = message["animation"]["mime_type"];
}
msg["animation"] = animation;
}
if (message_content["@type"] == "messagePhoto") {
// http://0.0.0.0:8080/classtd_1_1td__api_1_1animation.html
final List photos = [];
if (message_content["photo"] is Map) {
final List photo_sizes = message_content["photo"]["sizes"];
for (var i = 0; i < photo_sizes.length; i++) {
final dynamic photo_data = photo_sizes[i];
if (photo_data is Map) {
if (photo_data["photo"] is Map) {
final Map jsonDataPhoto = <dynamic, dynamic>{};
final Map message_photo = photo_data["photo"];
jsonDataPhoto["id"] = message_photo["id"];
if (message_photo["local"] is Map) {
jsonDataPhoto["file_path"] = message_photo["local"]["path"];
jsonDataPhoto["is_downloading_active"] =
message_photo["local"]["is_downloading_active"];
jsonDataPhoto["is_downloading_completed"] =
message_photo["local"]["is_downloading_completed"];
}
if (message_photo["remote"] is Map) {
jsonDataPhoto["file_id"] = message_photo["remote"]["id"];
jsonDataPhoto["file_unique_id"] =
message_photo["remote"]["unique_id"];
jsonDataPhoto["file_size"] = message_photo["remote"]["size"];
}
photos.add(jsonDataPhoto);
}
}
}
}
msg["photo"] = photos;
}
if (message["content"]["@type"] == "messageVideo") {
msg["content_type"] = "video";
if (message["content"]["video"] is Map) {
if (message["content"]["video"]["@type"] == "video") {
final Map jsonVideo = <dynamic, dynamic>{};
final Map contentVideo = message["content"]["video"];
jsonVideo["duration"] = contentVideo["duration"];
jsonVideo["height"] = contentVideo["height"];
jsonVideo["file_name"] = contentVideo["file_name"];
jsonVideo["mime_type"] = contentVideo["mime_type"];
try {
if (message["content"]["video"]["thumbnail"] is Map &&
message["content"]["video"]["thumbnail"]["@type"]
.toString()
.toLowerCase() ==
"thumbnail") {
final Map contentThumb = contentVideo["thumbnail"];
final Map jsonThumb = <dynamic, dynamic>{};
jsonVideo["thumb"] = jsonThumb;
jsonThumb["file_id"] = contentThumb["file"]["remote"]["id"];
jsonThumb["file_unique_id"] =
contentThumb["file"]["remote"]["unique_id"];
jsonThumb["file_size"] = contentThumb["file"]["size"];
jsonThumb["width"] = contentThumb["width"];
jsonThumb["height"] = contentThumb["height"];
}
} catch (e) {}
jsonVideo["file_id"] = contentVideo["video"]["remote"]["id"];
jsonVideo["file_size"] = contentVideo["video"]["size"];
msg["video"] = jsonVideo;
}
}
}
// http://localhost:8080/classtd_1_1td__api_1_1audio.html
if (message["content"]["@type"] == "messageAudio") {
final String contentType = "audio";
msg["content_type"] = "audio";
if (message["content"]["audio"] is Map) {
if (message["content"]["audio"]["@type"] == "audio") {
final Map jsonContent = <dynamic, dynamic>{};
final Map contentUpdate = message["content"][contentType];
jsonContent["id"] = contentUpdate[contentType]["id"];
jsonContent["file_path"] =
contentUpdate[contentType]["local"]["path"];
jsonContent["is_downloading_active"] =
contentUpdate[contentType]["local"]["is_downloading_active"];
jsonContent["is_downloading_completed"] =
contentUpdate[contentType]["local"]["is_downloading_completed"];
jsonContent["file_id"] = contentUpdate[contentType]["remote"]["id"];
jsonContent["file_unique_id"] =
contentUpdate[contentType]["remote"]["unique_id"];
jsonContent["duration"] = contentUpdate["duration"];
jsonContent["performer"] = contentUpdate["performer"];
jsonContent["file_name"] = contentUpdate["file_name"];
jsonContent["title"] = contentUpdate["title"];
jsonContent["mime_type"] = contentUpdate["mime_type"];
jsonContent["file_size"] = contentUpdate[contentType]["size"];
msg[contentType] = jsonContent;
}
}
}
// http://localhost:8080/classtd_1_1td__api_1_1file.html
// https://core.telegram.org/bots/api#contact
if (message["content"]["@type"] == "messageContact") {
final String contentType = "contact";
msg["content_type"] = contentType;
if (message["content"][contentType] is Map) {
if (message["content"][contentType]["@type"] == contentType) {
final Map jsonContent = <dynamic, dynamic>{};
final Map contentUpdate = message["content"][contentType];
jsonContent["phone_number"] = contentUpdate["phone_number"];
jsonContent["first_name"] = contentUpdate["first_name"];
jsonContent["last_name"] = contentUpdate["last_name"];
jsonContent["user_id"] = contentUpdate["user_id"];
jsonContent["vcard"] = contentUpdate["vcard"];
msg[contentType] = jsonContent;
}
}
}
if (message["content"]["@type"] == "messagePoll") {
final String contentType = "poll";
msg["content_type"] = contentType;
if (message["content"][contentType] is Map) {
if (message["content"][contentType]["@type"] == contentType) {
final Map jsonContent = <dynamic, dynamic>{};
final Map contentUpdate = message["content"][contentType];
jsonContent["id"] = contentUpdate["id"];
jsonContent["question"] = contentUpdate["question"];
jsonContent["options"] = contentUpdate["options"];
jsonContent["total_voter_count"] =
contentUpdate["total_voter_count"];
jsonContent["recent_voter_user_ids"] =
contentUpdate["recent_voter_user_ids"];
jsonContent["is_anonymous"] = contentUpdate["is_anonymous"];
jsonContent["type"] = contentUpdate["type"];
jsonContent["open_period"] = contentUpdate["open_period"];
jsonContent["close_date"] = contentUpdate["close_date"];
jsonContent["is_closed"] = contentUpdate["is_closed"];
msg[contentType] = jsonContent;
}
}
}
if (message["content"]["@type"] == "messageDocument") {
final String contentType = "document";
msg["content_type"] = contentType;
if (message["content"][contentType] is Map) {
if (message["content"][contentType]["@type"] == contentType) {
final Map jsonContent = <dynamic, dynamic>{};
final Map contentUpdate = message["content"][contentType];
jsonContent["file_name"] = contentUpdate["file_name"];
jsonContent["mime_type"] = contentUpdate["mime_type"];
jsonContent["file_id"] = contentUpdate[contentType]["remote"]["id"];
jsonContent["unique_id"] =
contentUpdate[contentType]["remote"]["unique_id"];
jsonContent["file_size"] = contentUpdate[contentType]["size"];
msg[contentType] = jsonContent;
}
}
}
if (message["content"]["@type"] == "messageSticker") {
final String contentType = "sticker";
msg["content_type"] = contentType;
if (message["content"][contentType] is Map) {
if (message["content"][contentType]["@type"] == contentType) {
final Map jsonContent = <dynamic, dynamic>{};
final Map contentUpdate = message["content"][contentType];
jsonContent["set_id"] = contentUpdate["set_id"];
try {
final Map res = await invoke(
parameters: {
"@type": "getStickerSet",
"set_id": int.tryParse(contentUpdate["set_id"]) ??
contentUpdate["set_id"],
},
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
telegramClientData: telegramClientData,
);
jsonContent["set_name"] = res["name"];
} catch (e) {
// print(e);
}
jsonContent["width"] = contentUpdate["width"];
jsonContent["height"] = contentUpdate["height"];
jsonContent["emoji"] = contentUpdate["emoji"];
jsonContent["is_animated"] = contentUpdate["is_animated"];
jsonContent["is_mask"] = contentUpdate["is_mask"];
try {
if (message["content"][contentType]["thumbnail"] is Map &&
message["content"][contentType]["thumbnail"]["@type"]
.toString()
.toLowerCase() ==
"thumbnail") {
final Map contentThumb = contentUpdate["thumbnail"];
final Map jsonThumb = <dynamic, dynamic>{};
jsonThumb["file_id"] = contentThumb["file"]["remote"]["id"];
jsonThumb["file_unique_id"] =
contentThumb["file"]["remote"]["unique_id"];
jsonThumb["file_size"] = contentThumb["file"]["size"];
jsonThumb["width"] = contentThumb["width"];
jsonThumb["height"] = contentThumb["height"];
jsonContent["thumb"] = jsonThumb;
}
} catch (e) {}
jsonContent["file_id"] = contentUpdate[contentType]["remote"]["id"];
jsonContent["file_unique_id"] =
contentUpdate[contentType]["remote"]["unique_id"];
jsonContent["file_size"] = contentUpdate[contentType]["size"];
msg[contentType] = jsonContent;
}
}
}
if (message["content"]["@type"] == "messageVoiceNote") {
final String contentType = "voice_note";
msg["content_type"] = contentType;
if (message["content"][contentType] is Map) {
if (message["content"][contentType]["@type"] == "voiceNote") {
final Map jsonContent = <dynamic, dynamic>{};
final Map contentUpdate = message["content"][contentType];
jsonContent["duration"] = contentUpdate["duration"];
jsonContent["waveform"] = contentUpdate["waveform"];
jsonContent["mime_type"] = contentUpdate["mime_type"];
jsonContent["file_id"] = contentUpdate["voice"]["remote"]["id"];
jsonContent["unique_id"] =
contentUpdate["voice"]["remote"]["unique_id"];
jsonContent["file_size"] = contentUpdate["voice"]["size"];
msg["voice"] = jsonContent;
}
}
}
if (message["content"]["@type"] == "messageChatJoinByLink") {
msg["content_type"] = "new_member";
final Map newMemberFrom = msg["from"];
try {
newMemberFrom.remove("detail");
} catch (e) {}
msg["new_chat_members"] = [newMemberFrom];
}
if (message["content"]["@type"] == "messageScreenshotTaken") {
msg["content_type"] = "screenshot";
msg["is_screenshot"] = true;
}
if (message["content"]["@type"] == "messageStory") {
msg["content_type"] = "story";
msg["story"] = <dynamic, dynamic>{
"story_sender_chat_id": message["content"]["story_sender_chat_id"],
"story_id": message["content"]["story_id"],
};
}
}
if (message["reply_markup"] is Map) {
final Map? reply_markup = TgUtils.replyMarkupTdlibToTgApi(
replyMarkup: message["reply_markup"],
);
if (reply_markup != null) {
msg["reply_markup"] = reply_markup;
}
}
message.forEach((key, value) {
if (value is bool) {
msg[key] = value;
}
});
if (message["can_be_saved"] == false) {
msg["has_protected_content"] = true;
}
return msg;
}