added TUser.sendMessage and advanced error reporting

This commit is contained in:
mrbesen 2019-02-06 18:32:42 +01:00
parent 5147b40fdc
commit df285ac33c
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
2 changed files with 65 additions and 43 deletions

View File

@ -42,7 +42,7 @@ public class TelegramAPI implements Runnable {
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());
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();
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) {
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);
}
}
@ -103,18 +109,18 @@ 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) {
}
sendMessage(new MessageBuilder().setText(msg).setReciver(user.getID()).build());
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());
}
}).start();
}
}).start();
}
public void stop() {

View File

@ -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