2018-07-18 22:31:05 +02:00
|
|
|
package de.mrbesen.telegram;
|
|
|
|
|
2018-10-11 14:49:11 +02:00
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
2019-02-07 18:35:42 +01:00
|
|
|
import de.mrbesen.telegram.AsyncHandler.Callback;
|
|
|
|
import de.mrbesen.telegram.objects.TMessage;
|
2018-07-18 22:31:05 +02:00
|
|
|
import de.mrbesen.telegram.objects.TReplyMarkup;
|
2019-03-01 04:49:20 +01:00
|
|
|
import de.mrbesen.telegram.objects.TUser;
|
2018-07-18 22:31:05 +02:00
|
|
|
|
|
|
|
public class MessageBuilder {
|
|
|
|
|
|
|
|
private String text;
|
|
|
|
private Formatting format = Formatting.None;
|
|
|
|
private boolean silent = false;
|
|
|
|
private boolean no_web_view = false;
|
2019-04-13 01:58:28 +02:00
|
|
|
private long reciver_id = 0;
|
2018-07-18 22:31:05 +02:00
|
|
|
private int reply_to_message_id = 0;
|
|
|
|
private TReplyMarkup markup = null;
|
2019-02-07 18:35:42 +01:00
|
|
|
private boolean async = false;
|
2019-03-19 02:42:33 +01:00
|
|
|
private boolean asyncprio = false;
|
2019-02-07 18:35:42 +01:00
|
|
|
private Callback<TMessage, ?> callback = null;
|
2019-02-08 03:15:31 +01:00
|
|
|
private Callback<Throwable, Void> excpt = null;
|
2019-02-07 18:35:42 +01:00
|
|
|
private Attachment attachmenttype = Attachment.none;
|
|
|
|
private String attachment = null;
|
|
|
|
private String caption = null;
|
2019-02-25 01:49:24 +01:00
|
|
|
private int updates = 0;
|
2018-07-18 22:31:05 +02:00
|
|
|
|
2019-04-13 01:58:28 +02:00
|
|
|
public MessageBuilder setReciver(long id) {
|
2018-07-18 22:31:05 +02:00
|
|
|
reciver_id = id;
|
|
|
|
return this;
|
|
|
|
}
|
2019-03-01 04:49:20 +01:00
|
|
|
|
|
|
|
public MessageBuilder setReciver(TUser user) {
|
|
|
|
reciver_id = user.getID();
|
|
|
|
return this;
|
|
|
|
}
|
2018-07-18 22:31:05 +02:00
|
|
|
|
|
|
|
public MessageBuilder setText(String txt) {
|
|
|
|
txt = txt.trim();
|
|
|
|
if(txt.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("text is not allowed to be empty");
|
|
|
|
}
|
2018-10-11 14:49:11 +02:00
|
|
|
try {
|
|
|
|
text = URLEncoder.encode(txt, "UTF-8");
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-07-18 22:31:05 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-07 18:35:42 +01:00
|
|
|
public MessageBuilder setReplyTo(TMessage msg) {
|
2019-04-30 12:03:09 +02:00
|
|
|
if(reciver_id == 0)
|
|
|
|
setReciver(msg.getChatID());
|
2019-02-07 18:35:42 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-19 02:42:33 +01:00
|
|
|
public MessageBuilder setAsyncPrio() {
|
|
|
|
async = true;
|
|
|
|
asyncprio = true;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-02-07 18:35:42 +01:00
|
|
|
public MessageBuilder setCallback(Callback<TMessage, ?> clb) {
|
|
|
|
callback = clb;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-02-08 03:15:31 +01:00
|
|
|
public MessageBuilder setExceptionHandler(Callback<Throwable, Void> exchndl) {
|
|
|
|
excpt = exchndl;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-02-07 18:35:42 +01:00
|
|
|
public MessageBuilder setAttachment(Attachment type, String cont) {
|
2020-06-14 18:04:50 +02:00
|
|
|
if(cont == null) {
|
|
|
|
if(type != Attachment.none) throw new IllegalArgumentException("cont == null requires type == none");
|
|
|
|
attachmenttype = type;
|
|
|
|
attachment = null;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-02-07 18:35:42 +01:00
|
|
|
if(attachmenttype != Attachment.none) {
|
|
|
|
throw new IllegalArgumentException("You can only attach one thing!");
|
|
|
|
}
|
2020-06-14 18:04:50 +02:00
|
|
|
|
2019-02-07 18:35:42 +01:00
|
|
|
attachmenttype = type;
|
2019-02-11 16:38:05 +01:00
|
|
|
try {
|
2020-06-14 18:04:50 +02:00
|
|
|
attachment = URLEncoder.encode(cont, "UTF-8");
|
2019-02-11 16:38:05 +01:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2019-02-07 18:35:42 +01:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public MessageBuilder setCaption(String text) {
|
2019-02-11 16:38:05 +01:00
|
|
|
try {
|
|
|
|
this.caption = URLEncoder.encode(text, "UTF-8");
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2019-02-07 18:35:42 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-02-25 01:49:24 +01:00
|
|
|
public MessageBuilder setUpdates(int oldmsgid) {
|
|
|
|
updates = oldmsgid;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-07-18 22:31:05 +02:00
|
|
|
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) {
|
2019-02-07 18:35:42 +01:00
|
|
|
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) {
|
|
|
|
if(caption != null)
|
2019-02-11 16:46:27 +01:00
|
|
|
attachment = "&caption=" + caption;
|
|
|
|
attachment += "&" + attachmenttype.name().toLowerCase() + "=" + this.attachment;
|
2019-02-11 16:38:05 +01:00
|
|
|
cmd = "send" + attachmenttype.name();
|
2019-02-07 18:35:42 +01:00
|
|
|
} else {
|
2019-02-25 01:49:24 +01:00
|
|
|
if(updates > 0) {
|
|
|
|
cmd = "editMessageText";
|
|
|
|
optionals += "&message_id=" + updates;
|
|
|
|
} else
|
|
|
|
cmd = "sendMessage";
|
2019-02-07 18:35:42 +01:00
|
|
|
if(this.text != null) {
|
|
|
|
if(this.text.trim().isEmpty()) {
|
|
|
|
throw new MissingException("Text");
|
|
|
|
}
|
|
|
|
text = "&text=" + this.text.trim();
|
|
|
|
} else {
|
|
|
|
throw new MissingException("Text");
|
|
|
|
}
|
2018-07-18 22:31:05 +02:00
|
|
|
}
|
2019-03-29 17:59:23 +01:00
|
|
|
if(no_web_view) {
|
|
|
|
optionals += "&disable_web_page_preview=" + no_web_view;
|
|
|
|
}
|
|
|
|
if(silent) {
|
|
|
|
optionals += "&disable_notification=" + silent;
|
|
|
|
}
|
2018-07-18 22:31:05 +02:00
|
|
|
|
2019-03-29 17:59:23 +01:00
|
|
|
String q = "chat_id=" + reciver_id + text + optionals + attachment;
|
2019-03-19 02:42:33 +01:00
|
|
|
if(async) {
|
|
|
|
AsyncSendable tmp = new AsyncSendable(cmd, q, callback, excpt);
|
|
|
|
tmp.prio = asyncprio;
|
|
|
|
return tmp;
|
|
|
|
}
|
2019-02-07 18:35:42 +01:00
|
|
|
return new SendableMessage(cmd, q);
|
2018-07-18 22:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class SendableMessage {
|
|
|
|
private String q;
|
2019-02-07 18:35:42 +01:00
|
|
|
private String command;
|
2018-07-18 22:31:05 +02:00
|
|
|
|
2019-02-07 18:35:42 +01:00
|
|
|
public SendableMessage(String cmd, String q) {
|
2018-07-18 22:31:05 +02:00
|
|
|
this.q = q;
|
2019-02-07 18:35:42 +01:00
|
|
|
command = cmd;
|
2018-07-18 22:31:05 +02:00
|
|
|
}
|
|
|
|
String getQ() {
|
|
|
|
return q;
|
|
|
|
}
|
2019-02-07 18:35:42 +01:00
|
|
|
String getCommand() {
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AsyncSendable extends SendableMessage {
|
|
|
|
|
|
|
|
Callback<TMessage, ?> callback;
|
2019-02-08 03:15:31 +01:00
|
|
|
Callback<Throwable, Void> excpt = null;
|
2019-03-19 02:42:33 +01:00
|
|
|
boolean prio = false;
|
2019-02-07 18:35:42 +01:00
|
|
|
|
2019-02-08 03:15:31 +01:00
|
|
|
public AsyncSendable(String cmd, String q, Callback<TMessage, ?> clb, Callback<Throwable, Void> excpt) {
|
2019-02-07 18:35:42 +01:00
|
|
|
super(cmd, q);
|
|
|
|
callback = clb;
|
2019-02-08 03:15:31 +01:00
|
|
|
this.excpt = excpt;
|
2019-02-07 18:35:42 +01:00
|
|
|
}
|
2018-07-18 22:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public enum Formatting {
|
|
|
|
None(""),
|
|
|
|
HTML("HTML"),
|
|
|
|
MD("Markdown");
|
|
|
|
|
|
|
|
private String apiname;
|
|
|
|
|
|
|
|
private Formatting(String s) {
|
|
|
|
apiname = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getname() {
|
|
|
|
return apiname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 18:35:42 +01:00
|
|
|
public enum Attachment {
|
|
|
|
none,
|
2019-03-29 17:59:23 +01:00
|
|
|
Video,//video need to be less than 50MB and mp4!
|
|
|
|
Document,
|
|
|
|
Audio,
|
|
|
|
Voice,
|
2019-02-07 18:35:42 +01:00
|
|
|
Photo,
|
|
|
|
Animation;
|
2019-03-29 17:59:23 +01:00
|
|
|
|
|
|
|
public static Attachment getForFileExt(String extention) {
|
|
|
|
if(extention.startsWith("."))
|
|
|
|
extention = extention.substring(1);
|
2019-04-11 12:31:07 +02:00
|
|
|
|
|
|
|
extention = extention.toLowerCase();
|
|
|
|
|
|
|
|
if(extention.equals("mp4")) {
|
2019-03-29 17:59:23 +01:00
|
|
|
return Attachment.Video;
|
2019-04-11 12:31:07 +02:00
|
|
|
} else if(extention.equals("mp3")) {
|
2019-03-29 17:59:23 +01:00
|
|
|
return Attachment.Audio;
|
2019-04-11 12:31:07 +02:00
|
|
|
} else if(extention.equals("ogg")) {
|
2019-03-29 17:59:23 +01:00
|
|
|
return Attachment.Voice;
|
2019-04-11 12:31:07 +02:00
|
|
|
} else if(extention.equals("jpg") || extention.equals("jpeg") || extention.equals("png")) {
|
2019-04-08 03:35:34 +02:00
|
|
|
return Attachment.Photo;
|
2019-04-11 12:31:07 +02:00
|
|
|
} else if(extention.equals("gif")) {
|
|
|
|
return Attachment.Animation;
|
2019-03-29 17:59:23 +01:00
|
|
|
}
|
2019-04-11 12:31:07 +02:00
|
|
|
|
2019-03-29 17:59:23 +01:00
|
|
|
return Attachment.Document;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Attachment getForFileName(String name) {
|
|
|
|
return getForFileExt(name.substring(name.lastIndexOf('.')+1));
|
|
|
|
}
|
2019-02-07 18:35:42 +01:00
|
|
|
}
|
|
|
|
|
2018-07-18 22:31:05 +02:00
|
|
|
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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|