Created
March 6, 2019 13:50
-
-
Save xISRAPILx/c38e3c1cd98f35546d8fcd2fe16bed86 to your computer and use it in GitHub Desktop.
Класс для генерации клавиатуры бота ВКонтакте. Пример того, как не нужно делать.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Пример того, как делать не нужно. | |
Работает и пофиг! | |
*/ | |
import com.google.gson.Gson; | |
import com.google.gson.JsonObject; | |
import java.util.ArrayList; | |
public class Keyboard{ | |
private boolean one_time; | |
private ArrayList<ArrayList<Button>> buttons = new ArrayList<>(); | |
private int row = 0; | |
public Keyboard(boolean one_time){ | |
this.one_time = one_time; | |
this.buttons.add(new ArrayList<>()); | |
} | |
public Keyboard addButton(Button button){ | |
return this.addButton(button, false); | |
} | |
public Keyboard addButton(Button button, boolean new_line){ | |
if(new_line){ | |
this.row += 1; | |
this.buttons.add(new ArrayList<>()); | |
} | |
this.buttons.get(this.row).add(button); | |
return this; | |
} | |
@Override | |
public String toString(){ | |
Gson gson = new Gson(); | |
JsonObject json = new JsonObject(); | |
json.addProperty("one_time", this.one_time); | |
json.add("buttons", gson.toJsonTree(buttons)); | |
return json.toString(); | |
} | |
public static class Button{ | |
public final static String COLOR_PRIMARY = "primary"; | |
public final static String COLOR_DEFAULT = "default"; | |
public final static String COLOR_NEGATIVE = "negative"; | |
public final static String COLOR_POSITIVE = "positive"; | |
private Action action; | |
private String color; | |
public Button(Action action, String color){ | |
this.action = action; | |
this.color = color; | |
} | |
public Action getAction(){ | |
return action; | |
} | |
public String getColor(){ | |
return color; | |
} | |
public static class Action{ | |
private String type = "text"; | |
private String payload; | |
private String label; | |
public Action(Payload payload, String label){ | |
this.payload = payload.toString(); | |
this.label = label; | |
} | |
public String getType(){ | |
return type; | |
} | |
public String getPayload(){ | |
return payload; | |
} | |
public String getLabel(){ | |
return label; | |
} | |
public static class Payload{ | |
private String command; | |
public Payload(String command){ | |
this.command = command; | |
} | |
public String getCommand(){ | |
return command; | |
} | |
@Override | |
public String toString(){ | |
JsonObject json = new JsonObject(); | |
json.addProperty("command", this.command); | |
return json.toString(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment