added TUser.sendMessage and advanced error reporting
This commit is contained in:
parent
5147b40fdc
commit
df285ac33c
|
@ -38,11 +38,11 @@ public class TelegramAPI implements Runnable {
|
|||
private Thread thread;
|
||||
private boolean run = true;
|
||||
private String helpmessage = "generic helppage\nuse TelegramAPI.setHelpText(java.lang.String) to change this.";
|
||||
|
||||
|
||||
private LinkedList<TUser> users = new LinkedList<>();
|
||||
private CommandManager cmdmgr = new CommandManager();
|
||||
private EventManager evntmgr = new EventManager();
|
||||
//Log log = new SimpleLog();
|
||||
|
||||
Log log = new Log4JLog();
|
||||
|
||||
public TelegramAPI(String apikey) {
|
||||
|
@ -71,18 +71,24 @@ public class TelegramAPI implements Runnable {
|
|||
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
|
||||
wr.write(parameter);
|
||||
wr.flush();
|
||||
|
||||
log.log( "request: " + request + " content " + parameter + " -> " + con.getResponseCode() + ", " + con.getResponseMessage());
|
||||
|
||||
Scanner s = new Scanner(con.getInputStream());
|
||||
StringBuilder sb_apianswer = new StringBuilder();
|
||||
while(s.hasNextLine()) {
|
||||
sb_apianswer.append(s.nextLine()).append('\n');
|
||||
}
|
||||
s.close();
|
||||
if(con.getResponseCode() == 200) {
|
||||
Scanner s = new Scanner(con.getInputStream());
|
||||
StringBuilder sb_apianswer = new StringBuilder();
|
||||
while(s.hasNextLine()) {
|
||||
sb_apianswer.append(s.nextLine()).append('\n');
|
||||
}
|
||||
s.close();
|
||||
|
||||
return new JSONObject(sb_apianswer.toString());
|
||||
} else {
|
||||
throw new IOException("API request returned HTTP " + con.getResponseCode() + " (" + con.getResponseMessage() + ") for action " + request );
|
||||
String errdesc = "";
|
||||
try {
|
||||
JSONObject returned = new JSONObject(sb_apianswer.toString());
|
||||
errdesc = returned.getString("description");
|
||||
} catch(Throwable ignore) { }
|
||||
throw new IOException("API request returned HTTP " + con.getResponseCode() + " (" + con.getResponseMessage() + ") for action " + request + "\nurl: " + url.toString() + "\nparams: " + parameter + "\nerror description: " + errdesc);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,7 +98,7 @@ public class TelegramAPI implements Runnable {
|
|||
builder.setText(text);
|
||||
return sendMessage(builder.build());
|
||||
}
|
||||
|
||||
|
||||
public TMessage sendMessage(SendableMessage msg) {
|
||||
try {
|
||||
JSONObject o = request("sendMessage", msg.getQ());
|
||||
|
@ -103,20 +109,20 @@ public class TelegramAPI implements Runnable {
|
|||
return null;
|
||||
}
|
||||
public void sendTypedMessage(final String msg, final TUser user, final int seconds) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
user.sendStatus(Status.Typing);
|
||||
try {
|
||||
Thread.sleep(seconds*1000);
|
||||
} catch (InterruptedException e) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
user.sendStatus(Status.Typing);
|
||||
try {
|
||||
Thread.sleep(seconds*1000);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
sendMessage(new MessageBuilder().setText(msg).setReciver(user.getID()).build());
|
||||
|
||||
}
|
||||
sendMessage(new MessageBuilder().setText(msg).setReciver(user.getID()).build());
|
||||
|
||||
}
|
||||
}).start();
|
||||
}).start();
|
||||
}
|
||||
|
||||
|
||||
public void stop() {
|
||||
run = false;
|
||||
thread.interrupt();
|
||||
|
@ -166,7 +172,7 @@ public class TelegramAPI implements Runnable {
|
|||
msg_offset = (upd.update_id+1 > msg_offset ? upd.update_id+1 : msg_offset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public TUser getUser(String name) {
|
||||
for(TUser us : users) {
|
||||
if(us.getName().equals(name))
|
||||
|
@ -217,7 +223,7 @@ public class TelegramAPI implements Runnable {
|
|||
else
|
||||
log = l;
|
||||
}
|
||||
|
||||
|
||||
public CommandManager getCommandManager() {
|
||||
return cmdmgr;
|
||||
}
|
||||
|
@ -231,7 +237,7 @@ public class TelegramAPI implements Runnable {
|
|||
throw new IllegalArgumentException("UpdateInterval is not allowed to be negative.");
|
||||
updateInterval = d;
|
||||
}
|
||||
|
||||
|
||||
public String getHelpMessage() {
|
||||
return helpmessage;
|
||||
}
|
||||
|
@ -250,7 +256,7 @@ public class TelegramAPI implements Runnable {
|
|||
|
||||
if(json.has("message")) {
|
||||
msg = new TMessage(json.getJSONObject("message"), api);
|
||||
|
||||
|
||||
String text = msg.getText();
|
||||
if(text != null) {
|
||||
if(text.startsWith("/" )) {
|
||||
|
@ -262,21 +268,21 @@ public class TelegramAPI implements Runnable {
|
|||
if(getMsg().has(Member.audio)) {
|
||||
e = new UserSendAudioEvent(getUser(), new TAudio((JSONObject) getMsg().get(Member.audio)));
|
||||
} else if(getMsg().has(Member.video)) {
|
||||
|
||||
|
||||
} else if(getMsg().has(Member.document)) {
|
||||
|
||||
|
||||
} else if(getMsg().has(Member.invoice)) {
|
||||
|
||||
|
||||
} else if(getMsg().has(Member.location)) {
|
||||
|
||||
|
||||
} else if(getMsg().has(Member.video_note)) {
|
||||
|
||||
|
||||
} else if(getMsg().has(Member.game)) {
|
||||
|
||||
|
||||
} else if(getMsg().has(Member.contact)) {
|
||||
|
||||
|
||||
} else if(getMsg().has(Member.sticker)) {
|
||||
|
||||
|
||||
} else {
|
||||
e = new UserSendMessageEvent(getMsg());
|
||||
}
|
||||
|
|
|
@ -80,6 +80,19 @@ public class TUser {
|
|||
}
|
||||
|
||||
public boolean sendMessage(String text, TReplyMarkup rm, int reply_to_msg) {
|
||||
return sendMessage(api, id, text, rm, reply_to_msg);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param api
|
||||
* @param userid
|
||||
* @param text
|
||||
* @param rm null = no markup
|
||||
* @param reply_to_msg 0 = no reply
|
||||
* @return
|
||||
*/
|
||||
public static boolean sendMessage(TelegramAPI api, int userid, String text, TReplyMarkup rm, int reply_to_msg) {
|
||||
if(api == null) {
|
||||
System.err.println("api == null!");
|
||||
return false;
|
||||
|
@ -87,7 +100,7 @@ public class TUser {
|
|||
String replyto = (reply_to_msg > 0 ? "&reply_to_message_id=" +reply_to_msg : "");
|
||||
try {
|
||||
String replymarkup = (rm == null ? "" : "&reply_markup=" + URLEncoder.encode(rm.toJSONString(), "UTF-8"));
|
||||
return api.request("sendMessage", "chat_id=" + id
|
||||
return api.request("sendMessage", "chat_id=" + userid
|
||||
+ "&text=" + URLEncoder.encode(text, "UTF-8")
|
||||
+ replymarkup + replyto).getBoolean("ok");
|
||||
} catch (IOException e) {
|
||||
|
@ -104,11 +117,11 @@ public class TUser {
|
|||
}
|
||||
}
|
||||
|
||||
public void sendImage(String caption, String url) {
|
||||
sendImage(api, id, caption, url);
|
||||
public boolean sendImage(String caption, String url) {
|
||||
return sendImage(api, id, caption, url);
|
||||
}
|
||||
|
||||
public static void sendImage(TelegramAPI api, int userid, String caption, String url) {
|
||||
public static boolean sendImage(TelegramAPI api, int userid, String caption, String url) {
|
||||
try {
|
||||
String cap = "";
|
||||
if(caption != null) {
|
||||
|
@ -118,15 +131,16 @@ public class TUser {
|
|||
api.request("sendPhoto", "chat_id=" + userid + cap + "&photo=" + url);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public void sendAnimation(String caption, String url) {
|
||||
sendAnimation(api, id, caption, url);
|
||||
public boolean sendAnimation(String caption, String url) {
|
||||
return sendAnimation(api, id, caption, url);
|
||||
}
|
||||
|
||||
public static void sendAnimation(TelegramAPI api, int userid, String caption, String url) {
|
||||
public static boolean sendAnimation(TelegramAPI api, int userid, String caption, String url) {
|
||||
try {
|
||||
String cap = "";
|
||||
if(caption != null) {
|
||||
|
@ -136,7 +150,9 @@ public class TUser {
|
|||
api.request("sendAnimation", "chat_id=" + userid + cap + "&animation=" + url);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue