156 lines
3.9 KiB
Java
156 lines
3.9 KiB
Java
package de.mrbesen.telegram.objects;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import de.mrbesen.telegram.AsyncHandler.Callback;
|
|
import de.mrbesen.telegram.MessageBuilder;
|
|
import de.mrbesen.telegram.MessageBuilder.Attachment;
|
|
import de.mrbesen.telegram.TelegramAPI;
|
|
|
|
public class TUser {
|
|
|
|
private int id;
|
|
|
|
private String uname;//optional
|
|
private String firstname;
|
|
private String lastname;//optional
|
|
private String langcode; // optional
|
|
private boolean isBot = false;
|
|
|
|
private TelegramAPI api = null;
|
|
|
|
TUser(int chatid, String uname, TelegramAPI api) {
|
|
this.id = chatid;
|
|
this.uname = uname;
|
|
this.api = api;
|
|
}
|
|
|
|
public TUser(int chatid, TelegramAPI api) {
|
|
this.api = api;
|
|
this.id = chatid;
|
|
}
|
|
|
|
public TUser(JSONObject o, TelegramAPI api) {
|
|
this.api = api;
|
|
firstname = o.getString("first_name");
|
|
isBot = o.getBoolean("is_bot");
|
|
id = o.getInt("id");
|
|
if(o.has("last_name"))
|
|
lastname = o.getString("last_name");
|
|
if(o.has("username"))
|
|
uname = o.getString("username");
|
|
if(o.has("language_code"))
|
|
langcode = o.getString("language_code");
|
|
}
|
|
|
|
public String getFirstName() {
|
|
return firstname;
|
|
}
|
|
|
|
public String getLastName() {
|
|
return lastname;
|
|
}
|
|
|
|
public String getLangcode() {
|
|
return langcode;
|
|
}
|
|
|
|
public boolean isBot() {
|
|
return isBot;
|
|
}
|
|
|
|
public int getID() {
|
|
return id;
|
|
}
|
|
|
|
public String getName() {
|
|
return uname;
|
|
}
|
|
|
|
public void sendHelpPage() {
|
|
sendMessage(api.getHelpMessage());
|
|
}
|
|
|
|
public boolean sendMessage(String text) {
|
|
return sendMessage(text, null);
|
|
}
|
|
|
|
public boolean sendMessage(String text, TReplyMarkup rm) {
|
|
return sendMessage(text, rm, -1);
|
|
}
|
|
|
|
public boolean sendMessage(String text, TReplyMarkup rm, int reply_to_msg) {
|
|
return sendMessage(api, id, text, rm, reply_to_msg, false, null);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @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, boolean async, Callback<TMessage, ?> callb) {
|
|
if(api == null) {
|
|
System.err.println("api == null!");
|
|
return false;
|
|
}
|
|
MessageBuilder msgb = new MessageBuilder().setReciver(userid).setReplyTo(reply_to_msg).setText(text).setCallback(callb).setMarkup(rm).setAsync(async);
|
|
api.sendMessage(msgb.build());
|
|
return true;
|
|
}
|
|
|
|
public void sendStatus(Status status) {
|
|
try {
|
|
api.request("sendChatAction", "chat_id="+id+"&action=" + status.getAPIName());
|
|
} catch (Exception e) {
|
|
System.err.println("Failed to send status.");
|
|
}
|
|
}
|
|
|
|
public boolean sendImage(String caption, String url) {
|
|
return sendImage(api, id, caption, url, null, null) != null;
|
|
}
|
|
|
|
public static TMessage sendImage(TelegramAPI api, int userid, String caption, String url, TReplyMarkup rply, Callback<TMessage, ?> async) {
|
|
MessageBuilder msgb = new MessageBuilder().setReciver(userid).setCaption(caption).setAttachment(Attachment.Photo, url).setMarkup(rply).setCallback(async).setAsync(async != null);
|
|
return api.sendMessage(msgb.build());
|
|
}
|
|
|
|
public boolean sendAnimation(String caption, String url) {
|
|
return sendAnimation(api, id, caption, url, null, null) != null;
|
|
}
|
|
|
|
public static TMessage sendAnimation(TelegramAPI api, int userid, String caption, String url, TReplyMarkup rply, Callback<TMessage, ?> async) {
|
|
MessageBuilder msgb = new MessageBuilder().setReciver(userid).setCaption(caption).setAttachment(Attachment.Animation, url).setMarkup(rply).setCallback(async).setAsync(async != null);
|
|
return api.sendMessage(msgb.build());
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object user) {
|
|
if(user instanceof TUser) {
|
|
return ((TUser) user).getID() == id;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public enum Status {
|
|
Typing,
|
|
Upload_Photo,
|
|
Record_Video,
|
|
Upload_Video,
|
|
Record_Audio,
|
|
Upload_Audio,
|
|
Upload_Document,
|
|
Find_Location,
|
|
Record_Video_Note,
|
|
Upload_Video_Note;
|
|
|
|
String getAPIName() {
|
|
return name().toLowerCase();
|
|
}
|
|
}
|
|
}
|