215 lines
4.8 KiB
Java
215 lines
4.8 KiB
Java
package de.mrbesen.telegram;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.net.URLEncoder;
|
|
|
|
import de.mrbesen.telegram.AsyncHandler.Callback;
|
|
import de.mrbesen.telegram.objects.TMessage;
|
|
import de.mrbesen.telegram.objects.TReplyMarkup;
|
|
|
|
public class MessageBuilder {
|
|
|
|
private String text;
|
|
private Formatting format = Formatting.None;
|
|
private boolean silent = false;
|
|
private boolean no_web_view = false;
|
|
private int reciver_id = 0;
|
|
private int reply_to_message_id = 0;
|
|
private TReplyMarkup markup = null;
|
|
private boolean async = false;
|
|
private Callback<TMessage, ?> callback = null;
|
|
private Callback<Throwable, Void> excpt = null;
|
|
private Attachment attachmenttype = Attachment.none;
|
|
private String attachment = null;
|
|
private String caption = null;
|
|
|
|
public MessageBuilder setReciver(int id) {
|
|
reciver_id = id;
|
|
return this;
|
|
}
|
|
|
|
public MessageBuilder setText(String txt) {
|
|
txt = txt.trim();
|
|
if(txt.isEmpty()) {
|
|
throw new IllegalArgumentException("text is not allowed to be empty");
|
|
}
|
|
try {
|
|
text = URLEncoder.encode(txt, "UTF-8");
|
|
} catch (UnsupportedEncodingException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public MessageBuilder setFormatting(Formatting form) {
|
|
format = form;
|
|
return this;
|
|
}
|
|
|
|
public MessageBuilder setSilent(boolean b) {
|
|
silent = b;
|
|
return this;
|
|
}
|
|
|
|
public MessageBuilder setNoWebView(boolean b) {
|
|
no_web_view = b;
|
|
return this;
|
|
}
|
|
|
|
public MessageBuilder setMarkup(TReplyMarkup markup) {
|
|
this.markup = markup;
|
|
return this;
|
|
}
|
|
|
|
public MessageBuilder setReplyTo(TMessage msg) {
|
|
return setReplyTo(msg.getMessageID());
|
|
}
|
|
|
|
public MessageBuilder setReplyTo(int msgid) {
|
|
reply_to_message_id = msgid;
|
|
return this;
|
|
}
|
|
|
|
public MessageBuilder setAsync() {
|
|
async = true;
|
|
return this;
|
|
}
|
|
|
|
public MessageBuilder setAsync(boolean b) {
|
|
async = b;
|
|
return this;
|
|
}
|
|
|
|
public MessageBuilder setCallback(Callback<TMessage, ?> clb) {
|
|
callback = clb;
|
|
return this;
|
|
}
|
|
|
|
public MessageBuilder setExceptionHandler(Callback<Throwable, Void> exchndl) {
|
|
excpt = exchndl;
|
|
return this;
|
|
}
|
|
|
|
public MessageBuilder setAttachment(Attachment type, String cont) {
|
|
if(attachmenttype != Attachment.none) {
|
|
throw new IllegalArgumentException("You can only attach one thing!");
|
|
}
|
|
if(cont == null) {
|
|
throw new IllegalArgumentException("attachment empty!");
|
|
}
|
|
attachmenttype = type;
|
|
attachment = cont;
|
|
|
|
return this;
|
|
}
|
|
|
|
public MessageBuilder setCaption(String text) {
|
|
this.caption = text;
|
|
return this;
|
|
}
|
|
|
|
public SendableMessage build() {
|
|
if(reciver_id == 0) {
|
|
throw new MissingException("Reciver");
|
|
}
|
|
String optionals = "";
|
|
if(format != Formatting.None) {
|
|
optionals += "&parse_mode=" + format.getname().toLowerCase();
|
|
}
|
|
if(reply_to_message_id != 0) {
|
|
optionals += "&reply_to_message_id=" + reply_to_message_id;
|
|
}
|
|
if(markup != null) {
|
|
try {
|
|
optionals += "&reply_markup=" + URLEncoder.encode(markup.toJSONString(), "UTF-8");
|
|
} catch (UnsupportedEncodingException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
String attachment = "";
|
|
String cmd;
|
|
String text = "";
|
|
if(attachmenttype != Attachment.none) {
|
|
attachment = "&" + attachmenttype.name().toLowerCase() + "=" + this.attachment;
|
|
cmd = "send" + attachmenttype.name();
|
|
if(caption != null)
|
|
attachment += "&caption=" + caption;
|
|
} else {
|
|
cmd = "sendMessage";
|
|
if(this.text != null) {
|
|
if(this.text.trim().isEmpty()) {
|
|
throw new MissingException("Text");
|
|
}
|
|
text = "&text=" + this.text.trim();
|
|
} else {
|
|
throw new MissingException("Text");
|
|
}
|
|
}
|
|
|
|
String q = "chat_id=" + reciver_id + text + optionals + "&disable_web_page_preview=" + no_web_view + "&disable_notification=" + silent + attachment;
|
|
if(async)
|
|
return new AsyncSendable(cmd, q, callback, excpt);
|
|
return new SendableMessage(cmd, q);
|
|
}
|
|
|
|
class SendableMessage {
|
|
private String q;
|
|
private String command;
|
|
|
|
public SendableMessage(String cmd, String q) {
|
|
this.q = q;
|
|
command = cmd;
|
|
}
|
|
String getQ() {
|
|
return q;
|
|
}
|
|
String getCommand() {
|
|
return command;
|
|
}
|
|
}
|
|
|
|
class AsyncSendable extends SendableMessage {
|
|
|
|
Callback<TMessage, ?> callback;
|
|
Callback<Throwable, Void> excpt = null;
|
|
|
|
public AsyncSendable(String cmd, String q, Callback<TMessage, ?> clb, Callback<Throwable, Void> excpt) {
|
|
super(cmd, q);
|
|
callback = clb;
|
|
this.excpt = excpt;
|
|
}
|
|
}
|
|
|
|
public enum Formatting {
|
|
None(""),
|
|
HTML("HTML"),
|
|
MD("Markdown");
|
|
|
|
private String apiname;
|
|
|
|
private Formatting(String s) {
|
|
apiname = s;
|
|
}
|
|
|
|
public String getname() {
|
|
return apiname;
|
|
}
|
|
}
|
|
|
|
public enum Attachment {
|
|
none,
|
|
Photo,
|
|
Animation;
|
|
}
|
|
|
|
public class MissingException extends RuntimeException {
|
|
|
|
private static final long serialVersionUID = 2750912631502103642L;
|
|
|
|
public MissingException(String missing_obj) {
|
|
super("The Object " + missing_obj + " is missing or invalid.");
|
|
}
|
|
}
|
|
|
|
}
|