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(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 void sendMessage(String text) { sendMessage(text, null); } public void sendMessage(String text, TReplyMarkup rm) { if(api == null) { System.err.println("api == null!"); return; } try { String reply= (rm == null ? "" : "&reply_markup=" + URLEncoder.encode(rm.toJSONString(), "UTF-8")); api.request("sendMessage", "chat_id=" + id + "&text=" + URLEncoder.encode(text, "UTF-8") + reply ); } catch (IOException e) { e.printStackTrace(); } } 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 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(); } } }