TelegramAPI/src/main/java/de/mrbesen/telegram/objects/TMessage.java

163 lines
5.2 KiB
Java

package de.mrbesen.telegram.objects;
import de.mrbesen.telegram.AsyncHandler.Task;
import de.mrbesen.telegram.TelegramAPI;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
public class TMessage extends JSONBased {
private int message_id;
private TUser from = null;//optional
private long date = -1;
private TUser forward_from = null; //optional
private String text = null;//optional
private long chatid = 0;
protected TelegramAPI api;
public void sendTo(TUser u) {
//if this message already exists forward, if not send directly.
throw new RuntimeException("not implemented");
}
public boolean reply(String msg, TReplyMarkup rm) {
if(from == null) {
throw new RuntimeException("no from defined!");
} else {
return TUser.sendMessage(api, chatid != 0 ? chatid : from.getID(), msg, rm, message_id, false, null);
}
// try {
// return api.request("sendMessage", "chat_id=" + from.getID() + "&text=" + msg + "&reply_to_message_id=" + message_id ).getBoolean("ok") ;
// } catch (IOException | NullPointerException e) {
// e.printStackTrace();
// return false;
// }
}
public boolean reply(String msg) {
return reply(msg, null);
}
public TMessage forward(TUser us) {
return forward(api, us.getID(), this);
}
public static TMessage forward(TelegramAPI api, long userid, TMessage tmsg) {
try {
String fro = String.valueOf(tmsg.forward_from == null ? tmsg.from.getID() : tmsg.forward_from.getID());
return new TMessage(api.request("forwardMessage", "chat_id=" + userid + "&from_chat_id=" + fro + "&message_id=" + tmsg.message_id, userid).getJSONObject("result"), api);
} catch (IOException | NullPointerException e) {
e.printStackTrace();
}
return null;
}
public static void forwardAsync(TelegramAPI api, long userid, TMessage tmsg) {
String fro = String.valueOf(tmsg.forward_from == null ? tmsg.from.getID() : tmsg.forward_from.getID());
api.request(new Task("forwardMessage", "chat_id=" + userid + "&from_chat_id=" + fro + "&message_id=" + tmsg.message_id, userid));
}
public TMessage(JSONObject json, TelegramAPI api) {
super(json);
this.api = api;
message_id = json.getInt("message_id");
date = json.getLong("date");
chatid = ((JSONObject) get(Member.chat)).getLong("id");
if(json.has("from"))
from = api.getUser(json.getJSONObject("from"));
if(json.has("forward_from"))
forward_from = api.getUser(json.getJSONObject("forward_from"));
if(json.has("text") )
text = json.getString("text");
allowedmembers = new Member[] {Member.forward_from_message_id, Member.forward_signature, Member.forward_date, Member.reply_to_message, Member.edit_date, Member.media_group_id, Member.author_signature, Member.author_signature, Member.audio, Member.document, Member.game, Member.photo, Member.sticker, Member.video, Member.voice, Member.video_note, Member.caption, Member.contact, Member.location, Member.venue, Member.new_chat_members, Member.left_chat_members, Member.new_chat_title, Member.new_chat_photo, Member.delete_chat_photo, Member.group_chat_created, Member.supergroup_chat_created, Member.channel_chat_created, Member.migrate_from_chat_id, Member.migrate_to_chat_id, Member.pinned_message, Member.invoice, Member.successful_payment, Member.connected_website, Member.chat};
}
public long getChatID() {
return chatid;
}
public TUser getFrom() {
return from;
}
public String getText() {
return text;
}
public long getDate() {
return date;
}
public TUser getForward_from() {
return forward_from;
}
public int getMessageID() {
return message_id;
}
/**
* Get the FileID if a file is attached to this message. If More than one File is attached to this message only one file id is returned
* @return
*/
public String getFileID() {
TSendable t = getFile();
return t == null ? null : t.getFile_id();
}
/**
* Get the FileName if a file is attached to this message. If More than one File is attached to this message only one filename is returned
* @return
*/
public String getFileName() {
try {
TSendable t = getFile();
return t == null ? null : t.get(Member.file_name); //TSendable does not garuantee a filename
} catch (JSONException e) {
return ""; //file exists but has no name?
}
}
/**
* get a file if one is connected to this message
* @return
*/
private TSendable getFile() {
try {
if(has(Member.document))
return new TDocument(get(Member.document));
else if(has(Member.video))
return new TVideo(get(Member.video));
else if(has(Member.audio))
return new TAudio(get(Member.audio));
else if(has(Member.voice))
return new TAudio(get(Member.voice)); //TODO: suboptimal, eine Voice als audio zu verstehen, sollte aber funktionieren, da alle nicht optinalen felder gleich sind
else return null;
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
public static void delete(TelegramAPI api, long chatid, int msgid) {
delete(api, chatid, msgid, false);
}
public static void delete(TelegramAPI api, long chatid, int msgid, boolean async) {
String q = "chat_id=" + chatid + "&message_id=" + msgid;
if(async) {
api.requestAsync("deleteMessage", q, chatid);
} else {
try {
api.request("deleteMessage", q, chatid);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}