Minor howto fixes

This commit is contained in:
Rubenlagus 2016-08-07 05:46:41 +02:00
parent c9f77855c7
commit b1b13efbbb

View File

@ -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 ##### Table of Contents
[Preperations](#preperations) [Preperations](#preperations)
@ -13,44 +13,39 @@ So, you just wanna program a own Telegram bot with @rubenlagus library TelegramB
<a name="preperations"/> <a name="preperations"/>
## Preperations ## 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. 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 ;) 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)
<a name="lets_code"/> <a name="lets_code"/>
## Let's code! ## 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 ```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)```
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)
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: At the end it could look like this:
```java ```java
public class BotConfig { public class BotConfig {
public static final String BOT_USERNAME = "echoBot"; public static final String BOT_USERNAME = "echoBot";
public static final String BOT_TOKEN = "{you secret bot token that you got from BotFather}";
public static final String BOT_TOKEN = "{you secret bot token you got from the BotFather}";
} }
``` ```
@ -60,7 +55,6 @@ After it, return these static variables like this one:
public String getBotToken() { public String getBotToken() {
return BotConfig.BOT_TOKEN; return BotConfig.BOT_TOKEN;
} }
``` ```
The last method could look like this: The last method could look like this:
@ -68,17 +62,15 @@ The last method could look like this:
```java ```java
@Override @Override
public void onUpdateReceived(Update update) { public void onUpdateReceived(Update update) {
//check if the update has a message //check if the update has a message
if(update.hasMessage()){ if(update.hasMessage()){
Message message = update.getMessage(); Message message = update.getMessage();
//check if the message has text. it could also contain for example a location ( message.hasLocation() ) //check if the message has text. it could also contain for example a location ( message.hasLocation() )
if(message.hasText()){ if(message.hasText()){
//create an object that contains the information to send back the message
//create a object that contains the information to send back the message
SendMessage sendMessageRequest = new SendMessage(); SendMessage sendMessageRequest = new SendMessage();
sendMessageRequest.setChatId(message.getChatId().toString()); //who should get the message? the sender from which we got the message... sendMessageRequest.setChatId(message.getChatId().toString()); //who should get from the message the sender that sent it.
sendMessageRequest.setText("you said: " + message.getText()); sendMessageRequest.setText("you said: " + message.getText());
try { try {
sendMessage(sendMessageRequest); //at the end, so some magic and send the message ;) sendMessage(sendMessageRequest); //at the end, so some magic and send the message ;)
@ -87,22 +79,20 @@ The last method could look like this:
} }
} }
} }
} }
``` ```
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 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 ```java
public static void main(String[] args) { public static void main(String[] args) {
TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
try { try {
telegramBotsApi.registerBot(new MyProjectHandler()); telegramBotsApi.registerBot(new MyProjectHandler());
@ -125,20 +115,14 @@ public static void main(String[] args) {
//check if the update has a message //check if the update has a message
if (update.hasMessage()) { if (update.hasMessage()) {
Message message = update.getMessage(); Message message = update.getMessage();
//check if we got some photos //check if we got some photos
if (message.getPhoto() != null) { 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. * 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 * And with their id's we can download them from Telegram servers
*/ */
for (int i = 0; i < photos.size(); i++) { for (int i = 0; i < photos.size(); i++) {
GetFile getFileRequest = new GetFile(); GetFile getFileRequest = new GetFile();
getFileRequest.setFileId(photos.get(0).getFileId()); getFileRequest.setFileId(photos.get(0).getFileId());
try { try {
@ -158,16 +142,10 @@ public static void main(String[] args) {
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
//TODO: so some error handling //TODO: so some error handling
} }
} }
} }
} }
} }
``` ```
Question: <a name="question_how_to_send_photos"/> Question: <a name="question_how_to_send_photos"/>
<b>How to send photos?</b> <b>How to send photos?</b>
@ -177,20 +155,14 @@ public static void main(String[] args) {
```java ```java
@Override @Override
public void onUpdateReceived(Update update) { public void onUpdateReceived(Update update) {
//check if the update has a message //check if the update has a message
if(update.hasMessage()){ if(update.hasMessage()){
Message message = update.getMessage(); Message message = update.getMessage();
//check if the message has text. it could also contain for example a location ( message.hasLocation() ) //check if the message has text. it could also contain for example a location ( message.hasLocation() )
if(message.hasText()){ if(message.hasText()){
if(message.getText().equals("/wiki")){ if(message.getText().equals("/wiki")){
SendPhoto sendPhotoRequest = new SendPhoto(); SendPhoto sendPhotoRequest = new SendPhoto();
sendPhotoRequest.setChatId(message.getChatId().toString()); sendPhotoRequest.setChatId(message.getChatId().toString());
//path: String, photoName: String //path: String, photoName: String
sendPhotoRequest.setNewPhoto("/home/marcel/Downloads/potd_wikipedia.jpg", "Good Friday.jpg"); // sendPhotoRequest.setNewPhoto("/home/marcel/Downloads/potd_wikipedia.jpg", "Good Friday.jpg"); //
try { try {
@ -201,12 +173,9 @@ public static void main(String[] args) {
* e.printStackTrace(); * e.printStackTrace();
*/ */
} }
}
} }
} }
}
} }
``` ```
@ -219,7 +188,6 @@ public static void main(String[] args) {
//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(); SendMessage sendMessageRequest = this.selectLanguage();
sendMessageRequest.setChatId(message.getChatId().toString()); sendMessageRequest.setChatId(message.getChatId().toString());
sendMessageRequest.setText("Change language"); sendMessageRequest.setText("Change language");
@ -227,11 +195,10 @@ sendMessageRequest.setText("Change language");
sendMessage(sendMessageRequest); 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 ```java
public static List<KeyboardRow> selectLanguage(Message message){ public static List<KeyboardRow> selectLanguage(Message message){
/* changed from "List<List<String>>" to "List<KeyboardRow>" /* changed from "List<List<String>>" to "List<KeyboardRow>"
* Now we have just a one dimension array. Like a stack or something like that * Now we have just a one dimension array. Like a stack or something like that
*/ */
@ -261,7 +228,6 @@ public static List<KeyboardRow> selectLanguage(Message message){
rows.add(row); rows.add(row);
return rows; return rows;
} }
``` ```