jsonMessage method

Future<Map> jsonMessage(
  1. Map update, {
  2. Map? from_data,
  3. Map? chat_data,
  4. bool? isUseCache,
  5. Duration? durationCacheExpire,
  6. bool is_detail = false,
  7. required bool is_skip_reply_message,
  8. bool is_super_detail = false,
  9. bool is_more_detail = false,
  10. bool is_from_send_message = false,
  11. required int clientId,
})

convert tdlib update to bot api for more humanize

Implementation

Future<Map> jsonMessage(
  Map update, {
  Map? from_data,
  Map? chat_data,
  bool? isUseCache,
  Duration? durationCacheExpire,
  bool is_detail = false,
  required bool is_skip_reply_message,
  bool is_super_detail = false,
  bool is_more_detail = false,
  bool is_from_send_message = false,
  required int clientId,
}) async {
  try {
    if (update["@type"] == "message") {
      Map json = {
        "id": update["id"],
      };
      Map chat_json = {
        "id": update["chat_id"],
        "first_name": "",
        "title": "",
        "type": "",
        "detail": {},
        "last_message": {},
      };
      if (update["is_channel_post"] ?? false) {
        chat_json["type"] = "channel";
        chat_json["title"] = "";
      } else {
        if (RegExp("^-100", caseSensitive: false)
            .hashData(update["chat_id"])) {
          chat_json["type"] = "supergroup";
          chat_json["title"] = "";
        } else if (RegExp("^-", caseSensitive: false)
            .hashData(update["chat_id"])) {
          chat_json["type"] = "group";
          chat_json["title"] = "";
        } else {
          chat_json["type"] = "private";
        }
      }

      if (!is_super_detail) {
        if (chat_json["type"] != "private") {
          chat_json.remove("first_name");
        } else {
          chat_json.remove("title");
        }
      }

      bool is_chat_not_same = true;
      if (is_super_detail) {
        try {
          if (chat_data != null) {
            if (update["chat_id"] == chat_data["id"]) {
              is_chat_not_same = false;
              chat_json = chat_data;
            }
          }
        } catch (e) {}
        if (is_chat_not_same) {
          try {
            var chatResult = await getChat(
              update["chat_id"],
              is_detail: is_detail,
              is_super_detail: is_super_detail,
              is_more_detail: is_more_detail,
              clientId: clientId,
              isUseCache: isUseCache,
              durationCacheExpire: durationCacheExpire,
              is_skip_reply_message: true,
            );
            if (chatResult["ok"] == true) {
              chat_json = chatResult["result"];
            }
          } catch (e) {}
        }
      }

      json["is_outgoing"] = update["is_outgoing"] ?? false;
      json["is_pinned"] = update["is_pinned"] ?? false;
      if (update["sender_id"] is Map) {
        Map from_json = {
          "id": 0,
          "first_name": "",
          "title": "",
          "type": "",
          "detail": {},
          "last_message": {}
        };
        if (update["sender_id"]["user_id"] != null) {
          from_json["id"] = update["sender_id"]["user_id"];
          if (update["chat_id"] == from_json["id"]) {
            from_json["type"] = chat_json["type"];
          } else if (RegExp("^-", caseSensitive: false)
              .hashData(from_json["chat_id"])) {
            from_json["type"] = "group";
          } else {
            from_json["type"] = "private";
          }

          if (is_super_detail) {
            if (update["chat_id"] == update["sender_id"]["user_id"]) {
              from_json = chat_json;
            } else {
              bool is_from_not_same = true;
              if (from_data != null) {
                if (update["chat_id"] == from_data["id"]) {
                  is_from_not_same = false;
                  from_json = from_data;
                }
              }
              if (is_from_not_same) {
                try {
                  var fromResult = await getUser(
                    update["sender_id"]["user_id"],
                    clientId: clientId,
                    isUseCache: isUseCache,
                    durationCacheExpire: durationCacheExpire,
                  );
                  if (fromResult["ok"] == true) {
                    from_json = fromResult["result"];
                  }
                } catch (e) {}
              }
            }
          }
        }

        if (update["sender_id"]["chat_id"] != null) {
          from_json["id"] = update["sender_id"]["chat_id"];
          if (update["chat_id"] == from_json["id"]) {
            from_json["type"] = chat_json["type"];
          } else if (RegExp("^-", caseSensitive: false)
              .hashData(from_json["chat_id"])) {
            from_json["type"] = "group";
          } else {
            from_json["type"] = "private";
          }

          if (is_super_detail) {
            if (update["chat_id"] == update["sender_id"]["chat_id"]) {
              from_json = chat_json;
            } else {
              bool is_from_not_same = true;
              if (from_data != null) {
                if (update["chat_id"] == from_data["id"]) {
                  is_from_not_same = false;
                  from_json = from_data;
                }
              }
              if (is_from_not_same) {
                try {
                  var fromResult = await getChat(
                    update["sender_id"]["chat_id"],
                    clientId: clientId,
                    isUseCache: isUseCache,
                    durationCacheExpire: durationCacheExpire,
                    is_skip_reply_message: true,
                  );
                  if (fromResult["ok"] == true) {
                    from_json = fromResult["result"];
                  }
                } catch (e) {}
              }
            }
          }
        }

        if (!is_super_detail) {
          if (from_json["type"] != "private") {
            from_json.remove("first_name");
          } else {
            from_json.remove("title");
          }
        }
        json["from"] = from_json;
      }

      json["chat"] = chat_json;
      json["date"] = update["date"];
      json["message_id"] = update["id"];
      json["api_message_id"] = TgUtils.messageTdlibToApi(update["id"]);
      update.forEach((key, value) {
        try {
          if (value is bool) {
            json[key] = value;
          }
        } catch (e) {}
      });

      if (chat_json["type"] == "channel") {
        if (update["author_signature"].toString().isNotEmpty) {
          json["author_signature"] = update["author_signature"];
        }
      }

      if (update["forward_info"] is Map) {
        var forward_info = update["forward_info"];
        if (forward_info["origin"] is Map) {
          if (forward_info["origin"]["@type"] ==
              "messageForwardOriginChannel") {
            Map forward_json = {
              "id": forward_info["origin"]["chat_id"],
              "first_name": "",
              "title": "",
              "type": "",
              "detail": {},
              "last_message": {}
            };
            try {
              var getchat_forward = await getChat(
                forward_json["id"],
                clientId: clientId,
                isUseCache: isUseCache,
                durationCacheExpire: durationCacheExpire,
                is_skip_reply_message: true,
              );
              if (getchat_forward["ok"] == true) {
                forward_json = getchat_forward["result"];
              }
            } catch (e) {}
            json["forward_from_chat"] = forward_json;
            json["forward_from_message_id"] =
                forward_info["origin"]["message_id"] ?? 0;
            json["api_forward_from_message_id"] =
                TgUtils.messageTdlibToApi(json["forward_from_message_id"]);
            json["forward_from_author_signature"] =
                forward_info["origin"]["author_signature"] ?? "";
          }
          if (forward_info["origin"]["@type"] == "messageForwardOriginUser") {
            Map forward_json = {
              "id": forward_info["origin"]["sender_user_id"],
              "first_name": "",
              "title": "",
              "type": "",
              "detail": {},
              "last_message": {}
            };
            try {
              var getuser_forward = await getUser(
                forward_json["id"],
                clientId: clientId,
                isUseCache: isUseCache,
                durationCacheExpire: durationCacheExpire,
              );
              if (getuser_forward["ok"] == true) {
                forward_json = getuser_forward["result"];
              }
            } catch (e) {}
            json["forward_from_chat"] = forward_json;
          }
        }
        json["forward_date"] = forward_info["date"] ?? 0;
      }

      update["reply_to_message_id"] ??= 0;
      update["reply_in_chat_id"] ??= 0;

      if (update["reply_to_message_id"] == 0) {
        if (update["reply_to"] is Map) {
          if (update["reply_to"]["message_id"] is int) {
            update["reply_to_message_id"] = update["reply_to"]["message_id"];
          }
        }
      }

      if (update["reply_in_chat_id"] == 0) {
        if (update["reply_to"] is Map) {
          if (update["reply_to"]["chat_id"] is int) {
            update["reply_in_chat_id"] = update["reply_to"]["chat_id"];
          }
        }
      }
      if (update["reply_to_message_id"] != 0 &&
          update["reply_in_chat_id"] != 0 &&
          !is_skip_reply_message) {
        try {
          var get_message = await getMessage(
            update["reply_in_chat_id"],
            update["id"],
            methodName: "getRepliedMessage",
            is_detail: true,
            is_super_detail: true,
            isUseCache: isUseCache,
            is_skip_reply_message: true,
            durationCacheExpire: durationCacheExpire,
            clientId: clientId,
          );
          if (get_message["ok"] == true) {
            json["reply_to_message"] = get_message["result"];
            json["reply_to_message"]["message_id"] =
                json["reply_to_message"]["message_id"];
            json["reply_to_message"]["api_message_id"] =
                TgUtils.messageTdlibToApi(
                    json["reply_to_message"]["message_id"]);
          }
        } catch (e) {}
      }

      if (update["content"] is Map) {
        List old_entities = [];

        if (update["content"]["@type"] == "messageText") {
          json["type_content"] = "text";
          if (update["content"]["text"] is Map) {
            if (update["content"]["text"]["@type"] == "formattedText") {
              json["text"] = update["content"]["text"]["text"];
              old_entities = update["content"]["text"]["entities"];
            }
          }
        }

        if (update["content"]["@type"] == "messagePhoto") {
          json["type_content"] = "photo";
          if (update["content"]["photo"] is Map) {
            if (update["content"]["photo"]["@type"] == "photo") {
              var size_photo = [];
              var photo = update["content"]["photo"]["sizes"];
              for (var i = 0; i < photo.length; i++) {
                var photo_json = photo[i];
                var json_photo = {};
                json_photo["id"] = photo_json["photo"]["id"];
                if (photo_json["photo"]["local"]["@type"] == "localFile") {
                  json_photo["path"] = photo_json["photo"]["local"]["path"];
                }
                if (photo_json["photo"]["remote"]["@type"] == "remoteFile") {
                  json_photo["file_id"] = photo_json["photo"]["remote"]["id"];
                }
                if (photo_json["photo"]["remote"]["unique_id"] != null) {
                  json_photo["file_unique_id"] =
                      photo_json["photo"]["remote"]["unique_id"];
                }
                json_photo["file_size"] = photo_json["photo"]["size"];
                json_photo["width"] = photo_json["width"];
                json_photo["height"] = photo_json["height"];
                size_photo.add(json_photo);
              }
              json["photo"] = size_photo;
            }
          }
        }

        if (update["content"]["@type"] == "messageVideo") {
          json["type_content"] = "video";
          if (update["content"]["video"] is Map) {
            if (update["content"]["video"]["@type"] == "video") {
              var json_video = {};
              var content_video = update["content"]["video"];
              json_video["duration"] = content_video["duration"];
              json_video["height"] = content_video["height"];
              json_video["file_name"] = content_video["file_name"];
              json_video["mime_type"] = content_video["mime_type"];
              try {
                if (update["content"]["video"]["thumbnail"] != null &&
                    update["content"]["video"]["thumbnail"]["@type"]
                            .toString()
                            .toLowerCase() ==
                        "thumbnail") {
                  var content_thumb = content_video["thumbnail"];
                  var json_thumb = {};
                  json_video["thumb"] = json_thumb;
                  json_thumb["file_id"] =
                      content_thumb["file"]["remote"]["id"];
                  json_thumb["file_unique_id"] =
                      content_thumb["file"]["remote"]["unique_id"];
                  json_thumb["file_size"] = content_thumb["file"]["size"];
                  json_thumb["width"] = content_thumb["width"];
                  json_thumb["height"] = content_thumb["height"];
                }
              } catch (e) {}
              json_video["file_id"] = content_video["video"]["remote"]["id"];
              json_video["file_size"] = content_video["video"]["size"];
              json["video"] = json_video;
            }
          }
        }

        if (update["content"]["@type"] == "messageAudio") {
          var type_content = "audio";
          json["type_content"] = "audio";
          if (update["content"]["audio"] is Map) {
            if (update["content"]["audio"]["@type"] == "audio") {
              var json_content = {};
              var content_update = update["content"][type_content];
              json_content["duration"] = content_update["duration"];
              json_content["title"] = content_update["title"];
              json_content["performer"] = content_update["performer"];
              json_content["file_name"] = content_update["file_name"];
              json_content["mime_type"] = content_update["mime_type"];
              json_content["file_id"] =
                  content_update[type_content]["remote"]["id"];
              json_content["unique_id"] =
                  content_update[type_content]["remote"]["unique_id"];
              json_content["file_size"] =
                  content_update[type_content]["size"];
              json[type_content] = json_content;
            }
          }
        }

        if (update["content"]["@type"] == "messageAnimation") {
          var type_content = "animation";
          json["type_content"] = "animation";
          if (update["content"]["animation"] is Map) {
            if (update["content"]["animation"]["@type"] == "animation") {
              var json_content = {};
              var content_update = update["content"][type_content];
              json_content["duration"] = content_update["duration"];
              json_content["width"] = content_update["width"];
              json_content["height"] = content_update["height"];
              json_content["file_name"] = content_update["file_name"];
              json_content["mime_type"] = content_update["mime_type"];
              json_content["mime_type"] = content_update["mime_type"];
              json_content["has_stickers"] = content_update["has_stickers"];

              try {
                if (update["content"][type_content]["thumbnail"] != null &&
                    update["content"][type_content]["thumbnail"]["@type"]
                            .toString()
                            .toLowerCase() ==
                        "thumbnail") {
                  var content_thumb = content_update["thumbnail"];
                  var json_thumb = {};
                  json_thumb["file_id"] =
                      content_thumb["file"]["remote"]["id"];
                  json_thumb["file_unique_id"] =
                      content_thumb["file"]["remote"]["unique_id"];
                  json_thumb["file_size"] = content_thumb["file"]["size"];
                  json_thumb["width"] = content_thumb["width"];
                  json_thumb["height"] = content_thumb["height"];
                  json_content["thumb"] = json_thumb;
                }
              } catch (e) {}
              json_content["file_id"] =
                  content_update[type_content]["remote"]["id"];
              json_content["unique_id"] =
                  content_update[type_content]["remote"]["unique_id"];
              json_content["file_size"] =
                  content_update[type_content]["size"];
              json[type_content] = json_content;
            }
          }
        }

        if (update["content"]["@type"] == "messageContact") {
          var type_content = "contact";
          json["type_content"] = type_content;
          if (update["content"][type_content] is Map) {
            if (update["content"][type_content]["@type"] == type_content) {
              var json_content = {};
              var content_update = update["content"][type_content];
              json_content["phone_number"] = content_update["phone_number"];
              json_content["first_name"] = content_update["first_name"];
              json_content["last_name"] = content_update["last_name"];
              json_content["vcard"] = content_update["vcard"];
              json_content["user_id"] = content_update["user_id"];
              json[type_content] = json_content;
            }
          }
        }

        if (update["content"]["@type"] == "messagePoll") {
          var type_content = "poll";
          json["type_content"] = type_content;
          if (update["content"][type_content] is Map) {
            if (update["content"][type_content]["@type"] == type_content) {
              var json_content = {};
              var content_update = update["content"][type_content];
              json_content["id"] = content_update["id"];
              json_content["question"] = content_update["question"];
              json_content["options"] = content_update["options"];
              json_content["total_voter_count"] =
                  content_update["total_voter_count"];
              json_content["recent_voter_user_ids"] =
                  content_update["recent_voter_user_ids"];
              json_content["is_anonymous"] = content_update["is_anonymous"];
              json_content["type"] = content_update["type"];
              json_content["open_period"] = content_update["open_period"];
              json_content["close_date"] = content_update["close_date"];
              json_content["is_closed"] = content_update["is_closed"];
              json[type_content] = json_content;
            }
          }
        }

        if (update["content"]["@type"] == "messageDocument") {
          var type_content = "document";
          json["type_content"] = type_content;
          if (update["content"][type_content] is Map) {
            if (update["content"][type_content]["@type"] == type_content) {
              var json_content = {};
              var content_update = update["content"][type_content];
              json_content["file_name"] = content_update["file_name"];
              json_content["mime_type"] = content_update["mime_type"];

              json_content["file_id"] =
                  content_update[type_content]["remote"]["id"];
              json_content["unique_id"] =
                  content_update[type_content]["remote"]["unique_id"];
              json_content["file_size"] =
                  content_update[type_content]["size"];
              json[type_content] = json_content;
            }
          }
        }

        if (update["content"]["@type"] == "messageSticker") {
          var type_content = "sticker";
          json["type_content"] = type_content;
          if (update["content"][type_content] is Map) {
            if (update["content"][type_content]["@type"] == type_content) {
              var json_content = {};
              var content_update = update["content"][type_content];
              json_content["set_id"] = content_update["set_id"];
              json_content["width"] = content_update["width"];
              json_content["height"] = content_update["height"];
              json_content["emoji"] = content_update["emoji"];
              json_content["is_animated"] = content_update["is_animated"];
              json_content["is_mask"] = content_update["is_mask"];

              try {
                if (update["content"][type_content]["thumbnail"] != null &&
                    update["content"][type_content]["thumbnail"]["@type"]
                            .toString()
                            .toLowerCase() ==
                        "thumbnail") {
                  var content_thumb = content_update["thumbnail"];
                  var json_thumb = {};
                  json_thumb["file_id"] =
                      content_thumb["file"]["remote"]["id"];
                  json_thumb["file_unique_id"] =
                      content_thumb["file"]["remote"]["unique_id"];
                  json_thumb["file_size"] = content_thumb["file"]["size"];
                  json_thumb["width"] = content_thumb["width"];
                  json_thumb["height"] = content_thumb["height"];
                  json_content["thumb"] = json_thumb;
                }
              } catch (e) {}

              json_content["file_id"] =
                  content_update[type_content]["remote"]["id"];
              json_content["unique_id"] =
                  content_update[type_content]["remote"]["unique_id"];
              json_content["file_size"] =
                  content_update[type_content]["size"];
              json[type_content] = json_content;
            }
          }
        }

        if (update["content"]["@type"] == "messageVoiceNote") {
          var type_content = "voice_note";
          json["type_content"] = type_content;
          if (update["content"][type_content] is Map) {
            if (update["content"][type_content]["@type"] == "voiceNote") {
              var json_content = {};
              var content_update = update["content"][type_content];

              json_content["duration"] = content_update["duration"];
              json_content["waveform"] = content_update["waveform"];
              json_content["mime_type"] = content_update["mime_type"];

              json_content["file_id"] =
                  content_update["voice"]["remote"]["id"];
              json_content["unique_id"] =
                  content_update["voice"]["remote"]["unique_id"];
              json_content["file_size"] = content_update["voice"]["size"];
              json["voice"] = json_content;
            }
          }
        }
        if (update["content"]["@type"] == "messageChatJoinByLink") {
          json["type_content"] = "new_member";
          Map new_member_from = json["from"];
          try {
            new_member_from.remove("detail");
          } catch (e) {}
          json["new_members"] = [new_member_from];
        }
        if (update["content"]["@type"] == "messageChatAddMembers") {
          json["type_content"] = "new_member";
          List new_members = [];
          if (is_super_detail) {
            for (var i = 0;
                i < update["content"]["member_user_ids"].length;
                i++) {
              var loop_data = update["content"]["member_user_ids"][i];
              try {
                Map result_user = await getUser(
                  loop_data,
                  clientId: clientId,
                  isUseCache: isUseCache,
                  durationCacheExpire: durationCacheExpire,
                );
                try {
                  result_user.remove("detail");
                } catch (e) {}
                new_members.add(result_user["result"]);
              } catch (e) {
                new_members.add({
                  "id": loop_data,
                  "is_bot": false,
                  "first_name": "",
                  "last_name": "",
                  "type": "private"
                });
              }
            }
          } else {
            new_members = update["content"]["member_user_ids"];
          }
          json["new_members"] = new_members;
        }

        if (update["content"]["@type"] == "messageChatDeleteMember") {
          json["type_content"] = "left_member";
          List left_member = [];
          if (is_super_detail) {
            try {
              Map result_user = await getUser(
                update["content"]["user_id"],
                clientId: clientId,
                isUseCache: isUseCache,
                durationCacheExpire: durationCacheExpire,
              );
              try {
                result_user.remove("detail");
              } catch (e) {}
              left_member.add(result_user["result"]);
            } catch (e) {
              left_member.add({
                "id": update["content"]["user_id"],
                "is_bot": false,
                "first_name": "",
                "last_name": "",
                "type": "private"
              });
            }
          } else {
            left_member.add({
              "id": update["content"]["user_id"],
              "is_bot": false,
              "first_name": "",
              "last_name": "",
              "type": "private"
            });
          }
          json["left_member"] = left_member;
        }

        // caption
        if (update["content"]["caption"] is Map) {
          if (update["content"]["caption"]["@type"] == "formattedText") {
            if (update["content"]["caption"]["text"].toString().isNotEmpty) {
              json["caption"] = update["content"]["caption"]["text"];
            }
            old_entities = update["content"]["caption"]["entities"];
          }
        }

        List new_entities = [];
        for (var i = 0; i < old_entities.length; i++) {
          var data_entities = old_entities[i];
          try {
            var json_entities = {};
            json_entities["offset"] = data_entities["offset"];
            json_entities["length"] = data_entities["length"];
            if (data_entities["type"]["@type"] != null) {
              var type_entities = data_entities["type"]["@type"]
                  .toString()
                  .toLowerCase()
                  .replaceAll(
                      RegExp("textEntityType", caseSensitive: false), "")
                  .replaceAll(
                      RegExp("textUrl", caseSensitive: false), "text_link")
                  .replaceAll(RegExp("bot_command", caseSensitive: false),
                      "bot_command")
                  .replaceAll(RegExp("mentionname", caseSensitive: false),
                      "text_mention");
              json_entities["type"] = type_entities;
              if (data_entities["type"]["url"] != null) {
                json_entities["url"] = data_entities["type"]["url"];
              }
              if (type_entities == "text_mention" &&
                  data_entities["type"]["user_id"] != null) {
                var entitiesUserId = data_entities["type"]["user_id"];
                var fromJson = {"id": entitiesUserId};
                try {
                  var fromResult = await getChat(
                    update["sender_id"]["user_id"],
                    clientId: clientId,
                    isUseCache: isUseCache,
                    durationCacheExpire: durationCacheExpire,
                    is_skip_reply_message: true,
                  );
                  if (fromResult["ok"] == true) {
                    fromJson = fromResult["result"];
                  }
                } catch (e) {}
                json_entities["user"] = fromJson;
              }
            }
            new_entities.add(json_entities);
          } catch (e) {}
        }
        json["entities"] = new_entities;
      }

      if (update["reply_markup"] is Map) {
        Map update_reply_markup = update["reply_markup"];
        json["reply_markup"] = {};
        if (update_reply_markup["resize_keyboard"] is bool) {
          json["reply_markup"]["resize_keyboard"] =
              (update_reply_markup["resize_keyboard"] == true);
        }
        if (update_reply_markup["one_time"] is bool) {
          json["reply_markup"]["one_time"] =
              (update_reply_markup["one_time"] == true);
        }
        if (update_reply_markup["is_personal"] is bool) {
          json["reply_markup"]["is_personal"] =
              (update_reply_markup["is_personal"] == true);
        }

        if (update_reply_markup["input_field_placeholder"] is String) {
          json["reply_markup"]["input_field_placeholder"] =
              (update_reply_markup["input_field_placeholder"] is String)
                  ? (update_reply_markup["input_field_placeholder"] as String)
                  : "";
        }
        try {
          if (update_reply_markup["@type"] == "replyMarkupShowKeyboard") {
            List raw_keyboard = update_reply_markup["rows"];
            List<List<Map>> keyboards_data = [];

            for (var i = 0; i < raw_keyboard.length; i++) {
              dynamic raw_keyboards = raw_keyboard[i];

              if (raw_keyboards is List) {
                List<Map> new_keyboard = [];
                for (var ii = 0; ii < raw_keyboards.length; ii++) {
                  dynamic raw_keyboard_data = raw_keyboards[ii];
                  if (raw_keyboard_data is Map) {
                    Map jsonDataKeyboard = {
                      "text": raw_keyboard_data["text"],
                    };
                    if (raw_keyboard_data["type"] is Map) {
                      // https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1_inline_keyboard_button_type.html
                      if (raw_keyboard_data["@type"] ==
                          "keyboardButtonTypeRequestPoll") {
                        jsonDataKeyboard["is_request_poll"] = true;
                      }
                      if (raw_keyboard_data["@type"] ==
                          "keyboardButtonTypeRequestLocation") {
                        jsonDataKeyboard["is_request_location"] = true;
                      }
                      //
                      if (raw_keyboard_data["@type"] ==
                          "keyboardButtonTypeRequestPhoneNumber") {
                        jsonDataKeyboard["is_request_phone_number"] = true;
                      }
                    }
                    new_keyboard.add(jsonDataKeyboard);
                  }
                }
                keyboards_data.add(new_keyboard);
              }
            }
            json["reply_markup"]["keyboard"] = keyboards_data;
          }
          if (update_reply_markup["@type"] == "replyMarkupInlineKeyboard") {
            List raw_keyboard = update_reply_markup["rows"];
            List<List<Map>> keyboards_data = [];

            for (var i = 0; i < raw_keyboard.length; i++) {
              dynamic raw_keyboards = raw_keyboard[i];

              if (raw_keyboards is List) {
                List<Map> new_keyboard = [];
                for (var ii = 0; ii < raw_keyboards.length; ii++) {
                  dynamic raw_keyboard_data = raw_keyboards[ii];
                  if (raw_keyboard_data is Map) {
                    Map jsonDataKeyboard = {
                      "text": raw_keyboard_data["text"],
                    };
                    if (raw_keyboard_data["type"] is Map) {
                      // https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1_inline_keyboard_button_type.html
                      if (raw_keyboard_data["@type"] ==
                          "inlineKeyboardButtonTypeCallback") {
                        if (raw_keyboard_data["type"]["data"] is String) {
                          jsonDataKeyboard["callback_data"] = convert.utf8
                              .decode(convert.base64
                                  .decode(raw_keyboard_data["type"]["data"]));
                        }
                      }
                      //
                      if (raw_keyboard_data["@type"] ==
                          "inlineKeyboardButtonTypeUrl") {
                        if (raw_keyboard_data["type"]["url"] is String) {
                          jsonDataKeyboard["url"] =
                              raw_keyboard_data["type"]["url"];
                        }
                      }
                    }
                    new_keyboard.add(jsonDataKeyboard);
                  }
                }
                keyboards_data.add(new_keyboard);
              }
            }
            json["reply_markup"]["inline_keyboard"] = keyboards_data;
          }
        } catch (e, stack) {}
      }
      if (is_detail && is_from_send_message) {
        return {"ok": true, "result": json};
      }
      if (is_detail) {
        if (is_super_detail) {
          if (json["chat"]["type"] != null) {
            if (json["chat"]["type"] == "channel") {
              return {
                "ok": true,
                "result": {"update_channel_post": json}
              };
            } else {
              return {
                "ok": true,
                "result": {"update_message": json}
              };
            }
          }
        } else {
          if (json["chat"]["type"] == "channel") {
            return {
              "ok": true,
              "result": {"update_channel_post": json}
            };
          } else {
            return {
              "ok": true,
              "result": {"update_message": json}
            };
          }
        }
      }
      return {"ok": true, "result": json};
    }
    if (update["@type"] == "updateNewCallbackQuery") {
      Map json = {};
      Map from = {"id": update["sender_user_id"]};
      Map chat = {"id": update["chat_id"]};
      json["id"] = update["id"];

      if (is_super_detail) {
        try {
          var fromResult = await getChat(
            chat["id"],
            clientId: clientId,
            isUseCache: isUseCache,
            durationCacheExpire: durationCacheExpire,
            is_skip_reply_message: true,
          );
          if (fromResult["ok"] == true) {
            chat = fromResult["result"];
          }
        } catch (e) {}
        try {
          var fromResult = await getUser(
            from["id"],
            clientId: clientId,
            isUseCache: isUseCache,
            durationCacheExpire: durationCacheExpire,
          );
          if (fromResult["ok"] == true) {
            from = fromResult["result"];
          }
        } catch (e) {}
      }

      try {
        var get_message = await getMessage(
          chat["id"],
          update["message_id"],
          methodName: "getMessageLocally",
          isUseCache: isUseCache,
          durationCacheExpire: durationCacheExpire,
          is_detail: true,
          is_super_detail: true,
          clientId: clientId,
        );
        if (get_message["ok"] == true) {
          if (get_message["result"]["update_message"] != null) {
            json["message"] = get_message["result"]["update_message"];
          }

          if (get_message["result"]["update_channel_post"] != null) {
            json["message"] = get_message["result"]["update_channel_post"];
          }
        }
      } catch (e) {}
      json["api_message_id"] =
          TgUtils.messageTdlibToApi(update["message_id"]);
      json["message_id"] = update["message_id"];
      json["from"] = from;
      json["chat"] = chat;
      json["chat_instance"] = update["chat_instance"];
      json["data"] = convert.utf8
          .decode(convert.base64.decode(update["payload"]["data"]));
      // json["data"] = Buffer.from(update["payload"]["data"], 'base64').toStringEncode('utf8');
      return {
        "ok": true,
        "result": {"callback_query": json}
      };
    }

    if (update["@type"] == "updateChatMember") {
      Map json = {};
      Map chat = {"id": update["chat_id"]};
      Map from = {"id": update["actor_user_id"]};
      if (is_super_detail) {
        try {
          var fromResult = await getChat(
            chat["id"],
            clientId: clientId,
            isUseCache: isUseCache,
            durationCacheExpire: durationCacheExpire,
            is_skip_reply_message: true,
          );
          if (fromResult["ok"] == true) {
            chat = fromResult["result"];
          }
        } catch (e) {}
        try {
          var fromResult = await getUser(
            from["id"],
            clientId: clientId,
            isUseCache: isUseCache,
            durationCacheExpire: durationCacheExpire,
          );
          if (fromResult["ok"] == true) {
            from = fromResult["result"];
          }
        } catch (e) {}
      }
      json["from"] = from;
      json["chat"] = chat;
      json["date"] = update["date"];
      if (update["old_chat_member"]["@type"] == "chatMember") {
        Map json_new_member = {};
        if (update["old_chat_member"]["member_id"]["@type"] ==
            "messageSenderUser") {
          Map json_data_user = {
            "id": update["old_chat_member"]["member_id"]["user_id"]
          };
          if (is_super_detail) {
            try {
              var fromResult = await getUser(
                json_data_user["id"],
                clientId: clientId,
                isUseCache: isUseCache,
                durationCacheExpire: durationCacheExpire,
              );
              if (fromResult["ok"] == true) {
                json_data_user = fromResult["result"];
              }
            } catch (e) {}
          }
          json_new_member["user"] = json_data_user;
        }

        json_new_member["status"] = update["old_chat_member"]["status"]
                ["@type"]
            .toString()
            .replaceAll(RegExp(r"chatMemberStatus", caseSensitive: false), "")
            .toLowerCase();
        json["old_member"] = json_new_member;
      }
      if (update["new_chat_member"]["@type"] == "chatMember") {
        Map json_new_member = {};

        if (update["new_chat_member"]["member_id"]["@type"] ==
            "messageSenderUser") {
          Map json_data_user = {
            "id": update["new_chat_member"]["member_id"]["user_id"]
          };
          if (is_super_detail) {
            try {
              var fromResult = await getUser(
                json_data_user["id"],
                clientId: clientId,
                isUseCache: isUseCache,
                durationCacheExpire: durationCacheExpire,
              );
              if (fromResult["ok"] == true) {
                json_data_user = fromResult["result"];
              }
            } catch (e) {}
          }
          json_new_member["user"] = json_data_user;
        }

        json_new_member["status"] = update["new_chat_member"]["status"]
                ["@type"]
            .toString()
            .replaceAll(RegExp(r"chatMemberStatus", caseSensitive: false), "")
            .toLowerCase();
        json["new_member"] = json_new_member;
      }

      return {
        "ok": true,
        "result": {"chat_member": json}
      };
    }

    if (update["@type"] == "updateNewInlineQuery") {
      Map json = {};
      Map from = {"id": update["sender_user_id"]};
      json["id"] = update["id"];
      try {
        var fromResult = await getUser(
          from["id"],
          clientId: clientId,
          isUseCache: isUseCache,
          durationCacheExpire: durationCacheExpire,
        );
        if (fromResult["ok"] == true) {
          from = fromResult["result"];
        }
      } catch (e) {}
      json["from"] = from;
      json["chat_type"] = update["chat_type"]["@type"]
          .toString()
          .replaceAll(RegExp("chatType", caseSensitive: false), "")
          .toLowerCase();
      try {
        if (json["chat_type"] == "supergroup" &&
            update["chat_type"]["is_channel"]) {
          json["chat_type"] = "channel";
        }
      } catch (e) {}
      json["query"] = update["query"];
      json["offset"] = update["offset"];
      return {
        "ok": true,
        "result": {"inline_query": json}
      };
    }
  } catch (e) {
    update["ok"] = false;
    return update;
  }
  update["ok"] = false;
  return update;
}