Browse Source

1. Sendvideo and sendPhoto helpers

master
Rubenlagu 11 years ago
committed by Rubenlagus
parent
commit
04bee09041
  1. 114
      src/main/java/org/telegram/SenderHelper.java
  2. 58
      src/main/java/org/telegram/methods/SendPhoto.java
  3. 72
      src/main/java/org/telegram/methods/SendVideo.java

114
src/main/java/org/telegram/SenderHelper.java

@ -115,26 +115,96 @@ public class SenderHelper {
}
}
public static void SendWebhook(String webHookURL, String botToken) {
public static void SendPhoto(SendPhoto sendPhoto, String botToken) {
try {
CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
String url = Constants.BASEURL + botToken + "/" + SetWebhook.PATH;
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = Constants.BASEURL + botToken + "/" + SendPhoto.PATH;
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-type", "application/x-www-form-urlencoded");
httppost.addHeader("charset", "UTF-8");
if (sendPhoto.isNewPhoto()) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(SendPhoto.CHATID_FIELD, sendPhoto.getChatId().toString());
builder.addBinaryBody(SendPhoto.PHOTO_FIELD, new File(sendPhoto.getPhoto()), ContentType.APPLICATION_OCTET_STREAM, sendPhoto.getPhotoName());
if (sendPhoto.getReplayMarkup() != null) {
builder.addTextBody(SendPhoto.REPLYMARKUP_FIELD, sendPhoto.getReplayMarkup().toJson().toString());
}
if (sendPhoto.getReplayToMessageId() != null) {
builder.addTextBody(SendPhoto.REPLYTOMESSAGEID_FIELD, sendPhoto.getReplayToMessageId().toString());
}
if (sendPhoto.getCaption() != null) {
builder.addTextBody(SendPhoto.CAPTION_FIELD, sendPhoto.getCaption());
}
HttpEntity multipart = builder.build();
httppost.setEntity(multipart);
} else {
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair(SetWebhook.URL_FIELD, webHookURL));
nameValuePairs.add(new BasicNameValuePair(SendPhoto.CHATID_FIELD, sendPhoto.getChatId().toString()));
nameValuePairs.add(new BasicNameValuePair(SendPhoto.PHOTO_FIELD, sendPhoto.getPhoto()));
if (sendPhoto.getReplayMarkup() != null) {
nameValuePairs.add(new BasicNameValuePair(SendPhoto.REPLYMARKUP_FIELD, sendPhoto.getReplayMarkup().toString()));
}
if (sendPhoto.getReplayToMessageId() != null) {
nameValuePairs.add(new BasicNameValuePair(SendPhoto.REPLYTOMESSAGEID_FIELD, sendPhoto.getReplayToMessageId().toString()));
}
if (sendPhoto.getCaption() != null) {
nameValuePairs.add(new BasicNameValuePair(SendPhoto.CAPTION_FIELD, sendPhoto.getCaption()));
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
CloseableHttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
String responseContent = EntityUtils.toString(buf, "UTF-8");
}
CloseableHttpResponse response = httpClient.execute(httppost);
} catch (IOException e) {
log.error(e);
}
}
public static void SendVideo(SendVideo sendVideo, String botToken) {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = Constants.BASEURL + botToken + "/" + SendVideo.PATH;
HttpPost httppost = new HttpPost(url);
if (sendVideo.isNewVideo()) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(SendVideo.CHATID_FIELD, sendVideo.getChatId().toString());
builder.addBinaryBody(SendVideo.VIDEO_FIELD, new File(sendVideo.getVideo()), ContentType.APPLICATION_OCTET_STREAM, sendVideo.getVideoName());
if (sendVideo.getReplayMarkup() != null) {
builder.addTextBody(SendVideo.REPLYMARKUP_FIELD, sendVideo.getReplayMarkup().toJson().toString());
}
if (sendVideo.getReplayToMessageId() != null) {
builder.addTextBody(SendVideo.REPLYTOMESSAGEID_FIELD, sendVideo.getReplayToMessageId().toString());
}
if (sendVideo.getCaption() != null) {
builder.addTextBody(SendVideo.CAPTION_FIELD, sendVideo.getCaption());
}
if (sendVideo.getDuration() != null) {
builder.addTextBody(SendVideo.DURATION_FIELD, sendVideo.getDuration().toString());
}
HttpEntity multipart = builder.build();
httppost.setEntity(multipart);
} else {
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair(SendVideo.CHATID_FIELD, sendVideo.getChatId().toString()));
nameValuePairs.add(new BasicNameValuePair(SendVideo.VIDEO_FIELD, sendVideo.getVideo()));
if (sendVideo.getReplayMarkup() != null) {
nameValuePairs.add(new BasicNameValuePair(SendVideo.REPLYMARKUP_FIELD, sendVideo.getReplayMarkup().toString()));
}
if (sendVideo.getReplayToMessageId() != null) {
nameValuePairs.add(new BasicNameValuePair(SendVideo.REPLYTOMESSAGEID_FIELD, sendVideo.getReplayToMessageId().toString()));
}
if (sendVideo.getCaption() != null) {
nameValuePairs.add(new BasicNameValuePair(SendVideo.CAPTION_FIELD, sendVideo.getCaption()));
}
if (sendVideo.getDuration() != null) {
nameValuePairs.add(new BasicNameValuePair(SendVideo.DURATION_FIELD, sendVideo.getDuration().toString()));
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
}
CloseableHttpResponse response = httpClient.execute(httppost);
} catch (IOException e) {
log.error(e);
}
}
public static void sendSticker(SendSticker sendSticker, String botToken) {
@ -178,4 +248,26 @@ public class SenderHelper {
}
}
public static void SendWebhook(String webHookURL, String botToken) {
try {
CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
String url = Constants.BASEURL + botToken + "/" + SetWebhook.PATH;
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-type", "application/x-www-form-urlencoded");
httppost.addHeader("charset", "UTF-8");
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair(SetWebhook.URL_FIELD, webHookURL));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
CloseableHttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
String responseContent = EntityUtils.toString(buf, "UTF-8");
} catch (IOException e) {
log.error(e);
}
}
}

58
src/main/java/org/telegram/methods/SendPhoto.java

@ -22,8 +22,66 @@ public class SendPhoto {
public static final String REPLYMARKUP_FIELD = "reply_markup";
private ReplyKeyboard replayMarkup; ///< Optional. JSON-serialized object for a custom reply keyboard
private boolean isNewPhoto; ///< True if the photo must be uploaded from a file, file if it is a fileId
private String photoName; ///< Name of the photo
public SendPhoto() {
super();
}
public Integer getChatId() {
return chatId;
}
public void setChatId(Integer chatId) {
this.chatId = chatId;
}
public String getPhoto() {
return photo;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public Integer getReplayToMessageId() {
return replayToMessageId;
}
public void setReplayToMessageId(Integer replayToMessageId) {
this.replayToMessageId = replayToMessageId;
}
public ReplyKeyboard getReplayMarkup() {
return replayMarkup;
}
public void setReplayMarkup(ReplyKeyboard replayMarkup) {
this.replayMarkup = replayMarkup;
}
public boolean isNewPhoto() {
return isNewPhoto;
}
public String getPhotoName() {
return photoName;
}
public void setPhoto(String photo) {
this.photo = photo;
this.isNewPhoto = false;
}
public void setNewPhoto(String photo, String photoName) {
this.photo = photo;
this.isNewPhoto = true;
this.photoName = photoName;
}
}

72
src/main/java/org/telegram/methods/SendVideo.java

@ -18,11 +18,81 @@ public class SendVideo {
public static final String VIDEO_FIELD = "video";
private String video; ///< Video to send. file_id as String to resend a video that is already on the Telegram servers
public static final String DURATION_FIELD = "duration";
private String duration; ///< Optional. Duration of sent video in seconds
private Integer duration; ///< Optional. Duration of sent video in seconds
public static final String CAPTION_FIELD = "caption";
private String caption; ///< OptionaL. Video caption (may also be used when resending videos by file_id).
public static final String REPLYTOMESSAGEID_FIELD = "reply_to_message_id";
private Integer replayToMessageId; ///< Optional. If the message is a reply, ID of the original message
public static final String REPLYMARKUP_FIELD = "reply_markup";
private ReplyKeyboard replayMarkup; ///< Optional. JSON-serialized object for a custom reply keyboard
private boolean isNewVideo; ///< True to upload a new video, false to use a fileId
private String videoName; ///< Name of the video
public SendVideo() {
super();
}
public Integer getChatId() {
return chatId;
}
public void setChatId(Integer chatId) {
this.chatId = chatId;
}
public String getVideo() {
return video;
}
public Integer getDuration() {
return duration;
}
public void setDuration(Integer duration) {
this.duration = duration;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public Integer getReplayToMessageId() {
return replayToMessageId;
}
public void setReplayToMessageId(Integer replayToMessageId) {
this.replayToMessageId = replayToMessageId;
}
public ReplyKeyboard getReplayMarkup() {
return replayMarkup;
}
public void setReplayMarkup(ReplyKeyboard replayMarkup) {
this.replayMarkup = replayMarkup;
}
public boolean isNewVideo() {
return isNewVideo;
}
public String getVideoName() {
return videoName;
}
public void setVideo(String video) {
this.video = video;
this.isNewVideo = false;
}
public void setNewVideo(String video, String videoName) {
this.video = video;
this.isNewVideo = true;
this.videoName = videoName;
}
}

Loading…
Cancel
Save