neues event setzten (ohne casten), neue events(UserSendPhoto, UserSendDocument)

This commit is contained in:
mrbesen 2021-01-16 14:53:13 +01:00
parent d359b89851
commit 1e7b3f4794
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
15 changed files with 116 additions and 104 deletions

View File

@ -8,9 +8,7 @@ import de.mrbesen.telegram.commands.CommandManager;
import de.mrbesen.telegram.commands.FeedbackCommand; import de.mrbesen.telegram.commands.FeedbackCommand;
import de.mrbesen.telegram.event.Event; import de.mrbesen.telegram.event.Event;
import de.mrbesen.telegram.event.EventManager; import de.mrbesen.telegram.event.EventManager;
import de.mrbesen.telegram.event.events.UserCallbackEvent; import de.mrbesen.telegram.event.events.*;
import de.mrbesen.telegram.event.events.UserSendAudioEvent;
import de.mrbesen.telegram.event.events.UserSendMessageEvent;
import de.mrbesen.telegram.log.Log; import de.mrbesen.telegram.log.Log;
import de.mrbesen.telegram.log.Log4JLog; import de.mrbesen.telegram.log.Log4JLog;
import de.mrbesen.telegram.log.SimpleLog; import de.mrbesen.telegram.log.SimpleLog;
@ -43,7 +41,7 @@ public class TelegramAPI implements Runnable {
private static final String API_URL_DEFAULT = "https://api.telegram.org/bot"; private static final String API_URL_DEFAULT = "https://api.telegram.org/bot";
private static final String TOKENREGEX = "^\\d{4,10}:[\\w-]{12,64}$"; private static final String TOKENREGEX = "^\\d{4,10}:[\\w-]{12,64}$";
private static final int TELEGRAMFILESIZELIMIT = 20000000;//20MB filesize https://core.telegram.org/bots/api#sending-files private static final int TELEGRAMFILESIZELIMIT = 20000000;//20MB filesize https://core.telegram.org/bots/api#sending-files
public static final String APIVERSION = "3.9";//Feb 29, 2020 public static final String APIVERSION = "3.10";//Jan 16, 2021
private int msg_offset = 0; private int msg_offset = 0;
private int updateInterval = 60; private int updateInterval = 60;
@ -508,7 +506,7 @@ public class TelegramAPI implements Runnable {
} }
/** /**
* seconds to wait for longpolling or milliseconds to wait petween shortpolling * seconds to wait for longpolling or milliseconds to wait between shortpolling
* @param d * @param d
*/ */
public void setUpdateInterval(int d) { public void setUpdateInterval(int d) {
@ -575,26 +573,28 @@ public class TelegramAPI implements Runnable {
} }
} else { } else {
Event e = null; Event e = null;
if(getMsg().has(Member.audio)) { if(msg.has(Member.audio)) {
e = new UserSendAudioEvent(getUser(), new TAudio((JSONObject) getMsg().get(Member.audio))); e = new UserSendAudioEvent(msg);
} else if(getMsg().has(Member.video)) { } else if(msg.has(Member.video)) {
} else if(getMsg().has(Member.document)) { } else if(msg.has(Member.document)) {
e = new UserSendDocumentEvent(msg);
} else if(msg.has(Member.photo)) {
e = new UserSendPhotoEvent(msg);
} else if(msg.has(Member.invoice)) {
} else if(getMsg().has(Member.invoice)) { } else if(msg.has(Member.location)) {
} else if(getMsg().has(Member.location)) { } else if(msg.has(Member.video_note)) {
} else if(getMsg().has(Member.video_note)) { } else if(msg.has(Member.game)) {
} else if(getMsg().has(Member.game)) { } else if(msg.has(Member.contact)) {
} else if(getMsg().has(Member.contact)) { } else if(msg.has(Member.sticker)) {
} else if(getMsg().has(Member.sticker)) {
} else { } else {
e = new UserSendMessageEvent(getMsg()); e = new UserSendMessageEvent(msg);
//call feedback cmd first //call feedback cmd first
if(!disableFeedback) { if(!disableFeedback) {
if (feedbackCmd.onMsg((UserSendMessageEvent) e)) { if (feedbackCmd.onMsg((UserSendMessageEvent) e)) {

View File

@ -57,9 +57,9 @@ public class FeedbackCommand implements JSONCommandHandler {
public boolean onMsg(UserSendMessageEvent e) { public boolean onMsg(UserSendMessageEvent e) {
TUser user = e.getUser(); TUser user = e.getUser();
TMessage msg = e.getMessage(); TMessage msg = e.getMsg();
if (awaitingFeedback.containsKey(user.getID())) { //user send a feedback if (awaitingFeedback.containsKey(user.getID())) { //user send a feedback
removeMsg(user.getID(), e.getMessage()); removeMsg(user.getID(), e.getMsg());
if(isFeedback(msg.getText())) { if(isFeedback(msg.getText())) {
//fwd to admins //fwd to admins
admins.forEach(a -> TMessage.forwardAsync(api, a, msg)); admins.forEach(a -> TMessage.forwardAsync(api, a, msg));

View File

@ -1,17 +1,5 @@
package de.mrbesen.telegram.event; package de.mrbesen.telegram.event;
import de.mrbesen.telegram.objects.TUser; public interface Event {
public class Event {
private TUser user;
public Event(TUser u) {
user = u;
}
public TUser getUser() {
return user;
}
} }

View File

@ -6,17 +6,17 @@ import java.util.function.Consumer;
public class EventManager { public class EventManager {
private HashMap<Class<? extends Event>, ArrayList<Consumer<? super Event>>> listeners = new HashMap<>(); private HashMap<Class<? extends Event>, ArrayList<Consumer<? extends Event>>> listeners = new HashMap<>();
public EventManager() {} public EventManager() {}
public void registerEvent(Class<? extends Event> eventtype, Consumer<? super Event> handler) { public <T extends Event> void registerEvent(Class<T> eventtype, Consumer<T> handler) {
if (handler == null) { if (handler == null) {
throw new NullPointerException(); throw new NullPointerException();
} }
//get list //get list
ArrayList<Consumer<? super Event>> list = listeners.get(eventtype); ArrayList<Consumer<? extends Event>> list = listeners.get(eventtype);
if (list == null) { if (list == null) {
//create and add list //create and add list
list = new ArrayList<>(); list = new ArrayList<>();
@ -29,9 +29,9 @@ public class EventManager {
} }
public Event callEvent(Event e) { public Event callEvent(Event e) {
if(e == null) return e; if(e == null) return null;
ArrayList<Consumer<? super Event>> listner = listeners.get(e.getClass()); ArrayList<Consumer<? extends Event>> listner = listeners.get(e.getClass());
if(listner != null) { if(listner != null) {
for (Consumer listn : listner) { for (Consumer listn : listner) {
try { try {

View File

@ -0,0 +1,18 @@
package de.mrbesen.telegram.event.events;
import de.mrbesen.telegram.event.Event;
import de.mrbesen.telegram.objects.TMessage;
import de.mrbesen.telegram.objects.TUser;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
public class MessageEvent implements Event {
@Getter
private TMessage msg;
public TUser getUser() {
return msg.getFrom();
}
}

View File

@ -1,37 +1,27 @@
package de.mrbesen.telegram.event.events; package de.mrbesen.telegram.event.events;
import lombok.Getter;
import org.json.JSONObject; import org.json.JSONObject;
import de.mrbesen.telegram.event.Event; import de.mrbesen.telegram.event.Event;
import de.mrbesen.telegram.objects.TMessage; import de.mrbesen.telegram.objects.TMessage;
import de.mrbesen.telegram.objects.TUser; import de.mrbesen.telegram.objects.TUser;
public class UserCallbackEvent extends Event { public class UserCallbackEvent extends MessageEvent {
@Getter
private final String data; private final String data;
private final TMessage msg; @Getter
private final String id; private final String ID;
public UserCallbackEvent(TUser u, String data, String id, TMessage msg) { public UserCallbackEvent(TUser u, String data, String id, TMessage msg) {
super(u); super(msg);
this.data = data; this.data = data;
this.msg = msg; this.ID = id;
this.id = id;
}
public TMessage getMsg() {
return msg;
}
public String getData() {
return data;
} }
public JSONObject asJSON() { public JSONObject asJSON() {
return new JSONObject(data); return new JSONObject(data);
} }
public final String getID() {
return id;
}
} }

View File

@ -1,19 +1,17 @@
package de.mrbesen.telegram.event.events; package de.mrbesen.telegram.event.events;
import de.mrbesen.telegram.event.Event; import de.mrbesen.telegram.objects.JSONBased;
import de.mrbesen.telegram.objects.TAudio; import de.mrbesen.telegram.objects.TAudio;
import de.mrbesen.telegram.objects.TUser; import de.mrbesen.telegram.objects.TMessage;
import lombok.Getter;
public class UserSendAudioEvent extends Event { public class UserSendAudioEvent extends MessageEvent {
@Getter
private TAudio audio; private TAudio audio;
public UserSendAudioEvent(TUser u, TAudio a) { public UserSendAudioEvent(TMessage msg) {
super(u); super(msg);
audio = a; audio = new TAudio(msg.get(JSONBased.Member.audio));
}
public TAudio getAudio() {
return audio;
} }
} }

View File

@ -0,0 +1,17 @@
package de.mrbesen.telegram.event.events;
import de.mrbesen.telegram.objects.JSONBased;
import de.mrbesen.telegram.objects.TDocument;
import de.mrbesen.telegram.objects.TMessage;
import lombok.Getter;
public class UserSendDocumentEvent extends MessageEvent {
@Getter
private TDocument document;
public UserSendDocumentEvent(TMessage msg) {
super(msg);
document = new TDocument(msg.get(JSONBased.Member.document));
}
}

View File

@ -1,23 +1,10 @@
package de.mrbesen.telegram.event.events; package de.mrbesen.telegram.event.events;
import de.mrbesen.telegram.event.Event;
import de.mrbesen.telegram.objects.TMessage; import de.mrbesen.telegram.objects.TMessage;
import de.mrbesen.telegram.objects.TUser;
public class UserSendMessageEvent extends Event { public class UserSendMessageEvent extends MessageEvent {
private TMessage m;
public UserSendMessageEvent(TMessage m) { public UserSendMessageEvent(TMessage m) {
super(m.getFrom()); super(m);
this.m = m;
}
public TMessage getMessage() {
return m;
}
public TUser getUser() {
return m.getFrom();
} }
} }

View File

@ -0,0 +1,25 @@
package de.mrbesen.telegram.event.events;
import de.mrbesen.telegram.objects.JSONBased;
import de.mrbesen.telegram.objects.TMessage;
import de.mrbesen.telegram.objects.TPhotoSize;
import lombok.Getter;
import org.json.JSONArray;
import org.json.JSONObject;
public class UserSendPhotoEvent extends MessageEvent {
@Getter
private TPhotoSize[] photos;
public UserSendPhotoEvent(TMessage msg) {
super(msg);
JSONArray pics = msg.get(JSONBased.Member.photo);
photos = new TPhotoSize[pics.length()];
for (int i = 0; i < pics.length(); i++) {
JSONObject pic = pics.getJSONObject(i);
photos[i] = new TPhotoSize(pic);
}
}
}

View File

@ -1,8 +1,11 @@
package de.mrbesen.telegram.objects; package de.mrbesen.telegram.objects;
import lombok.Getter;
import org.json.JSONObject; import org.json.JSONObject;
public class TAudio extends TDocument { public class TAudio extends TDocument {
@Getter
private int duration; private int duration;
public TAudio(JSONObject o) { public TAudio(JSONObject o) {
@ -10,8 +13,4 @@ public class TAudio extends TDocument {
duration = o.getInt("duration"); duration = o.getInt("duration");
allowedmembers = new Member[] {Member.performer, Member.title, Member.mime_type, Member.file_size}; allowedmembers = new Member[] {Member.performer, Member.title, Member.mime_type, Member.file_size};
} }
public int getDuration() {
return duration;
}
} }

View File

@ -6,10 +6,6 @@ public class TDocument extends TSendable {
public TDocument(JSONObject o) { public TDocument(JSONObject o) {
super(o); super(o);
allowedmembers = new Member[] {Member.thumb, Member.file_name, Member.mime_type, Member.file_size}; allowedmembers = new Member[] {Member.file_id, Member.file_unique_id, Member.thumb, Member.file_name, Member.mime_type, Member.file_size};
}
public String getFile_id() {
return file_id;
} }
} }

View File

@ -2,7 +2,7 @@ package de.mrbesen.telegram.objects;
import org.json.JSONObject; import org.json.JSONObject;
public class TFile extends TDocument { public class TFile extends JSONBased {
public TFile(JSONObject json) { public TFile(JSONObject json) {
super(json); super(json);

View File

@ -1,9 +1,11 @@
package de.mrbesen.telegram.objects; package de.mrbesen.telegram.objects;
import lombok.Getter;
import org.json.JSONObject; import org.json.JSONObject;
public class TPhotoSize extends TDocument { public class TPhotoSize extends TSendable {
@Getter
private int width, height; private int width, height;
public TPhotoSize(JSONObject json) { public TPhotoSize(JSONObject json) {
@ -12,12 +14,4 @@ public class TPhotoSize extends TDocument {
height = json.getInt("height"); height = json.getInt("height");
allowedmembers = new Member[] {Member.file_size}; allowedmembers = new Member[] {Member.file_size};
} }
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
} }

View File

@ -1,12 +1,16 @@
package de.mrbesen.telegram.objects; package de.mrbesen.telegram.objects;
import lombok.Getter;
import org.json.JSONObject; import org.json.JSONObject;
public class TSendable extends JSONBased { public class TSendable extends JSONBased {
@Getter
protected String file_id; protected String file_id;
@Getter
protected int file_size;
public void set(Member mem, Object value ) { public void set(Member mem, Object value) {
if(isAllowed(mem)) { if(isAllowed(mem)) {
if(value instanceof Integer) if(value instanceof Integer)
base_json.put(mem.name(), (int) value); base_json.put(mem.name(), (int) value);
@ -22,10 +26,6 @@ public class TSendable extends JSONBased {
public TSendable(JSONObject o) { public TSendable(JSONObject o) {
super(o); super(o);
file_id = o.getString("file_id"); file_id = o.getString("file_id");
} file_size = o.optInt("file_size", -1);
public boolean upload() {
return false;
} }
} }