replyMarkupTgApiToTdlib static method

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

Implementation

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

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

    final List keyboards_old = (replyMarkup["keyboard"] as List);

    final List keyboards = [];
    for (var i = 0; i < keyboards_old.length; i++) {
      final dynamic row = keyboards_old[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>{
              "@type": "keyboardButton",
              "text": data_row["text"],
              "type": <dynamic, dynamic>{"@type": "keyboardButtonTypeText"},
            };

            if (data_row["request_chat"] is Map) {
              jsonData["type"] = <dynamic, dynamic>{
                "@type": "keyboardButtonTypeRequestChat",
              };
              (data_row["request_chat"] as Map).forEach((key, value) {
                if (key == "request_id") {
                  jsonData["type"]["id"] = value;
                } else {
                  if (value is Map) {
                  } else {
                    jsonData["type"][key] = value;
                  }
                }
              });
            }

            if (data_row["request_user"] is Map) {
              jsonData["type"] = <dynamic, dynamic>{
                "@type": "keyboardButtonTypeRequestUser",
              };
              (data_row["request_user"] as Map).forEach((key, value) {
                if (key == "request_id") {
                  jsonData["type"]["id"] = value;
                } else {
                  if (value is Map) {
                  } else {
                    jsonData["type"][key] = value;
                  }
                }
              });
            }

            if (data_row["request_contact"] == true) {
              jsonData["type"] = <dynamic, dynamic>{
                "@type": "keyboardButtonTypeRequestPhoneNumber",
              };
            }
            if (data_row["request_location"] == true) {
              jsonData["type"] = <dynamic, dynamic>{
                "@type": "keyboardButtonTypeRequestLocation",
              };
            }

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

  if (replyMarkup["inline_keyboard"] is List) {
    final Map replyMarkupInlineKeyboard = <dynamic, dynamic>{
      "@type": "replyMarkupInlineKeyboard",
    };

    final List inline_keyboards_old =
        (replyMarkup["inline_keyboard"] as List);

    final List inline_keyboards = [];
    for (var i = 0; i < inline_keyboards_old.length; i++) {
      final dynamic row = inline_keyboards_old[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 = {
              "@type": "inlineKeyboardButton",
              "text": data_row["text"],
              "type": {},
            };

            if (data_row.containsKey("callback_data")) {
              jsonData["type"] = {
                "@type": "inlineKeyboardButtonTypeCallback",
                "data": base64.encode(utf8.encode(data_row["callback_data"])),
              };
            }

            if (data_row["web_app"] is Map) {
              jsonData["type"] = {
                "@type": "inlineKeyboardButtonTypeWebApp",
                "url": data_row["web_app"]["url"],
              };
            }

            if (data_row.containsKey("switch_inline_query_current_chat")) {
              jsonData["type"] = {
                "@type": "inlineKeyboardButtonTypeSwitchInline",
                "query": data_row["switch_inline_query_current_chat"],
                "target_chat": {
                  "@type": "targetChatCurrent",
                },
              };
            }

            if (data_row.containsKey("url")) {
              jsonData["type"] = {
                "@type": "inlineKeyboardButtonTypeUrl",
                "url": data_row["url"],
              };
            }

            new_keyboard.add(jsonData);
          }
        }
        inline_keyboards.add(new_keyboard);
      }
    }
    replyMarkupInlineKeyboard["rows"] = inline_keyboards;
    return replyMarkupInlineKeyboard;
  }
  return null;
}