PageRenderTime 62ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/de/bots/TelegramAPI.java

https://gitlab.com/plotnikau/telebotapi
Java | 251 lines | 198 code | 47 blank | 6 comment | 13 complexity | b3eadf364cccfef9909616d36baa8731 MD5 | raw file
  1. package de.bots;
  2. import de.bots.model.*;
  3. import de.bots.model.request.SendLocationRequest;
  4. import de.bots.model.request.SendMessageRequest;
  5. import de.bots.model.request.SendVenueRequest;
  6. import de.bots.model.response.FileResponse;
  7. import de.bots.model.response.MessageResponse;
  8. import de.bots.model.response.UpdateResponse;
  9. import de.bots.model.response.UserResponse;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.core.io.FileSystemResource;
  12. import org.springframework.core.io.Resource;
  13. import org.springframework.http.HttpEntity;
  14. import org.springframework.http.HttpHeaders;
  15. import org.springframework.http.MediaType;
  16. import org.springframework.stereotype.Component;
  17. import org.springframework.util.LinkedMultiValueMap;
  18. import org.springframework.util.MultiValueMap;
  19. import org.springframework.web.client.RestTemplate;
  20. import org.springframework.web.util.UriComponentsBuilder;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.net.URI;
  24. import java.net.URL;
  25. import java.nio.file.Files;
  26. import java.nio.file.StandardCopyOption;
  27. @Component
  28. public class TelegramAPI {
  29. public enum TextMessageParseMode {
  30. MARKDOWN("Markdown"), HTML("HTML");
  31. private final String text;
  32. private TextMessageParseMode(final String text) {
  33. this.text = text;
  34. }
  35. @Override
  36. public String toString() {
  37. return text;
  38. }
  39. }
  40. public static boolean commandMatch(String messageText, String command) {
  41. if (messageText!=null && command!=null) {
  42. return messageText.toLowerCase().startsWith(command);
  43. } else return false;
  44. }
  45. public static boolean commandMatch(Update update, String command) {
  46. return commandMatch(update.getMessage().getText(), command);
  47. }
  48. private String apiBaseUrl;
  49. private RestTemplate restTemplate;
  50. @Autowired
  51. public TelegramAPI(BotProperties botProperties) {
  52. apiBaseUrl = "https://api.telegram.org/bot" + botProperties.getApiKey() + "/";
  53. }
  54. private RestTemplate getRestTemplate() {
  55. if (null == restTemplate) {
  56. restTemplate = new RestTemplate();
  57. }
  58. return restTemplate;
  59. }
  60. private URI getSendMessageURI() {
  61. return URI.create(apiBaseUrl + "sendMessage");
  62. }
  63. private URI getSendLocationURI() {
  64. return URI.create(apiBaseUrl + "sendLocation");
  65. }
  66. private URI getSendPhotoURI() {
  67. return URI.create(apiBaseUrl + "sendPhoto");
  68. }
  69. private URI getUpdatesURI() {
  70. return URI.create(apiBaseUrl + "getUpdates");
  71. }
  72. private URI getSendVenueURI() {
  73. return URI.create(apiBaseUrl + "sendVenue");
  74. }
  75. protected URI getFileURI() {
  76. return URI.create(apiBaseUrl + "getFile");
  77. }
  78. public UserResponse getMe() {
  79. return getRestTemplate().getForObject(URI.create(apiBaseUrl + "getMe"), UserResponse.class);
  80. }
  81. public UpdateResponse getUpdates() {
  82. return getRestTemplate().getForObject(getUpdatesURI(), UpdateResponse.class);
  83. }
  84. public UpdateResponse getUpdates(Long offset) {
  85. URI targetUrl = UriComponentsBuilder.fromUri(getUpdatesURI())
  86. .queryParam("offset", offset.toString())
  87. .queryParam("timeout", "5")
  88. .build()
  89. .toUri();
  90. return getRestTemplate().getForObject(targetUrl, UpdateResponse.class);
  91. }
  92. public FileResponse getFile(String file_id) {
  93. URI targetUrl = UriComponentsBuilder.fromUri(getUpdatesURI())
  94. .queryParam("offset", file_id)
  95. .build()
  96. .toUri();
  97. return getRestTemplate().getForObject(targetUrl, FileResponse.class);
  98. }
  99. public MessageResponse sendMessage(int chat_id, String text) {
  100. return sendMessageInternal(chat_id, text, null, false, 0, null);
  101. }
  102. public MessageResponse sendMessage(int chat_id, String text, TextMessageParseMode parse_mode) {
  103. return sendMessageInternal(chat_id, text, parse_mode, false, 0, null);
  104. }
  105. public MessageResponse sendMessage(int chat_id, String text, boolean disable_web_page_preview, int reply_to_message_id, ReplyMarkup reply_markup) {
  106. return sendMessageInternal(chat_id, text, null, disable_web_page_preview, reply_to_message_id, reply_markup);
  107. }
  108. public MessageResponse sendMessage(int chat_id, String text, TextMessageParseMode parse_mode, boolean disable_web_page_preview, int reply_to_message_id, ReplyMarkup reply_markup) {
  109. return sendMessageInternal(chat_id, text, parse_mode, disable_web_page_preview, reply_to_message_id, reply_markup);
  110. }
  111. private MessageResponse sendMessageInternal(int chat_id, String text, TextMessageParseMode parse_mode, boolean disable_web_page_preview, int reply_to_message_id, ReplyMarkup reply_markup) {
  112. SendMessageRequest request = new SendMessageRequest(chat_id, text, parse_mode, disable_web_page_preview, reply_to_message_id, reply_markup);
  113. return getRestTemplate().postForObject(getSendMessageURI(), request, MessageResponse.class);
  114. }
  115. public MessageResponse sendLocation(int chat_id, Location location) {
  116. return sendLocationInternal(chat_id, location, false, 0, null);
  117. }
  118. /*
  119. private MessageResponse sendLocationInternal(int chat_id, Location location, boolean disable_notification, int reply_to_message_id, ReplyMarkup reply_markup) {
  120. SendLocationRequest request = new SendLocationRequest(chat_id, location, disable_notification, reply_to_message_id, reply_markup);
  121. return getRestTemplate().postForObject(getSendLocationURI(), request, MessageResponse.class);
  122. }
  123. */
  124. private MessageResponse sendLocationInternal(int chat_id, Location location, boolean disable_notification, int reply_to_message_id, ReplyMarkup reply_markup) {
  125. SendLocationRequest request = new SendLocationRequest(chat_id, location, disable_notification, reply_to_message_id, reply_markup);
  126. RestTemplate rt = getRestTemplate();
  127. URI uri = getSendLocationURI();
  128. return rt.postForObject(uri, request, MessageResponse.class);
  129. }
  130. public MessageResponse sendVenue(int chat_id, Venue venue) {
  131. return sendVenueInternal(chat_id, venue, false, 0, null);
  132. }
  133. private MessageResponse sendVenueInternal(int chat_id, Venue venue, boolean disable_notification, int reply_to_message_id, ReplyMarkup reply_markup) {
  134. SendVenueRequest sendVenueRequest = new SendVenueRequest(chat_id, venue, disable_notification, reply_to_message_id, reply_markup);
  135. RestTemplate restTemplate = getRestTemplate();
  136. URI uri = getSendVenueURI();
  137. return restTemplate.postForObject(uri, sendVenueRequest, MessageResponse.class);
  138. }
  139. public MessageResponse sendPhoto(int chat_id, URL imageUrl, File tempFile) throws IOException {
  140. return sendPhotoInternal(chat_id, imageUrl, tempFile, null, 0, null);
  141. }
  142. public MessageResponse sendPhoto(int chat_id, URL imageUrl, File tempFile, String caption) throws IOException {
  143. return sendPhotoInternal(chat_id, imageUrl, tempFile, caption, 0, null);
  144. }
  145. public MessageResponse sendPhoto(int chat_id, URL imageUrl, File tempFile, String caption, int reply_to_message_id,
  146. ReplyKeyboardMarkup reply_markup) throws IOException {
  147. return sendPhotoInternal(chat_id, imageUrl, tempFile, caption, reply_to_message_id, reply_markup);
  148. }
  149. public MessageResponse sendPhoto(int chat_id, URL imageUrl, File tempFile, String caption, int reply_to_message_id,
  150. ReplyKeyboardHide reply_markup) throws IOException {
  151. return sendPhotoInternal(chat_id, imageUrl, tempFile, caption, reply_to_message_id, reply_markup);
  152. }
  153. public MessageResponse sendPhoto(int chat_id, URL imageUrl, File tempFile, String caption, int reply_to_message_id,
  154. ForceReply reply_markup) throws IOException {
  155. return sendPhotoInternal(chat_id, imageUrl, tempFile, caption, reply_to_message_id, reply_markup);
  156. }
  157. public MessageResponse resendPhoto(int chatId, String fileId, String caption) throws IOException {
  158. return resendPhotoInternal(chatId, fileId, caption, 0, null);
  159. }
  160. private MessageResponse sendPhotoInternal(int chat_id, URL imageUrl, File tempFile, String caption,
  161. int reply_to_message_id, Object reply_markup) throws IOException {
  162. HttpEntity<Object> request = createImageRequest(chat_id, imageUrl, tempFile, caption, reply_to_message_id,
  163. reply_markup);
  164. return getRestTemplate().postForObject(getSendPhotoURI(), request, MessageResponse.class);
  165. }
  166. private MessageResponse resendPhotoInternal(int chat_id, String fileId, String caption,
  167. int reply_to_message_id, Object reply_markup) throws IOException {
  168. HttpEntity<Object> request = createImageRequest(chat_id, fileId, caption, reply_to_message_id, reply_markup);
  169. return getRestTemplate().postForObject(getSendPhotoURI(), request, MessageResponse.class);
  170. }
  171. private HttpEntity<Object> createImageRequest(int chat_id, URL imageUrl, File tempFile, String caption,
  172. int reply_to_message_id, Object reply_markup) throws IOException {
  173. Files.copy(imageUrl.openStream(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
  174. Resource photoResource = new FileSystemResource(tempFile);
  175. MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>();
  176. data.add("chat_id", chat_id);
  177. data.add("photo", photoResource);
  178. data.add("caption", caption);
  179. if (reply_to_message_id != 0) {
  180. data.add("reply_to_message_id", reply_to_message_id);
  181. }
  182. if (reply_markup != null) {
  183. data.add("reply_markup", reply_markup);
  184. }
  185. HttpHeaders headers = new HttpHeaders();
  186. headers.setContentType(MediaType.MULTIPART_FORM_DATA);
  187. return new HttpEntity<Object>(data, headers);
  188. }
  189. private HttpEntity<Object> createImageRequest(int chat_id, String file_id, String caption, int reply_to_message_id, Object reply_markup) throws IOException {
  190. MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>();
  191. data.add("chat_id", chat_id);
  192. data.add("photo", file_id);
  193. data.add("caption", caption);
  194. if (reply_to_message_id != 0) {
  195. data.add("reply_to_message_id", reply_to_message_id);
  196. }
  197. if (reply_markup != null) {
  198. data.add("reply_markup", reply_markup);
  199. }
  200. HttpHeaders headers = new HttpHeaders();
  201. headers.setContentType(MediaType.MULTIPART_FORM_DATA);
  202. return new HttpEntity<Object>(data, headers);
  203. }
  204. }