MessageBuilder improved
This commit is contained in:
parent
da33d941bb
commit
eaccc1d618
|
@ -3,6 +3,8 @@ 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 {
|
||||
|
@ -14,6 +16,11 @@ public class MessageBuilder {
|
|||
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 Attachment attachmenttype = Attachment.none;
|
||||
private String attachment = null;
|
||||
private String caption = null;
|
||||
|
||||
public MessageBuilder setReciver(int id) {
|
||||
reciver_id = id;
|
||||
|
@ -53,17 +60,52 @@ public class MessageBuilder {
|
|||
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 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");
|
||||
}
|
||||
if(text != null) {
|
||||
if(text.trim().isEmpty()) {
|
||||
throw new MissingException("Text");
|
||||
}
|
||||
} else {
|
||||
throw new MissingException("Text");
|
||||
}
|
||||
String optionals = "";
|
||||
if(format != Formatting.None) {
|
||||
optionals += "&parse_mode=" + format.getname().toLowerCase();
|
||||
|
@ -72,21 +114,62 @@ public class MessageBuilder {
|
|||
optionals += "&reply_to_message_id=" + reply_to_message_id;
|
||||
}
|
||||
if(markup != null) {
|
||||
optionals += "&reply_markup=" + markup.toJSONString();
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
return new SendableMessage("chat_id=" + reciver_id + "&text=" + text.trim() + optionals + "&disable_web_page_preview=" + no_web_view + "&disable_notification=" + silent);
|
||||
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);
|
||||
return new SendableMessage(cmd, q);
|
||||
}
|
||||
|
||||
class SendableMessage {
|
||||
private String q;
|
||||
private String command;
|
||||
|
||||
public SendableMessage(String q) {
|
||||
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;
|
||||
|
||||
public AsyncSendable(String cmd, String q, Callback<TMessage, ?> clb) {
|
||||
super(cmd, q);
|
||||
callback = clb;
|
||||
}
|
||||
}
|
||||
|
||||
public enum Formatting {
|
||||
|
@ -105,6 +188,12 @@ public class MessageBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
public enum Attachment {
|
||||
none,
|
||||
Photo,
|
||||
Animation;
|
||||
}
|
||||
|
||||
public class MissingException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 2750912631502103642L;
|
||||
|
|
|
@ -13,6 +13,7 @@ import org.json.JSONObject;
|
|||
|
||||
import de.mrbesen.telegram.AsyncHandler.Callback;
|
||||
import de.mrbesen.telegram.AsyncHandler.Task;
|
||||
import de.mrbesen.telegram.MessageBuilder.AsyncSendable;
|
||||
import de.mrbesen.telegram.MessageBuilder.SendableMessage;
|
||||
import de.mrbesen.telegram.commands.CommandManager;
|
||||
import de.mrbesen.telegram.event.Event;
|
||||
|
@ -33,7 +34,7 @@ public class TelegramAPI implements Runnable {
|
|||
|
||||
private static final String API_URL = "https://api.telegram.org/bot";
|
||||
private static final String TOKENREGEX = "^\\d{4,9}:[\\w-]{12,64}$";
|
||||
public static final String APIVERSION = "3.6";//February 13, 2018
|
||||
public static final String APIVERSION = "3.7";//February 7, 2019
|
||||
|
||||
private int msg_offset = 0;
|
||||
private int updateInterval = 1500;
|
||||
|
@ -133,13 +134,22 @@ public class TelegramAPI implements Runnable {
|
|||
|
||||
public TMessage sendMessage(SendableMessage msg) {
|
||||
try {
|
||||
JSONObject o = request("sendMessage", msg.getQ());
|
||||
return new TMessage(o.getJSONObject("result"), this);
|
||||
if(msg instanceof AsyncSendable) {
|
||||
Callback<JSONObject, TMessage> adapter = getCallbackAdapter(this);
|
||||
adapter.next = ((AsyncSendable) msg).callback;
|
||||
Task t = new Task(msg.getCommand(), msg.getQ(), adapter);
|
||||
t.setExceptionhandl(IOE400supressor);
|
||||
async.enque(t);
|
||||
} else {
|
||||
JSONObject o = request(msg.getCommand(), msg.getQ());
|
||||
return new TMessage(o.getJSONObject("result"), this);
|
||||
}
|
||||
} catch(IOException e) {
|
||||
log.log("",e);
|
||||
log.log("", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void sendTypedMessage(final String msg, final TUser user, final int seconds) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
package de.mrbesen.telegram.objects;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.mrbesen.telegram.AsyncHandler.Callback;
|
||||
import de.mrbesen.telegram.AsyncHandler.Task;
|
||||
import de.mrbesen.telegram.MessageBuilder;
|
||||
import de.mrbesen.telegram.MessageBuilder.Attachment;
|
||||
import de.mrbesen.telegram.TelegramAPI;
|
||||
|
||||
public class TUser {
|
||||
|
@ -18,7 +16,7 @@ public class TUser {
|
|||
private String lastname;//optional
|
||||
private String langcode; // optional
|
||||
private boolean isBot = false;
|
||||
|
||||
|
||||
private TelegramAPI api = null;
|
||||
|
||||
TUser(int chatid, String uname, TelegramAPI api) {
|
||||
|
@ -31,7 +29,7 @@ public class TUser {
|
|||
this.api = api;
|
||||
this.id = chatid;
|
||||
}
|
||||
|
||||
|
||||
public TUser(JSONObject o, TelegramAPI api) {
|
||||
this.api = api;
|
||||
firstname = o.getString("first_name");
|
||||
|
@ -72,19 +70,19 @@ public class TUser {
|
|||
public void sendHelpPage() {
|
||||
sendMessage(api.getHelpMessage());
|
||||
}
|
||||
|
||||
|
||||
public boolean sendMessage(String text) {
|
||||
return sendMessage(text, null);
|
||||
}
|
||||
|
||||
|
||||
public boolean sendMessage(String text, TReplyMarkup rm) {
|
||||
return sendMessage(text, rm, -1);
|
||||
}
|
||||
|
||||
|
||||
public boolean sendMessage(String text, TReplyMarkup rm, int reply_to_msg) {
|
||||
return sendMessage(api, id, text, rm, reply_to_msg, false, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param api
|
||||
|
@ -99,27 +97,10 @@ public class TUser {
|
|||
System.err.println("api == null!");
|
||||
return false;
|
||||
}
|
||||
String replyto = (reply_to_msg > 0 ? "&reply_to_message_id=" +reply_to_msg : "");
|
||||
try {
|
||||
String replymarkup = parseRPMU(rm);
|
||||
String param = "chat_id=" + userid
|
||||
+ "&text=" + URLEncoder.encode(text, "UTF-8") + replymarkup + replyto;
|
||||
if(async)
|
||||
return api.request("sendMessage", param).getBoolean("ok");
|
||||
|
||||
Callback< JSONObject, TMessage> adapter = TelegramAPI.getCallbackAdapter(api);
|
||||
adapter.next = callb;
|
||||
api.request(new Task("sendMessage", param, adapter).setExceptionhandl(TelegramAPI.IOE400supressor));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
MessageBuilder msgb = new MessageBuilder().setReciver(userid).setReplyTo(reply_to_msg).setText(text).setCallback(callb).setMarkup(rm).setAsync(async);
|
||||
api.sendMessage(msgb.build());
|
||||
return true;
|
||||
}
|
||||
|
||||
private static String parseRPMU(TReplyMarkup rply) throws IOException {
|
||||
return (rply == null ? "" : "&reply_markup=" + URLEncoder.encode(rply.toJSONString(), "UTF-8"));
|
||||
}
|
||||
|
||||
|
||||
public void sendStatus(Status status) {
|
||||
try {
|
||||
|
@ -132,52 +113,21 @@ public class TUser {
|
|||
public boolean sendImage(String caption, String url) {
|
||||
return sendImage(api, id, caption, url, null, null) != null;
|
||||
}
|
||||
|
||||
|
||||
public static TMessage sendImage(TelegramAPI api, int userid, String caption, String url, TReplyMarkup rply, Callback<TMessage, ?> async) {
|
||||
try {
|
||||
String cap = "";
|
||||
if(caption != null) {
|
||||
if(!caption.isEmpty())
|
||||
cap = "&caption=" + caption;
|
||||
}
|
||||
|
||||
String params = "chat_id=" + userid + parseRPMU(rply) + cap + "&photo=" + url;
|
||||
if(async == null)
|
||||
return new TMessage(api.request("sendPhoto", params).getJSONObject("result"), api);
|
||||
|
||||
Callback< JSONObject, TMessage> adapter = TelegramAPI.getCallbackAdapter(api);
|
||||
adapter.next = async;
|
||||
api.request(new Task("sendPhoto", params, adapter).setExceptionhandl(TelegramAPI.IOE400supressor));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
MessageBuilder msgb = new MessageBuilder().setReciver(userid).setCaption(caption).setAttachment(Attachment.Photo, url).setMarkup(rply).setCallback(async).setAsync(async != null);
|
||||
return api.sendMessage(msgb.build());
|
||||
}
|
||||
|
||||
|
||||
public boolean sendAnimation(String caption, String url) {
|
||||
return sendAnimation(api, id, caption, url, null, null) != null;
|
||||
}
|
||||
|
||||
|
||||
public static TMessage sendAnimation(TelegramAPI api, int userid, String caption, String url, TReplyMarkup rply, Callback<TMessage, ?> async) {
|
||||
try {
|
||||
String cap = "";
|
||||
if(caption != null) {
|
||||
if(!caption.isEmpty())
|
||||
cap = "&caption=" + caption;
|
||||
}
|
||||
String params = "chat_id=" + userid + parseRPMU(rply) + cap + "&animation=" + url;
|
||||
if(async == null)
|
||||
return new TMessage(api.request("sendAnimation", params).getJSONObject("result"), api);
|
||||
|
||||
Callback< JSONObject, TMessage> adapter = TelegramAPI.getCallbackAdapter(api);
|
||||
adapter.next = async;
|
||||
api.request(new Task("sendAnimation", params, adapter).setExceptionhandl(TelegramAPI.IOE400supressor));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
MessageBuilder msgb = new MessageBuilder().setReciver(userid).setCaption(caption).setAttachment(Attachment.Animation, url).setMarkup(rply).setCallback(async).setAsync(async != null);
|
||||
return api.sendMessage(msgb.build());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object user) {
|
||||
if(user instanceof TUser) {
|
||||
|
@ -185,7 +135,7 @@ public class TUser {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public enum Status {
|
||||
Typing,
|
||||
Upload_Photo,
|
||||
|
@ -197,7 +147,7 @@ public class TUser {
|
|||
Find_Location,
|
||||
Record_Video_Note,
|
||||
Upload_Video_Note;
|
||||
|
||||
|
||||
String getAPIName() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue