replyMarkupTdlibToTgApi static method

Map? replyMarkupTdlibToTgApi({
  1. required Map replyMarkup,
})

Implementation

static Map? replyMarkupTdlibToTgApi({
  required Map replyMarkup,
}) {
  if (replyMarkup["@type"] == "replyMarkupShowKeyboard") {
    final Map replyMarkupShowKeyboard = <dynamic, dynamic>{};
    replyMarkup.forEach((key, value) {
      if (["@type"].contains(key)) {
        return;
      }

      if (value is List || value is Map) {
      } else {
        replyMarkupShowKeyboard[key] = value;
      }
    });

    if (replyMarkup["rows"] is List) {
      final List rows = replyMarkup["rows"];

      final List keyboards = [];
      for (var i = 0; i < rows.length; i++) {
        final dynamic row = rows[i];
        if (row is List) {
          final List new_keyboard = [];
          for (var index = 0; index < row.length; index++) {
            final dynamic data_row = row[index];
            if (data_row is Map) {
              final Map jsonData = <dynamic, dynamic>{
                "text": data_row["text"],
              };
              if (data_row["type"] is Map) {
                final Map data_row_type = data_row["type"];
                if (data_row_type["@type"] == "keyboardButtonTypeText") {}

                if (data_row_type["@type"] ==
                    "keyboardButtonTypeRequestChat") {
                  jsonData["request_chat"] = {};
                  data_row_type.forEach((key, value) {
                    if (key == "id") {
                      jsonData["request_chat"]["request_id"] = value;
                    } else {
                      if (value is Map) {
                      } else {
                        jsonData["request_chat"][key] = value;
                      }
                    }
                  });
                }
                if (data_row_type["@type"] ==
                    "keyboardButtonTypeRequestUser") {
                  jsonData["request_user"] = {};
                  data_row_type.forEach((key, value) {
                    if (key == "id") {
                      jsonData["request_user"]["request_id"] = value;
                    } else {
                      if (value is Map) {
                      } else {
                        jsonData["request_user"][key] = value;
                      }
                    }
                  });
                }

                if (data_row_type["@type"] ==
                    "keyboardButtonTypeRequestPhoneNumber") {
                  jsonData["request_contact"] = true;
                }
                if (data_row_type["@type"] ==
                    "keyboardButtonTypeRequestLocation") {
                  jsonData["request_location"] = true;
                }
              }

              new_keyboard.add(jsonData);
            }
          }
          keyboards.add(new_keyboard);
        }
      }
      replyMarkupShowKeyboard["keyboard"] = keyboards;
    }

    return replyMarkupShowKeyboard;
  }
  if (replyMarkup["@type"] == "replyMarkupInlineKeyboard") {
    final Map replyMarkupInlineKeyboard = <dynamic, dynamic>{};

    if (replyMarkup["rows"] is List) {
      final List rows = replyMarkup["rows"];

      final List inline_keyboards = [];
      for (var i = 0; i < rows.length; i++) {
        final dynamic row = rows[i];
        if (row is List) {
          final List new_keyboard = [];
          for (var index = 0; index < row.length; index++) {
            final dynamic data_row = row[index];
            if (data_row is Map) {
              final Map jsonData = <dynamic, dynamic>{
                "text": data_row["text"],
              };
              if (data_row["type"] is Map) {
                final Map data_row_type = data_row["type"];
                if (data_row_type["@type"] ==
                    "inlineKeyboardButtonTypeCallback") {
                  jsonData["callback_data"] =
                      utf8.decode(base64.decode(data_row_type["data"]));
                }
                if (data_row_type["@type"] ==
                    "inlineKeyboardButtonTypeWebApp") {
                  jsonData["web_app"] = {"url": data_row_type["url"]};
                }

                if (data_row_type["@type"] ==
                    "inlineKeyboardButtonTypeSwitchInline") {
                  if (data_row_type["target_chat"] is Map) {
                    if (data_row_type["target_chat"]["@type"] ==
                        "targetChatCurrent") {
                      jsonData["switch_inline_query_current_chat"] =
                          data_row_type["query"];
                    }
                  }
                }

                if (data_row_type["@type"] == "inlineKeyboardButtonTypeUrl") {
                  jsonData["url"] = data_row_type["url"];
                }

                if (data_row_type["@type"] == "inlineKeyboardButtonTypeBuy") {
                  jsonData["pay"] = true;
                }
              }

              new_keyboard.add(jsonData);
            }
          }
          inline_keyboards.add(new_keyboard);
        }
      }
      replyMarkupInlineKeyboard["inline_keyboard"] = inline_keyboards;
    }

    return replyMarkupInlineKeyboard;
  }
  return null;
}