replyMarkup method

Map? replyMarkup(
  1. dynamic keyboard
)

convert reply markup from bot api to tdlib only support bot

Implementation

Map? replyMarkup(keyboard) {
  try {
    if (keyboard["remove_keyboard"] == true) {
      Map json = {
        "@type": "replyMarkupRemoveKeyboard",
      };
      json["is_personal"] =
          (keyboard["is_personal"] is bool) ? keyboard["is_personal"] : true;
      return json;
    }
    if (keyboard["keyboard"] is List &&
        (keyboard["keyboard"] as List).isNotEmpty) {
      Map json = {
        "@type": "replyMarkupShowKeyboard",
      };
      List array_rows = [];
      for (var i = 0; i < keyboard["keyboard"].length; i++) {
        var loop_array_keyboard = keyboard["keyboard"][i];
        List array_loop = [];
        for (var ii = 0; ii < loop_array_keyboard.length; ii++) {
          var in_loop_array_keyboard = loop_array_keyboard[ii];
          Map in_json_keyboard = {
            "@type": "keyboardButton",
            "type": {"@type": "keyboardButtonTypeText"}
          };
          if (in_loop_array_keyboard is String) {
            in_json_keyboard["text"] = in_loop_array_keyboard;
          } else if (in_loop_array_keyboard is Map) {
            if (TgUtils.getBoolean(in_loop_array_keyboard["text"])) {
              in_json_keyboard["text"] = in_loop_array_keyboard["text"];
            }
            if (in_loop_array_keyboard["request_contact"] == true) {
              in_json_keyboard["type"]["@type"] =
                  "keyboardButtonTypeRequestPhoneNumber";
            }
            if (in_loop_array_keyboard["request_location"] == true) {
              in_json_keyboard["type"]["@type"] =
                  "keyboardButtonTypeRequestLocation";
            }
            array_loop.add(in_json_keyboard);
          }
        }
        array_rows.add(array_loop);
      }
      json["rows"] = array_rows;
      json["resize_keyboard"] = (keyboard["resize_keyboard"] is bool)
          ? keyboard["resize_keyboard"]
          : true;
      json["one_time"] =
          (keyboard["one_time"] is bool) ? keyboard["one_time"] : true;
      json["is_personal"] =
          (keyboard["is_personal"] is bool) ? keyboard["is_personal"] : true;
      return json;
    }

    if (keyboard["inline_keyboard"] is List &&
        keyboard["inline_keyboard"].length > 0) {
      Map json = {
        "@type": "replyMarkupInlineKeyboard",
      };
      List array_rows = [];
      for (var i = 0; i < keyboard["inline_keyboard"].length; i++) {
        var loop_array_keyboard = keyboard["inline_keyboard"][i];
        List array_loop = [];
        for (var ii = 0; ii < loop_array_keyboard.length; ii++) {
          var in_loop_array_keyboard = loop_array_keyboard[ii];
          Map in_json_keyboard = {"@type": "inlineKeyboardButton"};
          if (TgUtils.getBoolean(in_loop_array_keyboard["text"])) {
            in_json_keyboard["text"] = in_loop_array_keyboard["text"];
          }

          if (TgUtils.getBoolean(in_loop_array_keyboard["url"])) {
            in_json_keyboard["type"] = {
              "@type": "inlineKeyboardButtonTypeUrl",
              "url": in_loop_array_keyboard["url"]
            };
          }

          if (TgUtils.getBoolean(in_loop_array_keyboard["login_url"])) {
            in_json_keyboard["type"] = {
              "@type": "inlineKeyboardButtonTypeLoginUrl",
              "query": in_loop_array_keyboard["login_url"],
            };
          }
          if (TgUtils.getBoolean(in_loop_array_keyboard["callback_data"])) {
            in_json_keyboard["type"] = {
              "@type": "inlineKeyboardButtonTypeCallback",
              "data": convert.base64.encode(convert.utf8
                  .encode(in_loop_array_keyboard["callback_data"]))
            };
          }
          if (TgUtils.getBoolean(
              in_loop_array_keyboard["callback_data_password"])) {
            in_json_keyboard["type"] = {
              "@type": "inlineKeyboardButtonTypeCallbackWithPassword",
              "data": convert.base64.encode(convert.utf8
                  .encode(in_loop_array_keyboard["callback_data_password"]))
            };
          }

          if (TgUtils.getBoolean(
              in_loop_array_keyboard["switch_inline_query"])) {
            in_json_keyboard["type"] = {
              "@type": "inlineKeyboardButtonTypeSwitchInline",
              "query": in_loop_array_keyboard["switch_inline_query"],
              "in_current_chat": false
            };
          }

          if (TgUtils.getBoolean(
              in_loop_array_keyboard["switch_inline_query_current_chat"])) {
            in_json_keyboard["type"] = {
              "@type": "inlineKeyboardButtonTypeSwitchInline",
              "query":
                  in_loop_array_keyboard["switch_inline_query_current_chat"],
              "in_current_chat": true
            };
          }
          if (TgUtils.getBoolean(in_loop_array_keyboard["callback_game"])) {
            in_json_keyboard["type"] = {
              "@type": "inlineKeyboardButtonTypeSwitchInline",
              "query": in_loop_array_keyboard["callback_game"],
              "in_current_chat": false
            };
          }
          if (TgUtils.getBoolean(in_loop_array_keyboard["user_id"])) {
            in_json_keyboard["type"] = {
              "@type": "inlineKeyboardButtonTypeUser",
              "user_id": in_loop_array_keyboard["user_id"],
            };
          }
          if (TgUtils.getBoolean(in_loop_array_keyboard["pay"])) {
            in_json_keyboard["type"] = {
              "@type": "inlineKeyboardButtonTypeBuy"
            };
          }
          array_loop.add(in_json_keyboard);
        }
        array_rows.add(array_loop);
      }
      json["rows"] = array_rows;
      return json;
    }
  } catch (e) {
    return null;
  }
  return null;
}