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; import de.mrbesen.telegram.objects.TUser; public class MessageBuilder { private String text; private Formatting format = Formatting.None; private boolean silent = false; private boolean no_web_view = false; private long reciver_id = 0; private int reply_to_message_id = 0; private TReplyMarkup markup = null; private boolean async = false; private boolean asyncprio = false; private Callback callback = null; private Callback excpt = null; private Attachment attachmenttype = Attachment.none; private String attachment = null; private String caption = null; private int updates = 0; public MessageBuilder setReciver(long id) { reciver_id = id; return this; } public MessageBuilder setReciver(TUser user) { reciver_id = user.getID(); 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 setAsyncPrio() { async = true; asyncprio = true; return this; } public MessageBuilder setCallback(Callback clb) { callback = clb; return this; } public MessageBuilder setExceptionHandler(Callback 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; try { attachment = URLEncoder.encode(cont, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return this; } public MessageBuilder setCaption(String text) { try { this.caption = URLEncoder.encode(text, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return this; } public MessageBuilder setUpdates(int oldmsgid) { updates = oldmsgid; 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) { if(caption != null) attachment = "&caption=" + caption; attachment += "&" + attachmenttype.name().toLowerCase() + "=" + this.attachment; cmd = "send" + attachmenttype.name(); } else { if(updates > 0) { cmd = "editMessageText"; optionals += "&message_id=" + updates; } 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"); } } if(no_web_view) { optionals += "&disable_web_page_preview=" + no_web_view; } if(silent) { optionals += "&disable_notification=" + silent; } String q = "chat_id=" + reciver_id + text + optionals + attachment; if(async) { AsyncSendable tmp = new AsyncSendable(cmd, q, callback, excpt); tmp.prio = asyncprio; return tmp; } 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 callback; Callback excpt = null; boolean prio = false; public AsyncSendable(String cmd, String q, Callback clb, Callback 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, Video,//video need to be less than 50MB and mp4! Document, Audio, Voice, Photo, Animation; public static Attachment getForFileExt(String extention) { if(extention.startsWith(".")) extention = extention.substring(1); if(extention.equalsIgnoreCase("mp4")) { return Attachment.Video; } else if(extention.equalsIgnoreCase("mp3")) { return Attachment.Audio; } else if(extention.equalsIgnoreCase("ogg")) { return Attachment.Voice; } return Attachment.Document; } public static Attachment getForFileName(String name) { return getForFileExt(name.substring(name.lastIndexOf('.')+1)); } } 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."); } } }