TMessage getFile
This commit is contained in:
parent
a4a0b8e649
commit
1fe7bc64d3
|
@ -1,11 +1,11 @@
|
|||
package de.mrbesen.telegram.objects;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
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 {
|
||||
|
||||
|
@ -105,17 +105,38 @@ public class TMessage extends JSONBased {
|
|||
* @return
|
||||
*/
|
||||
public String getFileID() {
|
||||
try {
|
||||
JSONObject doc;
|
||||
if(has(JSONBased.Member.document))
|
||||
doc = ((JSONObject) get(JSONBased.Member.document));
|
||||
else if(has(JSONBased.Member.video))
|
||||
doc = ((JSONObject) get(JSONBased.Member.video));
|
||||
else if(has(JSONBased.Member.audio))
|
||||
doc = ((JSONObject) get(JSONBased.Member.audio));
|
||||
else return null;
|
||||
TSendable t = getFile();
|
||||
return t == null ? null : t.getFile_id();
|
||||
}
|
||||
|
||||
return doc.getString("file_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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue