Browse Source

Merge pull request #7 from rubenlagus/dev

Dev
master
Ruben Bermudez 11 years ago
parent
commit
ce1e270fd1
  1. 116
      src/main/java/org/telegram/SenderHelper.java
  2. 28
      src/main/java/org/telegram/api/Audio.java
  3. 69
      src/main/java/org/telegram/api/Voice.java
  4. 18
      src/main/java/org/telegram/methods/SendAudio.java
  5. 58
      src/main/java/org/telegram/methods/SendPhoto.java
  6. 72
      src/main/java/org/telegram/methods/SendVideo.java
  7. 31
      src/main/java/org/telegram/methods/SendVoice.java
  8. 9
      src/main/java/org/telegram/updateshandlers/WeatherHandlers.java

116
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");
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");
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(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);
} 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);
}
}
}

28
src/main/java/org/telegram/api/Audio.java

@ -6,8 +6,8 @@ import org.json.JSONObject;
/**
* @author Ruben Bermudez
* @version 1.0
* @brief This object represents an audio file (voice note)
* @date 20 of June of 2015
* @brief This object represents an audio file
* @date 16 of July of 2015
*/
public class Audio {
@ -23,6 +23,12 @@ public class Audio {
public static final String FILESIZE_FIELD = "file_size";
@JsonProperty(FILESIZE_FIELD)
private Integer fileSize; ///< Optional. File size
public static final String TITLE_FIELD = "title";
@JsonProperty(TITLE_FIELD)
private String title; ///< Optional. Title of the audio as defined by sender or by audio tags
public static final String PERFORMER_FIELD = "performer";
@JsonProperty(PERFORMER_FIELD)
private String performer; ///< Optional. Performer of the audio as defined by sender or by audio tags
public Audio() {
super();
@ -34,6 +40,8 @@ public class Audio {
this.duration = jsonObject.getInt(DURATION_FIELD);
this.mimeType = jsonObject.optString(MIMETYPE_FIELD, "");
this.fileSize = jsonObject.optInt(FILESIZE_FIELD, 0);
this.title = jsonObject.optString(TITLE_FIELD, "");
this.performer = jsonObject.optString(PERFORMER_FIELD, "");
}
public String getFileId() {
@ -67,4 +75,20 @@ public class Audio {
public void setFileSize(Integer fileSize) {
this.fileSize = fileSize;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPerformer() {
return performer;
}
public void setPerformer(String performer) {
this.performer = performer;
}
}

69
src/main/java/org/telegram/api/Voice.java

@ -0,0 +1,69 @@
package org.telegram.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.json.JSONObject;
/**
* @author Ruben Bermudez
* @version 1.0
* @brief This object represents a voice note
* @date 16 of July of 2015
*/
public class Voice {
public static final String FILEID_FIELD = "file_id";
@JsonProperty(FILEID_FIELD)
private String fileId; ///< Unique identifier for this file
public static final String DURATION_FIELD = "duration";
@JsonProperty(DURATION_FIELD)
private Integer duration; ///< Integer Duration of the audio in seconds as defined by sender
public static final String MIMETYPE_FIELD = "mime_type";
@JsonProperty(MIMETYPE_FIELD)
private String mimeType; ///< Optional. MIME type of the file as defined by sender
public static final String FILESIZE_FIELD = "file_size";
@JsonProperty(FILESIZE_FIELD)
private Integer fileSize; ///< Optional. File size
public Voice() {
super();
}
public Voice(JSONObject jsonObject) {
super();
this.fileId = jsonObject.getString(FILEID_FIELD);
this.duration = jsonObject.getInt(DURATION_FIELD);
this.mimeType = jsonObject.optString(MIMETYPE_FIELD, "");
this.fileSize = jsonObject.optInt(FILESIZE_FIELD, 0);
}
public String getFileId() {
return fileId;
}
public void setFileId(String fileId) {
this.fileId = fileId;
}
public Integer getDuration() {
return duration;
}
public void setDuration(Integer duration) {
this.duration = duration;
}
public String getMimeType() {
return mimeType;
}
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
public Integer getFileSize() {
return fileSize;
}
public void setFileSize(Integer fileSize) {
this.fileSize = fileSize;
}
}

18
src/main/java/org/telegram/methods/SendAudio.java

@ -6,10 +6,16 @@ import org.telegram.api.ReplyKeyboard;
* @author Ruben Bermudez
* @version 1.0
* @brief Use this method to send audio files,
* if you want Telegram clients to display the file as a playable voice message.
* For this to work, your audio must be in an .ogg file encoded with OPUS
* (other formats may be sent as Document). On success, the sent Message is returned.
* @date 20 of June of 2015
* Use this method to send audio files, if you want Telegram clients to display them in the music player.
* Your audio must be in an .mp3 format. On success, the sent Message is returned.
* Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future.
*
* @note For backward compatibility, when both fields title and description are empty and mime-type of the sent
* file is not audio/mpeg, file is sent as playable voice message.
* In this case, your audio must be in an .ogg file encoded with OPUS.
* This will be removed in the future. You need to use sendVoice method instead.
*
* @date 16 of July of 2015
*/
public class SendAudio {
public static final String PATH = "sendaudio";
@ -22,6 +28,10 @@ public class SendAudio {
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
public static final String PERFOMER_FIELD = "performer";
private String performer; ///< Optional. Performer of sent audio
public static final String TITLE_FIELD = "title";
private String title; ///< Optional. Title of sent audio
public SendAudio() {
super();

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;
}
}

31
src/main/java/org/telegram/methods/SendVoice.java

@ -0,0 +1,31 @@
package org.telegram.methods;
import org.telegram.api.ReplyKeyboard;
/**
* @author Ruben Bermudez
* @version 1.0
* @brief Use this method to send voice notes, if you want Telegram clients to display
* the file as a playable voice message.
* For this to work, your audio must be in an .ogg file encoded with OPUS
* (other formats may be sent as Audio or Document).
* @date 16 of July of 2015
*/
public class SendVoice {
public static final String PATH = "sendvoice";
public static final String CHATID_FIELD = "chat_id";
private Integer chatId; ///< Unique identifier for the message recepient — User or GroupChat id
public static final String AUDIO_FIELD = "audio";
private String audio; ///< Audio file to send. file_id as String to resend an audio that is already on the Telegram servers
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
public static final String DURATION_FIELD = "duration";
private Integer duration; ///< Optional. Duration of sent audio in seconds
public SendVoice() {
super();
}
}

9
src/main/java/org/telegram/updateshandlers/WeatherHandlers.java

@ -104,6 +104,9 @@ public class WeatherHandlers implements UpdatesCallback {
private static void handleIncomingMessage(Message message) {
final int state = DatabaseManager.getInstance().getWeatherState(message.getFrom().getId(), message.getChatId());
final String language = DatabaseManager.getInstance().getUserWeatherOptions(message.getFrom().getId())[0];
if (message.isGroupMessage() && message.hasText() && isCommandForOther(message.getText())) {
return;
}
switch(state) {
case MAINMENU:
messageOnMainMenu(message, language);
@ -138,6 +141,12 @@ public class WeatherHandlers implements UpdatesCallback {
}
}
private static boolean isCommandForOther(String text) {
boolean isSimpleCommand = text.equals("/start") || text.equals("/help");
boolean isCommandForMe = text.equals("/start@weatherbot") || text.equals("/help@weatherbot");
return text.startsWith("/") && !isSimpleCommand && !isCommandForMe;
}
// endregion Incoming messages handlers
// region Alerts Menu Option selected

Loading…
Cancel
Save