From c9f77855c7e728e41fafa78c1aeaddf41d948ac3 Mon Sep 17 00:00:00 2001 From: Clevero Date: Sun, 7 Aug 2016 05:29:03 +0200 Subject: [PATCH 1/6] Update HOWTO (#124) * Create HOW_TO.md * Rename HOW_TO.md to HOWTO.md * small changes correct linking, spelling and add TelegramLongPollingCommandBot --- HOWTO.md | 275 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 275 insertions(+) create mode 100644 HOWTO.md diff --git a/HOWTO.md b/HOWTO.md new file mode 100644 index 00000000..bbd64b2f --- /dev/null +++ b/HOWTO.md @@ -0,0 +1,275 @@ +So, you just wanna program a own Telegram bot with @rubenlagus library TelegramBots? Then I'm going to show you how to start ;) + +##### Table of Contents +[Preperations](#preperations) +[Let's code!](#lets_code) +[FAQ](#faq) +       1. [How to get picture?](#question_how_to_get_picture) +       2. [How to send photos?](#question_how_to_send_photos) +       3. [How to use custom keyboards?](#question_how_to_use_custom_keyboards) +       4. [How can I compile my project?](#question_how_to_compile) + + + + +## Preperations +First you need to download the latest .jar from the Release site [here](https://github.com/rubenlagus/TelegramBots/releases). You can choose between Jar dependencies and wthout. If you don't know what to choose, we recommend to download the full jar with all dependencies ;) + +Next, you need to integrate it into your project. + +Create a new project for your bot, in the example below we are showing you how to do it in eclipse. But of course, you are free to code in whatever IDE you want ;) + +If you don't know how to include a external .jar into your Eclipse project, maybe [this](https://www.youtube.com/watch?v=VWnfHkBgO1I) video is helpfull for you + +More information on how to use it with Gradly or Maven, can you find here [here](https://jitpack.io/#rubenlagus/TelegramBots/v2.3.3.4) + + + +## Let's code! +Create a new class that is extending one of the following + +```TelegramLongPollingBot``` -> bot is asking the Telegram servers continuously if new updates are available + +```TelegramWebhookBot``` -> our bot is "called" from the Telegram servers when updates are available + +```TelegramLongPollingCommandBot``` -> simply like TelegramLongPollingBot, but based around the idea of Commands + + +Due to the fact that the TelegramLongPollingBot is a little bit less complicated than the others, are we going to work with him in the following. + + +Extend ```TelegramLongPollingBot``` with one of your own classes. If we want that the bot can work normally, we must implement the following methods: ```getBotUsername():String```, ```getBotToken():String``` und ```onUpdateReceived(update: Update)``` + +The first two methods are really easy to implement. Jus create a new class that contains all our informations for the bot (username, token and maybe in the future some database information) + +At the end it could look like this: + +```java +public class BotConfig { + + public static final String BOT_USERNAME = "echoBot"; + + public static final String BOT_TOKEN = "{you secret bot token you got from the BotFather}"; + +} +``` + +After it, return these static variables like this one: +```java +@Override + public String getBotToken() { + return BotConfig.BOT_TOKEN; + } + +``` + +The last method could look like this: + +```java +@Override + public void onUpdateReceived(Update update) { + + //check if the update has a message + if(update.hasMessage()){ + Message message = update.getMessage(); + + //check if the message has text. it could also contain for example a location ( message.hasLocation() ) + if(message.hasText()){ + + //create a object that contains the information to send back the message + SendMessage sendMessageRequest = new SendMessage(); + sendMessageRequest.setChatId(message.getChatId().toString()); //who should get the message? the sender from which we got the message... + sendMessageRequest.setText("you said: " + message.getText()); + try { + sendMessage(sendMessageRequest); //at the end, so some magic and send the message ;) + } catch (TelegramApiException e) { + //do some error handling + } + } + } + + } +``` +The princip is rather easy, we have an ```update```, we see that it contains a text -> we create a Send*** object, fill it up with all necessary infos (user/chatId, text) and fire it up + +If you want to send also other types of media (such as photos or audio), then check out our FAQ at the end if this HOWTO. Also please check out the [TelegramBotsExample](https://github.com/rubenlagus/TelegramBotsExample) repo. It contains usefull informations on how to use the lib from @rubenlagus. + +If you have questions that are not handled here or in the example repo, than feel free to open a new Issue :P +In any case, you can reach us in our chat at [our group chat](https://telegram.me/JavaBotsApi) ;) + +But to be in context: our bot is not ready, yet. It lacks of a used way to the tell lib that we have a super cool new UpdateHandler written, you remember? 😏 + + +```java +public static void main(String[] args) { + + TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); + try { + telegramBotsApi.registerBot(new MyProjectHandler()); + } catch (TelegramApiException e) { + BotLogger.error(LOGTAG, e); + } + } +``` + + +## FAQ + + Question: + How to get a picture? + Answer: A ```onUpdateReceived()``` Method that just downloads every Photo that users send to the bot could look like this: + +```java + @Override + public void onUpdateReceived(Update update) { + //check if the update has a message + if(update.hasMessage()){ + Message message = update.getMessage(); + + //check if we got some photos + if(message.getPhoto() != null){ + + + /* + * Just save our received photos in a list. At this point, we do not really have the photos. We have just their id. + * And with their id's we can download them from telegram servers + */ + + for(int i = 0; i < photos.size(); i++){ + + GetFile getFileRequest = new GetFile(); + + getFileRequest.setFileId(photos.get(0).getFileId()); + try { + + //we send a request with our fileId to get our filePath. + File file = getFile(getFileRequest); + + /* + * After that, we can now start to save them on our local machine. + * Please have a look on the API specification, on how to download the files with their filepaths you just got in the code above + * https://core.telegram.org/bots/api#file + * + * Just replace with File.getFilePath(); + */ + + // In this example, we just print here the filePaths + System.out.println(file.getFilePath()); + } catch (TelegramApiException e) { + //TODO: so some error handling + } + + } + + } + + } + + + } + +``` + Question: + How to send photos? + Answer: + + +```java + @Override + public void onUpdateReceived(Update update) { + + //check if the update has a message + if(update.hasMessage()){ + Message message = update.getMessage(); + + //check if the message has text. it could also contain for example a location ( message.hasLocation() ) + if(message.hasText()){ + + if(message.getText().equals("/wiki")){ + + SendPhoto sendPhotoRequest = new SendPhoto(); + sendPhotoRequest.setChatId(message.getChatId().toString()); + + + //path: String, photoName: String + sendPhotoRequest.setNewPhoto("/home/marcel/Downloads/potd_wikipedia.jpg", "Good Friday.jpg"); // + try { + sendPhoto(sendPhotoRequest); + } catch (TelegramApiException e) { + /* + * Do some error handling + * e.printStackTrace(); + */ + } + + } + + } + } + + } +``` + + This method uploads the photo every time the user send the bot /wiki. Telegram stores the files we upload on their server. And if next time someone wants to retrieve THE SAME photo we uploaded some time ago, you should use instead of SendPhoto.setNewPhoto() the SendPhoto.setPhoto() method. This method has just one parameter, file_id. + + Question: + How to use custom keyboards? + Answer: +```java + //first, create a normal SendMessage object. Our CutsomKeyboard is attached to this object. But don't forget to assign a text. Otherwise the user get's no message +SendMessage sendMessageRequest = this.selectLanguage(); + + +sendMessageRequest.setChatId(message.getChatId().toString()); +sendMessageRequest.setText("Change language"); + +//ready top takeoff! +sendMessage(sendMessageRequest); +``` + +The selectLanguage() method could look like this (you also could put the code of the extra methode in your main() ) + +```java +public static List selectLanguage(Message message){ + + /* changed from "List>" to "List" + * Now we have just a one dimension array. Like a stack or something like that + */ + List rows = new ArrayList(); + rows.add(ReplyKeyboardFabricator.getHeader(message)); + + //Instead of a list that collects our strings, or "buttons" for each row, now we have a own Object for that + KeyboardRow row = new KeyboardRow(); + row.add(LocalisationService.getInstance().getString("change_language", DatabaseManager.getInstance().getUserLanguage(EchoHandler.getUserId(message)))); + rows.add(row); + + row = new KeyboardRow(); + row.add("🇦🇹 Deutsch (Östereich)"); + rows.add(row); + + //I just reused the object above. Of cource you could also create a new Keyboardrow for each real row + row = new KeyboardRow(); + row.add("🇩🇪 Deutsch (Deutschland)"); + rows.add(row); + + row = new KeyboardRow(); + row.add("🇧🇷 Português"); + rows.add(row); + + row = new KeyboardRow(); + row.add("🇺🇸 English (United States)"); + rows.add(row); + + return rows; + + } +``` + +For more example on this, please have a look at [this](https://github.com/rubenlagus/TelegramBotsExample/blob/master/src/main/java/org/telegram/updateshandlers/WeatherHandlers.java) bot in the example repo + + + Question: How can I compile my project? + Answer: This is just one way, how you can compile it (here with maven). The example below below is compiling the TelegramBotsExample repo. + [![asciicast](https://asciinema.org/a/4np9i2u9onuitkg287ism23kj.png)](https://asciinema.org/a/4np9i2u9onuitkg287ism23kj) + + From b1b13efbbbfcb801ec3e0e02454e1b5d6a46dfe4 Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Sun, 7 Aug 2016 05:46:41 +0200 Subject: [PATCH 2/6] Minor howto fixes --- HOWTO.md | 284 ++++++++++++++++++++++++------------------------------- 1 file changed, 125 insertions(+), 159 deletions(-) diff --git a/HOWTO.md b/HOWTO.md index bbd64b2f..50857c4b 100644 --- a/HOWTO.md +++ b/HOWTO.md @@ -1,4 +1,4 @@ -So, you just wanna program a own Telegram bot with @rubenlagus library TelegramBots? Then I'm going to show you how to start ;) +So, you just wanna program your own Telegram bot with @rubenlagus library TelegramBots? Then I'm going to show you how to start ;) ##### Table of Contents [Preperations](#preperations) @@ -13,103 +13,93 @@ So, you just wanna program a own Telegram bot with @rubenlagus library TelegramB ## Preperations -First you need to download the latest .jar from the Release site [here](https://github.com/rubenlagus/TelegramBots/releases). You can choose between Jar dependencies and wthout. If you don't know what to choose, we recommend to download the full jar with all dependencies ;) +First you need to download the latest .jar from the Release site [here](https://github.com/rubenlagus/TelegramBots/releases). You can choose between Jar with or without dependencies. If you don't know which one to choose, we recommend to download the full jar with all dependencies ;) Next, you need to integrate it into your project. Create a new project for your bot, in the example below we are showing you how to do it in eclipse. But of course, you are free to code in whatever IDE you want ;) -If you don't know how to include a external .jar into your Eclipse project, maybe [this](https://www.youtube.com/watch?v=VWnfHkBgO1I) video is helpfull for you +If you don't know how to include a external .jar into your Eclipse project, maybe [this](https://www.youtube.com/watch?v=VWnfHkBgO1I) video is helpful for you -More information on how to use it with Gradly or Maven, can you find here [here](https://jitpack.io/#rubenlagus/TelegramBots/v2.3.3.4) +More information on how to use it with Gradle or Maven, can be found here [here](https://jitpack.io/#rubenlagus/TelegramBots) ## Let's code! -Create a new class that is extending one of the following +Create a new class that extends one of the following -```TelegramLongPollingBot``` -> bot is asking the Telegram servers continuously if new updates are available +```TelegramLongPollingBot``` -> bot is asking Telegram servers continuously if new updates are available -```TelegramWebhookBot``` -> our bot is "called" from the Telegram servers when updates are available +```TelegramWebhookBot``` -> our bot is "called" from Telegram servers when updates are available ```TelegramLongPollingCommandBot``` -> simply like TelegramLongPollingBot, but based around the idea of Commands +Due to the fact that the TelegramLongPollingBot is a little bit less complicated than the others, we are going to work with him in this example. -Due to the fact that the TelegramLongPollingBot is a little bit less complicated than the others, are we going to work with him in the following. +Extend ```TelegramLongPollingBot``` with one of your own classes. If we want that the bot can work normally, we must implement the following methods: ```getBotUsername():String```, ```getBotToken():String``` and ```onUpdateReceived(update: Update)``` - -Extend ```TelegramLongPollingBot``` with one of your own classes. If we want that the bot can work normally, we must implement the following methods: ```getBotUsername():String```, ```getBotToken():String``` und ```onUpdateReceived(update: Update)``` - -The first two methods are really easy to implement. Jus create a new class that contains all our informations for the bot (username, token and maybe in the future some database information) +The first two methods are really easy to implement. Just create a new class that contains all the information for the bot (username, token and maybe in the future some database information) At the end it could look like this: ```java public class BotConfig { - - public static final String BOT_USERNAME = "echoBot"; - - public static final String BOT_TOKEN = "{you secret bot token you got from the BotFather}"; - + public static final String BOT_USERNAME = "echoBot"; + public static final String BOT_TOKEN = "{you secret bot token that you got from BotFather}"; } ``` After it, return these static variables like this one: ```java @Override - public String getBotToken() { - return BotConfig.BOT_TOKEN; - } - +public String getBotToken() { + return BotConfig.BOT_TOKEN; +} ``` The last method could look like this: ```java @Override - public void onUpdateReceived(Update update) { - - //check if the update has a message - if(update.hasMessage()){ - Message message = update.getMessage(); - - //check if the message has text. it could also contain for example a location ( message.hasLocation() ) - if(message.hasText()){ - - //create a object that contains the information to send back the message - SendMessage sendMessageRequest = new SendMessage(); - sendMessageRequest.setChatId(message.getChatId().toString()); //who should get the message? the sender from which we got the message... - sendMessageRequest.setText("you said: " + message.getText()); - try { - sendMessage(sendMessageRequest); //at the end, so some magic and send the message ;) - } catch (TelegramApiException e) { - //do some error handling - } - } - } +public void onUpdateReceived(Update update) { + //check if the update has a message + if(update.hasMessage()){ + Message message = update.getMessage(); + //check if the message has text. it could also contain for example a location ( message.hasLocation() ) + if(message.hasText()){ + //create an object that contains the information to send back the message + SendMessage sendMessageRequest = new SendMessage(); + sendMessageRequest.setChatId(message.getChatId().toString()); //who should get from the message the sender that sent it. + sendMessageRequest.setText("you said: " + message.getText()); + try { + sendMessage(sendMessageRequest); //at the end, so some magic and send the message ;) + } catch (TelegramApiException e) { + //do some error handling + } } + } +} ``` -The princip is rather easy, we have an ```update```, we see that it contains a text -> we create a Send*** object, fill it up with all necessary infos (user/chatId, text) and fire it up +The principle is rather easy, we have an ```update```, we see that it contains a text -> we create a Send*** object, fill it up with all necessary infos (user/chatId, text) and fire it up -If you want to send also other types of media (such as photos or audio), then check out our FAQ at the end if this HOWTO. Also please check out the [TelegramBotsExample](https://github.com/rubenlagus/TelegramBotsExample) repo. It contains usefull informations on how to use the lib from @rubenlagus. +If you want to send also other types of media (such as photos or audio), then check out our FAQ at the end if this HOWTO. Also please check out the [TelegramBotsExample](https://github.com/rubenlagus/TelegramBotsExample) repo. It contains useful information on how to use the lib from @rubenlagus. If you have questions that are not handled here or in the example repo, than feel free to open a new Issue :P -In any case, you can reach us in our chat at [our group chat](https://telegram.me/JavaBotsApi) ;) +In any case, you can reach us at [our Telegram group chat](https://telegram.me/JavaBotsApi) ;) -But to be in context: our bot is not ready, yet. It lacks of a used way to the tell lib that we have a super cool new UpdateHandler written, you remember? 😏 +But to be in context: our bot is not ready, yet. It lacks a way to tell the library that we have a super cool new UpdateHandler written, you remember? 😏 ```java public static void main(String[] args) { - - TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); - try { - telegramBotsApi.registerBot(new MyProjectHandler()); - } catch (TelegramApiException e) { - BotLogger.error(LOGTAG, e); - } + TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); + try { + telegramBotsApi.registerBot(new MyProjectHandler()); + } catch (TelegramApiException e) { + BotLogger.error(LOGTAG, e); } +} ``` @@ -120,54 +110,42 @@ public static void main(String[] args) { Answer: A ```onUpdateReceived()``` Method that just downloads every Photo that users send to the bot could look like this: ```java - @Override - public void onUpdateReceived(Update update) { - //check if the update has a message - if(update.hasMessage()){ - Message message = update.getMessage(); +@Override +public void onUpdateReceived(Update update) { + //check if the update has a message + if (update.hasMessage()) { + Message message = update.getMessage(); + //check if we got some photos + if (message.getPhoto() != null) { + /* + * Just save our received photos in a list. At this point, we do not really have the photos. We have just their id. + * And with their id's we can download them from Telegram servers + */ + for (int i = 0; i < photos.size(); i++) { + GetFile getFileRequest = new GetFile(); + getFileRequest.setFileId(photos.get(0).getFileId()); + try { - //check if we got some photos - if(message.getPhoto() != null){ + //we send a request with our fileId to get our filePath. + File file = getFile(getFileRequest); + /* + * After that, we can now start to save them on our local machine. + * Please have a look on the API specification, on how to download the files with their filepaths you just got in the code above + * https://core.telegram.org/bots/api#file + * + * Just replace with File.getFilePath(); + */ - /* - * Just save our received photos in a list. At this point, we do not really have the photos. We have just their id. - * And with their id's we can download them from telegram servers - */ - - for(int i = 0; i < photos.size(); i++){ - - GetFile getFileRequest = new GetFile(); - - getFileRequest.setFileId(photos.get(0).getFileId()); - try { - - //we send a request with our fileId to get our filePath. - File file = getFile(getFileRequest); - - /* - * After that, we can now start to save them on our local machine. - * Please have a look on the API specification, on how to download the files with their filepaths you just got in the code above - * https://core.telegram.org/bots/api#file - * - * Just replace with File.getFilePath(); - */ - - // In this example, we just print here the filePaths - System.out.println(file.getFilePath()); - } catch (TelegramApiException e) { - //TODO: so some error handling - } - - } - - } - - } - - - } - + // In this example, we just print here the filePaths + System.out.println(file.getFilePath()); + } catch (TelegramApiException e) { + //TODO: so some error handling + } + } + } + } +} ``` Question: How to send photos? @@ -175,39 +153,30 @@ public static void main(String[] args) { ```java - @Override - public void onUpdateReceived(Update update) { - - //check if the update has a message - if(update.hasMessage()){ - Message message = update.getMessage(); - - //check if the message has text. it could also contain for example a location ( message.hasLocation() ) - if(message.hasText()){ - - if(message.getText().equals("/wiki")){ - - SendPhoto sendPhotoRequest = new SendPhoto(); - sendPhotoRequest.setChatId(message.getChatId().toString()); - - - //path: String, photoName: String - sendPhotoRequest.setNewPhoto("/home/marcel/Downloads/potd_wikipedia.jpg", "Good Friday.jpg"); // - try { - sendPhoto(sendPhotoRequest); - } catch (TelegramApiException e) { - /* - * Do some error handling - * e.printStackTrace(); - */ - } - - } - - } - } - - } +@Override +public void onUpdateReceived(Update update) { + //check if the update has a message + if(update.hasMessage()){ + Message message = update.getMessage(); + //check if the message has text. it could also contain for example a location ( message.hasLocation() ) + if(message.hasText()){ + if(message.getText().equals("/wiki")){ + SendPhoto sendPhotoRequest = new SendPhoto(); + sendPhotoRequest.setChatId(message.getChatId().toString()); + //path: String, photoName: String + sendPhotoRequest.setNewPhoto("/home/marcel/Downloads/potd_wikipedia.jpg", "Good Friday.jpg"); // + try { + sendPhoto(sendPhotoRequest); + } catch (TelegramApiException e) { + /* + * Do some error handling + * e.printStackTrace(); + */ + } + } + } + } +} ``` This method uploads the photo every time the user send the bot /wiki. Telegram stores the files we upload on their server. And if next time someone wants to retrieve THE SAME photo we uploaded some time ago, you should use instead of SendPhoto.setNewPhoto() the SendPhoto.setPhoto() method. This method has just one parameter, file_id. @@ -216,10 +185,9 @@ public static void main(String[] args) { How to use custom keyboards? Answer: ```java - //first, create a normal SendMessage object. Our CutsomKeyboard is attached to this object. But don't forget to assign a text. Otherwise the user get's no message +//first, create a normal SendMessage object. Our CutsomKeyboard is attached to this object. But don't forget to assign a text. Otherwise the user get's no message SendMessage sendMessageRequest = this.selectLanguage(); - sendMessageRequest.setChatId(message.getChatId().toString()); sendMessageRequest.setText("Change language"); @@ -227,42 +195,40 @@ sendMessageRequest.setText("Change language"); sendMessage(sendMessageRequest); ``` -The selectLanguage() method could look like this (you also could put the code of the extra methode in your main() ) +The selectLanguage() method could look like this (you also could put the code of the extra method in your main() ) ```java public static List selectLanguage(Message message){ + /* changed from "List>" to "List" + * Now we have just a one dimension array. Like a stack or something like that + */ + List rows = new ArrayList(); + rows.add(ReplyKeyboardFabricator.getHeader(message)); - /* changed from "List>" to "List" - * Now we have just a one dimension array. Like a stack or something like that - */ - List rows = new ArrayList(); - rows.add(ReplyKeyboardFabricator.getHeader(message)); + //Instead of a list that collects our strings, or "buttons" for each row, now we have a own Object for that + KeyboardRow row = new KeyboardRow(); + row.add(LocalisationService.getInstance().getString("change_language", DatabaseManager.getInstance().getUserLanguage(EchoHandler.getUserId(message)))); + rows.add(row); - //Instead of a list that collects our strings, or "buttons" for each row, now we have a own Object for that - KeyboardRow row = new KeyboardRow(); - row.add(LocalisationService.getInstance().getString("change_language", DatabaseManager.getInstance().getUserLanguage(EchoHandler.getUserId(message)))); - rows.add(row); + row = new KeyboardRow(); + row.add("🇦🇹 Deutsch (Östereich)"); + rows.add(row); - row = new KeyboardRow(); - row.add("🇦🇹 Deutsch (Östereich)"); - rows.add(row); + //I just reused the object above. Of cource you could also create a new Keyboardrow for each real row + row = new KeyboardRow(); + row.add("🇩🇪 Deutsch (Deutschland)"); + rows.add(row); - //I just reused the object above. Of cource you could also create a new Keyboardrow for each real row - row = new KeyboardRow(); - row.add("🇩🇪 Deutsch (Deutschland)"); - rows.add(row); + row = new KeyboardRow(); + row.add("🇧🇷 Português"); + rows.add(row); - row = new KeyboardRow(); - row.add("🇧🇷 Português"); - rows.add(row); + row = new KeyboardRow(); + row.add("🇺🇸 English (United States)"); + rows.add(row); - row = new KeyboardRow(); - row.add("🇺🇸 English (United States)"); - rows.add(row); - - return rows; - - } + return rows; +} ``` For more example on this, please have a look at [this](https://github.com/rubenlagus/TelegramBotsExample/blob/master/src/main/java/org/telegram/updateshandlers/WeatherHandlers.java) bot in the example repo From b566ad9621b1df4c575a1fe2ae0ecde63e5c58cb Mon Sep 17 00:00:00 2001 From: Rumen Nikiforov Date: Sun, 7 Aug 2016 06:47:23 +0300 Subject: [PATCH 3/6] Fixing sending Files through InputStream causing exceptions. (#131) - Added proper methods to send Audio/Document/Photo/Sticker/Video/Voice through InputStream as it Telegram API requires file name to be specified. - Dropping not working methods that was using InputStream without name --- .../telegrambots/api/methods/send/SendAudio.java | 6 +++++- .../telegrambots/api/methods/send/SendDocument.java | 11 +++++------ .../telegrambots/api/methods/send/SendPhoto.java | 6 +++++- .../telegrambots/api/methods/send/SendSticker.java | 6 +++++- .../telegrambots/api/methods/send/SendVideo.java | 6 +++++- .../telegrambots/api/methods/send/SendVoice.java | 6 +++++- .../org/telegram/telegrambots/bots/AbsSender.java | 12 ++++++------ 7 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java index a450b5d0..e3e1afe8 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java @@ -4,6 +4,7 @@ import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; import java.io.File; import java.io.InputStream; +import java.util.Objects; /** * @author Ruben Bermudez @@ -111,7 +112,10 @@ public class SendAudio { return this; } - public SendAudio setNewAudio(InputStream inputStream) { + public SendAudio setNewAudio(String audioName, InputStream inputStream) { + Objects.requireNonNull(audioName, "audioName cannot be null!"); + Objects.requireNonNull(inputStream, "inputStream cannot be null!"); + this.audioName = audioName; this.isNewAudio = true; this.newAudioStream = inputStream; return this; diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java index 9ab38fee..493e566f 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java @@ -4,6 +4,7 @@ import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; import java.io.File; import java.io.InputStream; +import java.util.Objects; /** * @author Ruben Bermudez @@ -93,12 +94,10 @@ public class SendDocument { return this; } - /** - * Use this method to set the document to a new file - * - * @param inputStream New document file - */ - public SendDocument setNewDocument(InputStream inputStream) { + public SendDocument setNewDocument(String documentName, InputStream inputStream) { + Objects.requireNonNull(documentName, "documentName cannot be null!"); + Objects.requireNonNull(inputStream, "inputStream cannot be null!"); + this.documentName = documentName; this.isNewDocument = true; this.newDocumentStream = inputStream; return this; diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java index ecb89cf7..d4839fa7 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java @@ -4,6 +4,7 @@ import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; import java.io.File; import java.io.InputStream; +import java.util.Objects; /** * @author Ruben Bermudez @@ -171,7 +172,10 @@ public class SendPhoto { return this; } - public SendPhoto setNewPhoto(InputStream inputStream) { + public SendPhoto setNewPhoto(String photoName, InputStream inputStream) { + Objects.requireNonNull(photoName, "photoName cannot be null!"); + Objects.requireNonNull(inputStream, "inputStream cannot be null!"); + this.photoName = photoName; this.newPhotoStream = inputStream; this.isNewPhoto = true; return this; diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java index 8a091729..2a06c9fb 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java @@ -4,6 +4,7 @@ import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; import java.io.File; import java.io.InputStream; +import java.util.Objects; /** * @author Ruben Bermudez @@ -130,7 +131,10 @@ public class SendSticker { return this; } - public SendSticker setNewSticker(InputStream inputStream) { + public SendSticker setNewSticker(String stickerName, InputStream inputStream) { + Objects.requireNonNull(stickerName, "stickerName cannot be null!"); + Objects.requireNonNull(inputStream, "inputStream cannot be null!"); + this.stickerName = stickerName; this.isNewSticker = true; this.newStickerStream = inputStream; return this; diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java index 393edf38..f354f503 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java @@ -4,6 +4,7 @@ import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; import java.io.File; import java.io.InputStream; +import java.util.Objects; /** * @author Ruben Bermudez @@ -205,7 +206,10 @@ public class SendVideo { return this; } - public SendVideo setNewVideo(InputStream inputStream) { + public SendVideo setNewVideo(String videoName, InputStream inputStream) { + Objects.requireNonNull(videoName, "videoName cannot be null!"); + Objects.requireNonNull(inputStream, "inputStream cannot be null!"); + this.videoName = videoName; this.isNewVideo = true; this.newVideoStream = inputStream; return this; diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java index 0c7fce91..3c8a865f 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java @@ -4,6 +4,7 @@ import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; import java.io.File; import java.io.InputStream; +import java.util.Objects; /** * @author Ruben Bermudez @@ -144,7 +145,10 @@ public class SendVoice { return this; } - public SendVoice setNewVoice(InputStream inputStream) { + public SendVoice setNewVoice(String voiceName, InputStream inputStream) { + Objects.requireNonNull(voiceName, "voiceName cannot be null!"); + Objects.requireNonNull(inputStream, "inputStream cannot be null!"); + this.voiceName = voiceName; this.isNewVoice = true; this.newVoiceStream = inputStream; return this; diff --git a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java index 2c1cd61b..a270898c 100644 --- a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java +++ b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java @@ -519,7 +519,7 @@ public abstract class AbsSender { if (sendDocument.getNewDocumentFile() != null) { builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, sendDocument.getNewDocumentFile()); } else if (sendDocument.getNewDocumentStream() != null) { - builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, sendDocument.getNewDocumentStream()); + builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, sendDocument.getNewDocumentStream(), ContentType.APPLICATION_OCTET_STREAM, sendDocument.getDocumentName()); } else { builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, new java.io.File(sendDocument.getDocument()), ContentType.APPLICATION_OCTET_STREAM, sendDocument.getDocumentName()); } @@ -585,7 +585,7 @@ public abstract class AbsSender { if (sendPhoto.getNewPhotoFile() != null) { builder.addBinaryBody(SendPhoto.PHOTO_FIELD, sendPhoto.getNewPhotoFile()); } else if (sendPhoto.getNewPhotoStream() != null) { - builder.addBinaryBody(SendPhoto.PHOTO_FIELD, sendPhoto.getNewPhotoStream()); + builder.addBinaryBody(SendPhoto.PHOTO_FIELD, sendPhoto.getNewPhotoStream(), ContentType.APPLICATION_OCTET_STREAM, sendPhoto.getPhotoName()); } else { builder.addBinaryBody(SendPhoto.PHOTO_FIELD, new java.io.File(sendPhoto.getPhoto()), ContentType.APPLICATION_OCTET_STREAM, sendPhoto.getPhotoName()); } @@ -651,7 +651,7 @@ public abstract class AbsSender { if (sendVideo.getNewVideoFile() != null) { builder.addBinaryBody(SendVideo.VIDEO_FIELD, sendVideo.getNewVideoFile()); } else if (sendVideo.getNewVideoStream() != null) { - builder.addBinaryBody(SendVideo.VIDEO_FIELD, sendVideo.getNewVideoStream()); + builder.addBinaryBody(SendVideo.VIDEO_FIELD, sendVideo.getNewVideoStream(), ContentType.APPLICATION_OCTET_STREAM, sendVideo.getVideoName()); } else { builder.addBinaryBody(SendVideo.VIDEO_FIELD, new java.io.File(sendVideo.getVideo()), ContentType.APPLICATION_OCTET_STREAM, sendVideo.getVideoName()); } @@ -736,7 +736,7 @@ public abstract class AbsSender { if (sendSticker.getNewStickerFile() != null) { builder.addBinaryBody(SendSticker.STICKER_FIELD, sendSticker.getNewStickerFile()); } else if (sendSticker.getNewStickerStream() != null) { - builder.addBinaryBody(SendSticker.STICKER_FIELD, sendSticker.getNewStickerStream()); + builder.addBinaryBody(SendSticker.STICKER_FIELD, sendSticker.getNewStickerStream(), ContentType.APPLICATION_OCTET_STREAM, sendSticker.getStickerName()); } else { builder.addBinaryBody(SendSticker.STICKER_FIELD, new java.io.File(sendSticker.getSticker()), ContentType.APPLICATION_OCTET_STREAM, sendSticker.getStickerName()); } @@ -804,7 +804,7 @@ public abstract class AbsSender { if (sendAudio.getNewAudioFile() != null) { builder.addBinaryBody(SendAudio.AUDIO_FIELD, sendAudio.getNewAudioFile()); } else if (sendAudio.getNewAudioStream() != null) { - builder.addBinaryBody(SendAudio.AUDIO_FIELD, sendAudio.getNewAudioStream()); + builder.addBinaryBody(SendAudio.AUDIO_FIELD, sendAudio.getNewAudioStream(), ContentType.APPLICATION_OCTET_STREAM, sendAudio.getAudioName()); } else { builder.addBinaryBody(SendAudio.AUDIO_FIELD, new java.io.File(sendAudio.getAudio()), ContentType.create("audio/mpeg"), sendAudio.getAudioName()); } @@ -893,7 +893,7 @@ public abstract class AbsSender { if (sendVoice.getNewVoiceFile() != null) { builder.addBinaryBody(SendVoice.VOICE_FIELD, sendVoice.getNewVoiceFile()); } else if (sendVoice.getNewVoiceStream() != null) { - builder.addBinaryBody(SendVoice.VOICE_FIELD, sendVoice.getNewVoiceStream()); + builder.addBinaryBody(SendVoice.VOICE_FIELD, sendVoice.getNewVoiceStream(), ContentType.APPLICATION_OCTET_STREAM, sendVoice.getVoiceName()); } else { builder.addBinaryBody(SendVoice.VOICE_FIELD, new java.io.File(sendVoice.getVoice()), ContentType.create("audio/ogg"), sendVoice.getVoiceName()); } From 78bbe0b2c65d14139ebb4a5fafd2f0752aa01b55 Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Sun, 7 Aug 2016 14:04:00 +0200 Subject: [PATCH 4/6] Closes #128 --- src/main/java/org/telegram/telegrambots/bots/AbsSender.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java index a270898c..b56131c3 100644 --- a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java +++ b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java @@ -550,7 +550,7 @@ public abstract class AbsSender { if (sendDocument.getCaption() != null) { nameValuePairs.add(new BasicNameValuePair(SendDocument.CAPTION_FIELD, sendDocument.getCaption())); } - if (sendDocument.getReplyToMessageId() != null) { + if (sendDocument.getDisableNotification() != null) { nameValuePairs.add(new BasicNameValuePair(SendDocument.DISABLENOTIFICATION_FIELD, sendDocument.getDisableNotification().toString())); } httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8)); From b039558c21f66d52a5f0cbf1049b493419cb7478 Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Sun, 7 Aug 2016 14:24:10 +0200 Subject: [PATCH 5/6] Closes #126 --- .../telegrambots/bots/TelegramLongPollingCommandBot.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java index e6d2b25f..d28d4947 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java @@ -50,7 +50,6 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB } /** - * function message filter. * Override this function in your bot implementation to filter messages with commands * * For example, if you want to prevent commands execution incoming from group chat: @@ -58,12 +57,13 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB * # return !message.getChat().isGroupChat(); * # * + * @note Default implementation doesn't filter anything * @param message Received message * @return true if the message must be ignored by the command bot and treated as a non command message, * false otherwise */ protected boolean filter(Message message) { - return true; + return false; } @Override From 5db05165166d9df702794cde3f9133a93472fcfd Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Sun, 7 Aug 2016 14:34:27 +0200 Subject: [PATCH 6/6] Update version 2.3.3.7 --- README.md | 4 +++- pom.xml | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d058aefe..7048c3b9 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Both ways are supported, but I recommend long polling method. ## Usage -Just import add the library to your project using [Maven, Gradle, ...](https://jitpack.io/#rubenlagus/TelegramBots/v2.3.3.6) or download the jar(including all dependencies) from [here](https://github.com/rubenlagus/TelegramBots/releases/tag/v2.3.3.6) +Just import add the library to your project using [Maven, Gradle, ...](https://jitpack.io/#rubenlagus/TelegramBots/v2.3.3.7) or download the jar(including all dependencies) from [here](https://github.com/rubenlagus/TelegramBots/releases/tag/v2.3.3.7) In order to use Long Polling mode, just create your own bot extending `org.telegram.telegrambots.bots.TelegramLongPollingBot`. @@ -46,6 +46,8 @@ Once done, you just need to create a `org.telegram.telegrambots.TelegramBotsApi` ``` +For detailed explanation, visite our [How To](HOWTO.md) (thanks Clevero) + ## Example bots Open them and send them */help* command to get some information about their capabilities: diff --git a/pom.xml b/pom.xml index e41e3a4f..c158fa39 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ jar org.telegram telegrambots - 2.3.3.6 + 2.3.3.7 Telegram Bots https://telegram.me/JavaBotsApi