157 lines
3.3 KiB
Java
157 lines
3.3 KiB
Java
package de.mrbesen.telegram.objects;
|
|
|
|
import java.io.IOException;
|
|
import java.net.URLEncoder;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
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) {
|
|
if(api == null) {
|
|
System.err.println("api == null!");
|
|
return false;
|
|
}
|
|
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"));
|
|
return api.request("sendMessage", "chat_id=" + id
|
|
+ "&text=" + URLEncoder.encode(text, "UTF-8")
|
|
+ replymarkup + replyto).getBoolean("ok");
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
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 void sendImage(String caption, String url) {
|
|
try {
|
|
String cap = "";
|
|
if(caption != null) {
|
|
if(!caption.isEmpty())
|
|
cap = "&caption=" + caption;
|
|
}
|
|
api.request("sendPhoto", "chat_id" + id + cap + "&photo=" + url);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
public void sendAnimation(String caption, String url) {
|
|
try {
|
|
String cap = "";
|
|
if(caption != null) {
|
|
if(!caption.isEmpty())
|
|
cap = "&caption=" + caption;
|
|
}
|
|
api.request("sendAnimation", "chat_id" + id + cap + "&animation=" + url);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@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();
|
|
}
|
|
}
|
|
}
|