added callback Event

This commit is contained in:
mrbesen 2019-02-06 23:52:15 +01:00
parent 8a37a6881d
commit 72e321c741
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
4 changed files with 48 additions and 7 deletions

View File

@ -15,6 +15,7 @@ import de.mrbesen.telegram.MessageBuilder.SendableMessage;
import de.mrbesen.telegram.commands.CommandManager;
import de.mrbesen.telegram.event.Event;
import de.mrbesen.telegram.event.EventManager;
import de.mrbesen.telegram.event.events.UserCallbackEvent;
import de.mrbesen.telegram.event.events.UserSendAudioEvent;
import de.mrbesen.telegram.event.events.UserSendMessageEvent;
import de.mrbesen.telegram.log.Log;
@ -289,6 +290,11 @@ public class TelegramAPI implements Runnable {
getEventManager().callEvent(e);
}
}
} else if(json.has("callback_query")) {
JSONObject cbq = json.getJSONObject("callback_query");
TUser from = api.getUser(cbq.getJSONObject("from"));
String data = cbq.getString("data");
getEventManager().callEvent(new UserCallbackEvent(from, data));
}
}

View File

@ -0,0 +1,25 @@
package de.mrbesen.telegram.event.events;
import org.json.JSONObject;
import de.mrbesen.telegram.event.Event;
import de.mrbesen.telegram.objects.TUser;
public class UserCallbackEvent extends Event {
private final String data;
public UserCallbackEvent(TUser u, String data) {
super(u);
this.data = data;
}
public String getData() {
return data;
}
public JSONObject asJSON() {
return new JSONObject(data);
}
}

View File

@ -44,6 +44,11 @@ public class TInlineKeyboardMarkup implements TReplyMarkup {
return this;
}
public TInlineKeyboardMarkup addCallbackButton(String title, String callback, int row) {
arr_btn.get(row-1).put(new JSONObject().put("text", title).put("callback_data", callback));
return this;
}
@Override
public String toJSONString() {
return new JSONObject().put("inline_keyboard", new JSONArray(arr_btn)).toString();

View File

@ -99,7 +99,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"));
String replymarkup = parseRPMU(rm);
return api.request("sendMessage", "chat_id=" + userid
+ "&text=" + URLEncoder.encode(text, "UTF-8")
+ replymarkup + replyto).getBoolean("ok");
@ -109,6 +109,11 @@ public class TUser {
return false;
}
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 {
api.request("sendChatAction", "chat_id="+id+"&action=" + status.getAPIName());
@ -118,17 +123,17 @@ public class TUser {
}
public boolean sendImage(String caption, String url) {
return sendImage(api, id, caption, url);
return sendImage(api, id, caption, url, null);
}
public static boolean sendImage(TelegramAPI api, int userid, String caption, String url) {
public static boolean sendImage(TelegramAPI api, int userid, String caption, String url, TReplyMarkup rply) {
try {
String cap = "";
if(caption != null) {
if(!caption.isEmpty())
cap = "&caption=" + caption;
}
api.request("sendPhoto", "chat_id=" + userid + cap + "&photo=" + url);
api.request("sendPhoto", "chat_id=" + userid + parseRPMU(rply) + cap + "&photo=" + url);
} catch (IOException e) {
e.printStackTrace();
return false;
@ -137,17 +142,17 @@ public class TUser {
}
public boolean sendAnimation(String caption, String url) {
return sendAnimation(api, id, caption, url);
return sendAnimation(api, id, caption, url, null);
}
public static boolean sendAnimation(TelegramAPI api, int userid, String caption, String url) {
public static boolean sendAnimation(TelegramAPI api, int userid, String caption, String url, TReplyMarkup rply) {
try {
String cap = "";
if(caption != null) {
if(!caption.isEmpty())
cap = "&caption=" + caption;
}
api.request("sendAnimation", "chat_id=" + userid + cap + "&animation=" + url);
api.request("sendAnimation", "chat_id=" + userid + parseRPMU(rply) + cap + "&animation=" + url);
} catch (IOException e) {
e.printStackTrace();
return false;