TelegramAPI/src/main/java/de/mrbesen/telegram/objects/TInlineKeyboardMarkup.java

104 lines
2.5 KiB
Java
Raw Normal View History

package de.mrbesen.telegram.objects;
2018-07-18 04:44:04 +02:00
import java.util.ArrayList;
2021-01-12 13:33:19 +01:00
import java.util.LinkedList;
import java.util.List;
2018-07-18 04:44:04 +02:00
import org.json.JSONArray;
import org.json.JSONObject;
public class TInlineKeyboardMarkup implements TReplyMarkup {
2021-01-12 13:33:19 +01:00
List<JSONArray> arr_btn;
2018-07-18 04:44:04 +02:00
2018-07-18 22:31:05 +02:00
public TInlineKeyboardMarkup() {
this(1);
2021-01-12 13:33:19 +01:00
}
/**
* @param rows is a hint, how many lines will be needed
*/
2018-07-18 04:44:04 +02:00
public TInlineKeyboardMarkup(int rows) {
2021-01-12 13:33:19 +01:00
arr_btn = new ArrayList<>(rows);
for(int row = 0; row < rows; ++row) {
arr_btn.add(row, new JSONArray());
2018-07-18 22:31:05 +02:00
}
2018-07-18 04:44:04 +02:00
}
2021-01-12 13:33:19 +01:00
2018-07-18 22:31:05 +02:00
/**
* Insert button in first line
* @param title
* @param url
* @return
*/
public TInlineKeyboardMarkup addUrlButton(String title, String url) {
addUrlButton(title, url, 1);
return this;
}
/**
* Insert button in nth line
* @param title
* @param url
2021-01-12 13:33:19 +01:00
* @param row (1 indexed)
2018-07-18 22:31:05 +02:00
* @return
*/
public TInlineKeyboardMarkup addUrlButton(String title, String url, int row) {
2021-01-12 13:33:19 +01:00
addButton(row, new JSONObject().put("text", title).put("url", url));
2018-07-18 04:44:04 +02:00
return this;
}
2021-01-12 13:33:19 +01:00
/**
* insert button in nth line
* @param title
* @param callback
* @param row (1 indexed)
* @return
*/
2019-02-06 23:52:15 +01:00
public TInlineKeyboardMarkup addCallbackButton(String title, String callback, int row) {
2021-01-12 13:33:19 +01:00
addButton(row, new JSONObject().put("text", title).put("callback_data", callback));
2019-02-06 23:52:15 +01:00
return this;
}
2021-01-12 13:33:19 +01:00
/**
* Add a button, check if the row is available first
* @param row
* @param btn
*/
private void addButton(int row, JSONObject btn) {
--row;
if(arr_btn.size() < row) {
//row existiert noch nicht
row = arr_btn.size(); //auf nächst größte Zeile setzten, damit keine leerzeilen entstehen
//resize
arr_btn.add(row, new JSONArray());
}
JSONArray arr = arr_btn.get(row);
arr.put(btn);
}
public static TInlineKeyboardMarkup makeYesNo(String yes, String no) {
return makeYesNo(yes, no, "");
}
public static TInlineKeyboardMarkup makeYesNo(String yes, String no, String dataprefix) {
if(dataprefix == null) dataprefix = "";
TInlineKeyboardMarkup kb = new TInlineKeyboardMarkup(1);
kb.addCallbackButton(yes, dataprefix + "yes", 1);
kb.addCallbackButton(no, dataprefix + "no", 1);
return kb;
}
2020-11-21 20:11:45 +01:00
public static TInlineKeyboardMarkup makeSingleButton(String btn, String callback) {
TInlineKeyboardMarkup kb = new TInlineKeyboardMarkup(1);
kb.addCallbackButton(btn, callback, 1);
return kb;
}
2019-02-06 23:52:15 +01:00
2018-07-18 04:44:04 +02:00
@Override
public String toJSONString() {
2021-01-12 13:33:19 +01:00
arr_btn.removeIf(a -> a.isEmpty()); //remove empty rows
2018-07-18 22:31:05 +02:00
return new JSONObject().put("inline_keyboard", new JSONArray(arr_btn)).toString();
2018-07-18 04:44:04 +02:00
}
}