Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package erainformatica.tbjhelper.helpers; import erainformatica.tbjhelper.classes.File; import erainformatica.tbjhelper.classes.TelegramRequestResult; import erainformatica.tbjhelper.classes.ForceReply; import erainformatica.tbjhelper.classes.InputFile; import erainformatica.tbjhelper.classes.Message; import erainformatica.tbjhelper.classes.ReplyKeyboardHide; import erainformatica.tbjhelper.classes.ReplyKeyboardMarkup; import erainformatica.tbjhelper.classes.User; import erainformatica.tbjhelper.classes.UserProfilePhotos; import erainformatica.tbjhelper.protocols.TelegramBotProtocol; import erainformatica.utility.JSONHelper; import erainformatica.utility.Utility; import java.net.URL; import java.util.HashMap; import org.json.JSONObject; /** * * @author robertodedomenico */ public class TelegramBot implements TelegramBotProtocol { // ATTRIBUTES String api_key; // GETTERS AND SETTERS public String getApi_key() { return api_key; } public void setApi_key(String api_key) { this.api_key = api_key; } // CONTSTUCTORS public TelegramBot(String api_key) { this.api_key = api_key; } // METHODS @Override public TelegramRequestResult<User> getMe() { if (api_key == null || "".equals(api_key)) { return new TelegramRequestResult<>(false, "Token not valid", null); } else { User user = null; String url = Utility.buildAPIUrl(this.api_key, TelegramBot.API_OPERATION_GET_ME, null); TelegramRequestResult<JSONObject> result = JSONHelper.readJsonFromUrl(url); if (result.isRequestOk()) { JSONObject r = result.get(); if (r.getBoolean("ok") == true) { user = (User) Utility.castJsonToObject(r.getJSONObject("result").toString(), TelegramBot.TYPE_USER); if (user != null) return new TelegramRequestResult<>(true, null, user); else return new TelegramRequestResult<>(false, "No result", null); } else { return new TelegramRequestResult<>(false, result.get().getString("description"), null); } } else { return new TelegramRequestResult<>(false, result.getError(), null); } } } @Override public TelegramRequestResult<Message> sendMessage(Integer chat_id, String text) { return sendMessageUniversal(chat_id, text, null, null, null, null); } @Override public TelegramRequestResult<Message> sendMessage(Integer chat_id, String text, String parse_mode, Boolean disable_web_page_preview, Integer reply_to_message_id, ReplyKeyboardMarkup reply_markup) { return sendMessageUniversal(chat_id, text, parse_mode, disable_web_page_preview, reply_to_message_id, reply_markup); } @Override public TelegramRequestResult<Message> sendMessage(Integer chat_id, String text, String parse_mode, Boolean disable_web_page_preview, Integer reply_to_message_id, ReplyKeyboardHide reply_markup) { return sendMessageUniversal(chat_id, text, parse_mode, disable_web_page_preview, reply_to_message_id, reply_markup); } @Override public TelegramRequestResult<Message> sendMessage(Integer chat_id, String text, String parse_mode, Boolean disable_web_page_preview, Integer reply_to_message_id, ForceReply reply_markup) { return sendMessageUniversal(chat_id, text, parse_mode, disable_web_page_preview, reply_to_message_id, reply_markup); } @Override public TelegramRequestResult<Message> forwardMessage(Integer chat_id, Integer from_chat_id, Integer message_id) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> forwardMessage(Integer chat_id, String from_chat_id, Integer message_id) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> forwardMessage(String chat_id, Integer from_chat_id, Integer message_id) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> forwardMessage(String chat_id, String from_chat_id, Integer message_id) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendPhoto(Integer chat_id, InputFile photo) { return sendPhotoUniversal(chat_id, photo, null, null, null); } @Override public TelegramRequestResult<Message> sendPhoto(Integer chat_id, String file_id) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendPhoto(Integer chat_id, InputFile photo, String caption, Integer reply_to_message_id, ReplyKeyboardMarkup reply_markup) { return sendPhotoUniversal(chat_id, photo, caption, reply_to_message_id, reply_markup); } @Override public TelegramRequestResult<Message> sendPhoto(Integer chat_id, InputFile photo, String caption, Integer reply_to_message_id, ReplyKeyboardHide reply_markup) { return sendPhotoUniversal(chat_id, photo, caption, reply_to_message_id, reply_markup); } @Override public TelegramRequestResult<Message> sendPhoto(Integer chat_id, InputFile photo, String caption, Integer reply_to_message_id, ForceReply reply_markup) { return sendPhotoUniversal(chat_id, photo, caption, reply_to_message_id, reply_markup); } @Override public TelegramRequestResult<Message> sendPhoto(Integer chat_id, String photo, String caption, Integer reply_to_message_id, ReplyKeyboardMarkup reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendPhoto(Integer chat_id, String photo, String caption, Integer reply_to_message_id, ReplyKeyboardHide reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendPhoto(Integer chat_id, String photo, String caption, Integer reply_to_message_id, ForceReply reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendAudio(Integer chat_id, String audio) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendAudio(Integer chat_id, InputFile audio, Integer reply_to_message_id, ReplyKeyboardMarkup reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendAudio(Integer chat_id, InputFile audio, Integer reply_to_message_id, ReplyKeyboardHide reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendAudio(Integer chat_id, InputFile audio, Integer reply_to_message_id, ForceReply reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendAudio(Integer chat_id, String audio, Integer reply_to_message_id, ReplyKeyboardMarkup reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendAudio(Integer chat_id, String audio, Integer reply_to_message_id, ReplyKeyboardHide reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendAudio(Integer chat_id, InputFile audio) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendAudio(Integer chat_id, String audio, Integer reply_to_message_id, ForceReply reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendDocument(Integer chat_id, InputFile document) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendDocument(Integer chat_id, String document) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendDocument(Integer chat_id, InputFile document, Integer reply_to_message_id, ReplyKeyboardMarkup reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendDocument(Integer chat_id, InputFile document, Integer reply_to_message_id, ReplyKeyboardHide reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendDocument(Integer chat_id, InputFile document, Integer reply_to_message_id, ForceReply reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendDocument(Integer chat_id, String document, Integer reply_to_message_id, ReplyKeyboardMarkup reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendDocument(Integer chat_id, String document, Integer reply_to_message_id, ReplyKeyboardHide reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendDocument(Integer chat_id, String document, Integer reply_to_message_id, ForceReply reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendSticker(Integer chat_id, InputFile sticker) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendSticker(Integer chat_id, String sticker) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendSticker(Integer chat_id, InputFile sticker, Integer reply_to_message_id, ReplyKeyboardMarkup reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendSticker(Integer chat_id, InputFile sticker, Integer reply_to_message_id, ReplyKeyboardHide reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendSticker(Integer chat_id, InputFile sticker, Integer reply_to_message_id, ForceReply reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendSticker(Integer chat_id, String sticker, Integer reply_to_message_id, ReplyKeyboardMarkup reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendSticker(Integer chat_id, String sticker, Integer reply_to_message_id, ReplyKeyboardHide reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendSticker(Integer chat_id, String sticker, Integer reply_to_message_id, ForceReply reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVideo(Integer chat_id, InputFile video) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVideo(Integer chat_id, String video) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVideo(Integer chat_id, InputFile video, Integer duration, String caption, Integer reply_to_message_id, ReplyKeyboardMarkup reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVideo(Integer chat_id, InputFile video, Integer duration, String caption, Integer reply_to_message_id, ReplyKeyboardHide reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVideo(Integer chat_id, InputFile video, Integer duration, String caption, Integer reply_to_message_id, ForceReply reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVideo(Integer chat_id, String video, Integer duration, String caption, Integer reply_to_message_id, ReplyKeyboardMarkup reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVideo(Integer chat_id, String video, Integer duration, String caption, Integer reply_to_message_id, ReplyKeyboardHide reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVideo(Integer chat_id, String video, Integer duration, String caption, Integer reply_to_message_id, ForceReply reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVoice(Integer chat_id, InputFile voice) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVoice(Integer chat_id, String voice) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVoice(Integer chat_id, InputFile voice, Integer duration, Integer reply_to_message_id, ReplyKeyboardMarkup reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVoice(Integer chat_id, InputFile voice, Integer duration, Integer reply_to_message_id, ReplyKeyboardHide reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVoice(Integer chat_id, InputFile voice, Integer duration, Integer reply_to_message_id, ForceReply reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVoice(Integer chat_id, String voice, Integer duration, Integer reply_to_message_id, ReplyKeyboardMarkup reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVoice(Integer chat_id, String voice, Integer duration, Integer reply_to_message_id, ReplyKeyboardHide reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendVoice(Integer chat_id, String voice, Integer duration, Integer reply_to_message_id, ForceReply reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendLocation(Integer chat_id, Double latitude, Double longitude, Integer reply_to_message_id, ReplyKeyboardMarkup reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendLocation(Integer chat_id, Double latitude, Double longitude, Integer reply_to_message_id, ReplyKeyboardHide reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendLocation(Integer chat_id, Double latitude, Double longitude, Integer reply_to_message_id, ForceReply reply_markup) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendLocation(Integer chat_id, Double latitude, Double longitude) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<Message> sendLocation(String chat_id, Double latitude, Double longitude) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void sendChatAction(Integer chat_id, String action) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<UserProfilePhotos> getUserProfilePhotos(Integer user_id, Integer offset, Integer limit) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult<UserProfilePhotos> getUserProfilePhotos(Integer user_id) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public File getFile(String file_id) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public TelegramRequestResult setWebhook(URL webhook_url, InputFile certificate) { if (api_key == null || "".equals(api_key)) { return new TelegramRequestResult<>(false, "Token not valid", null); } else { HashMap parameters = new HashMap(); if (webhook_url != null) { parameters.put("url", webhook_url); } String url = Utility.buildAPIUrl(this.api_key, TelegramBot.API_OPERATION_SET_WEBHOOK, parameters); TelegramRequestResult<JSONObject> result = JSONHelper.readJsonFromUrl(url); if (result.isRequestOk()) { JSONObject r = result.get(); if (r.getBoolean("ok") == true) { Boolean res = r.getBoolean("result"); if (res == true) return new TelegramRequestResult<>(true, null, r.getString("description")); else return new TelegramRequestResult<>(false, r.getString("description"), null); } else { return new TelegramRequestResult<>(false, "ERR_CODE: " + result.get().getString("error_code") + " " + result.get().getString("description"), null); } } else { return new TelegramRequestResult<>(false, result.getError(), null); } } } @Override public TelegramRequestResult removeWebhook() { return setWebhook(null, null); } //<editor-fold defaultstate="collapsed" desc=" Private Methods "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ private TelegramRequestResult<Message> sendMessageUniversal(Integer chat_id, String text, String parse_mode, Boolean disable_web_page_preview, Integer reply_to_message_id, Object reply_markup) { if (api_key == null || "".equals(api_key)) { return new TelegramRequestResult<>(false, "Token not valid", null); } else if (reply_markup != null && !reply_markup.getClass().getName().equals(ReplyKeyboardMarkup.class.getName()) && !reply_markup.getClass().getName().equals(ReplyKeyboardHide.class.getName()) && !reply_markup.getClass().getName().equals(ForceReply.class.getName())) { return new TelegramRequestResult<>(false, "reply_markup not valid", null); } else { Message msg = null; if (chat_id == null || text == null) return new TelegramRequestResult<>(false, "chat_id and text required", null); else { HashMap<String, String> params = new HashMap(); params.put("chat_id", chat_id.toString()); params.put("text", text); if (disable_web_page_preview != null) params.put("disable_web_page_preview", disable_web_page_preview.toString()); if (parse_mode != null) params.put("parse_mode", parse_mode); if (reply_to_message_id != null) params.put("reply_to_message_id", reply_to_message_id.toString()); if (reply_markup != null) { params.put("reply_markup", new JSONObject(reply_markup).toString()); } String url = Utility.buildAPIUrl(this.api_key, TelegramBot.API_OPERATION_SEND_MESSAGE, params); TelegramRequestResult<JSONObject> result = JSONHelper.readJsonFromUrl(url); if (result.isRequestOk()) { JSONObject r = result.get(); if (r.getBoolean("ok") == true) { msg = (Message) Utility.castJsonToObject(r.getJSONObject("result").toString(), TelegramBot.TYPE_MESSAGE); if (msg != null) return new TelegramRequestResult<>(true, null, msg); else return new TelegramRequestResult<>(false, "No result", null); } else { return new TelegramRequestResult<>(false, result.get().getString("description"), null); } } else { return new TelegramRequestResult<>(false, result.getError(), null); } } } } private TelegramRequestResult<Message> sendPhotoUniversal(Integer chat_id, Object photo, String caption, Integer reply_to_message_id, Object reply_markup) { if (api_key == null || "".equals(api_key)) { return new TelegramRequestResult<>(false, "Token not valid", null); } else if (reply_markup != null && !reply_markup.getClass().getName().equals(ReplyKeyboardMarkup.class.getName()) && !reply_markup.getClass().getName().equals(ReplyKeyboardHide.class.getName()) && !reply_markup.getClass().getName().equals(ForceReply.class.getName())) { return new TelegramRequestResult<>(false, "reply_markup not valid", null); } else { Message msg = null; if (chat_id == null || photo == null) return new TelegramRequestResult<>(false, "chat_id and photo required", null); else { if (photo.getClass().toString().equals(InputFile.class.toString())) { HashMap<String, String> params = new HashMap(); params.put("chat_id", chat_id.toString()); if (caption != null) params.put("caption", caption); if (reply_to_message_id != null) params.put("reply_to_message_id", reply_to_message_id.toString()); if (reply_markup != null) { params.put("reply_markup", new JSONObject(reply_markup).toString()); } String url = Utility.buildAPIUrl(this.api_key, TelegramBot.API_OPERATION_SEND_FOTO, params); TelegramRequestResult<JSONObject> result = JSONHelper.readJsonFromUrl(url, (InputFile) photo); if (result.isRequestOk()) { JSONObject r = result.get(); if (r.getBoolean("ok") == true) { msg = (Message) Utility.castJsonToObject(r.getJSONObject("result").toString(), TelegramBot.TYPE_MESSAGE); if (msg != null) return new TelegramRequestResult<>(true, null, msg); else return new TelegramRequestResult<>(false, "No result", null); } else { return new TelegramRequestResult<>(false, result.get().getString("description"), null); } } else { return new TelegramRequestResult<>(false, result.getError(), null); } } else { return new TelegramRequestResult<>(false, "Not yet implemented", null); } } } } //</editor-fold> }