TelegramAPI/src/main/java/de/mrbesen/telegram/MessageBuilder.java

318 lines
7.6 KiB
Java
Raw Normal View History

2018-07-18 22:31:05 +02:00
package de.mrbesen.telegram;
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;
2021-01-19 21:19:53 +01:00
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
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;
private boolean allow_sending_without_reply = false;
2022-07-13 19:25:03 +02:00
private long receiver_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;
2020-11-26 21:24:47 +01:00
private TelegramAPI api = null;
public MessageBuilder(TelegramAPI api) {
this.api = api;
}
public MessageBuilder() { }
2019-04-13 01:58:28 +02:00
public MessageBuilder setReciver(long id) {
2022-07-13 19:25:03 +02:00
receiver_id = id;
2018-07-18 22:31:05 +02:00
return this;
}
2019-03-01 04:49:20 +01:00
public MessageBuilder setReciver(TUser user) {
2022-07-13 19:25:03 +02:00
receiver_id = user.getID();
2019-03-01 04:49:20 +01:00
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) {
2022-07-13 19:25:03 +02:00
if(receiver_id == 0)
2019-04-30 12:03:09 +02:00
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;
}
/**
* should fail when a message, thats replyed to is not existent default is true
* @param b
* @return this
*/
public MessageBuilder setFailwithoutReply(boolean b) {
allow_sending_without_reply = !b;
return this;
}
2018-07-18 22:31:05 +02:00
public SendableMessage build() {
2022-07-13 19:25:03 +02:00
if(receiver_id == 0) {
2018-07-18 22:31:05 +02:00
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=true";
2019-03-29 17:59:23 +01:00
}
if(silent) {
optionals += "&disable_notification=true";
}
if(allow_sending_without_reply) {
optionals += "&allow_sending_without_reply=true";
2019-03-29 17:59:23 +01:00
}
2018-07-18 22:31:05 +02:00
2022-07-13 19:25:03 +02:00
String q = "chat_id=" + receiver_id + text + optionals + attachment;
2019-03-19 02:42:33 +01:00
if(async) {
2022-07-13 19:25:03 +02:00
AsyncSendable tmp = new AsyncSendable(cmd, q, receiver_id, callback, excpt);
2019-03-19 02:42:33 +01:00
tmp.prio = asyncprio;
2022-07-13 19:25:03 +02:00
tmp.isSlow = attachmenttype != Attachment.none;
2019-03-19 02:42:33 +01:00
return tmp;
}
2022-07-13 19:25:03 +02:00
return new SendableMessage(cmd, q, receiver_id);
2018-07-18 22:31:05 +02:00
}
2020-11-26 21:24:47 +01:00
/**
* is only allowed, when the Message builder was contructed using a api
* @return the send TMessage when the Message Builder is used SYNC, or null when the Message Builder is used ASYNC or has encountered an error.
*/
public TMessage send() {
return api.sendMessage(build());
}
2021-01-19 21:19:53 +01:00
@Getter
@AllArgsConstructor
2018-07-18 22:31:05 +02:00
class SendableMessage {
2021-01-19 21:19:53 +01:00
private final String command;
private final String q;
2021-01-19 21:19:53 +01:00
private final long userid;
2019-02-07 18:35:42 +01:00
}
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;
2022-07-13 19:25:03 +02:00
boolean isSlow = false; // is it expected that this message is slow to porcess? sendMedia
2019-02-07 18:35:42 +01:00
2021-01-19 21:19:53 +01:00
public AsyncSendable(String cmd, String q, long userid, Callback<TMessage, ?> clb, Callback<Throwable, Void> excpt) {
super(cmd, q, userid);
2019-02-07 18:35:42 +01:00
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) {
2021-01-18 21:42:31 +01:00
if(extention == null) return Document;
2019-03-29 17:59:23 +01:00
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.");
}
}
}