1- ## Simple Java API for [ Telegram Bots] [ 1 ]
1+ ## Java API for [ Telegram Bots] [ 1 ]
22
3- Full support of all Bot API functions
3+ Full support of all Bot API 2.0 functions
44
55Download
66-------
7- Download the latest version via Gradle:
7+ Gradle:
88``` groovy
9- compile 'com.github.pengrad:java-telegram-bot-api:1.3.3 '
9+ compile 'com.github.pengrad:java-telegram-bot-api:2.0.0 '
1010```
11- or Maven:
11+ Maven:
1212``` xml
1313<dependency >
1414 <groupId >com.github.pengrad</groupId >
1515 <artifactId >java-telegram-bot-api</artifactId >
16- <version >1.3.3 </version >
16+ <version >2.0.0 </version >
1717</dependency >
1818```
19-
19+ JAR-files:
20+ https://oss.sonatype.org/content/repositories/releases/com/github/pengrad/java-telegram-bot-api/
2021
2122Usage
2223-------
2324#### Create bot
2425``` java
2526TelegramBot bot = TelegramBotAdapter . build(" BOT_TOKEN" );
2627```
27- ** All bot methods have the same signature as original ones.**
28- ** You can pass ` null ` as any _ Optional_ parameter**
28+ #### Execute methods
29+ ``` java
30+ // sync
31+ BaseResponse response = bot. execute(request);
32+
33+ // async
34+ bot. execute(request, new Callback () {
35+ @Override
36+ public void onResponse (BaseRequest request , BaseResponse response ) {
37+ }
38+ @Override
39+ public void onFailure (BaseRequest request , IOException e ) {
40+ }
41+ });
42+ ```
43+ ** All request methods have the same names as original ones.**
44+ ** Required params should be passed in constructor.**
45+ ** Optional params can be added in chains.**
2946
3047#### Send message
3148``` java
32- // short syntax
33- bot. sendMessage(chatId, " short message sending" );
34- bot. sendMessage(" @mychannel" , " short message sending" );
49+ bot. execute(new SendMessage (chatId, " message text" ));
3550
36- // full
37- bot. sendMessage(
38- chatId, // chat_id
39- " Hello _italic_ *bold*!" , // text
40- ParseMode . Markdown , // Markdown text or null
41- false , // disable_web_page_preview
42- replyMessageId, // reply_to_message_id
43- new ReplyKeyboardMarkup (new String []{" ok" , " cancel" }). oneTimeKeyboard(true )); // keyboard
51+ bot. execute(
52+ new SendMessage (chatId, " message <b>bold</b> text" )
53+ .parseMode(ParseMode . HTML )
54+ .replyMarkup(new ReplyKeyboardMarkup (new String []{" button 1" , " button 2" }))
55+ );
56+
57+ bot. execute(new SendMessage (" @mychannel" , " message text" ), new Callback<SendMessage , SendResponse > () {
58+ @Override
59+ public void onResponse (SendMessage request , SendResponse response ) {
60+ }
61+ @Override
62+ public void onFailure (SendMessage request , IOException e ) {
63+ }
64+ });
4465```
4566#### Keyboards
4667``` java
@@ -56,67 +77,12 @@ Keyboard replyKeyboardHide = new ReplyKeyboardHide(); // new ReplyKeyboardHide(i
5677```
5778#### Getting response to sending methods
5879``` java
59- SendResponse sendResponse = bot. sendMessage( chatId, " short message sending " );
80+ SendResponse sendResponse = bot. execute( new SendMessage ( chatId, " text " ) );
6081Message message = sendResponse. message();
6182```
62- #### Send files
63- 3 options to sending files
64- ``` java
65- // as String, resending existing file
66- String fileId;
67-
68- // as File
69- InputFile . photo(file);
70- InputFile . audio(file);
71- InputFile . video(file);
72- InputFile . voice(file);
73- new InputFile (" text/plain" , file);
74-
75- // as byte[]
76- InputFileBytes . photo(bytes);
77- InputFileBytes . audio(bytes);
78- InputFileBytes . video(bytes);
79- InputFileBytes . voice(bytes);
80- new InputFileBytes (" text/plain" , bytes, " my-file.txt" );
81- ```
82- Examples
83- ``` java
84- // Photo
85- String fileId = // resending fileId
86- bot. sendPhoto(chatId, fileId, " caption" , null , null );
87- bot. sendPhoto(chatId, InputFile . photo(imageFile), " caption" , replyMessageId, new ForceReply ());
88- bot. sendPhoto(chatId, InputFileBytes . photo(bytes), " caption" , null , new ReplyKeyboardHide ());
89-
90- // Same options for all types
91- // Audio
92- bot. sendAudio(chatId, InputFile . audio(audioFile), duration, performer, title, null , null );
93-
94- // Video
95- bot. sendVideo(chatId, InputFile . video(videoFile), duration, " caption" , null , null );
96-
97- // Document
98- bot. sendDocument(chatId, new InputFile (" text/plain" , docFile), null , null );
99-
100- // Sticker
101- bot. sendSticker(chatId, stickerId, null , null );
102-
103- // Voice
104- bot. sendVoice(chatId, InputFileBytes . voice(bytes), duration, null , null );
105- ```
106- #### Send chat action
107- ``` java
108- bot. sendChatAction(chatId, ChatAction . find_location);
109- bot. sendChatAction(chatId, ChatAction . typing);
110- bot. sendChatAction(chatId, ChatAction . record_audio);
111- bot. sendChatAction(chatId, ChatAction . record_video);
112- bot. sendChatAction(" @channel" , ChatAction . upload_audio);
113- bot. sendChatAction(" @channel" , ChatAction . upload_document);
114- bot. sendChatAction(" @channel" , ChatAction . upload_photo);
115- bot. sendChatAction(" @channel" , ChatAction . upload_video);
116- ```
11783#### Get updates
11884``` java
119- GetUpdatesResponse updatesResponse = bot. getUpdates(offset, limit, timeout);
85+ GetUpdatesResponse updatesResponse = bot. execute( new GetUpdates () . limit( 0 ) . offset( 0 ) . timeout( 0 ) );
12086List<Update > updates = updatesResponse. updates();
12187...
12288Message message = update. message()
@@ -129,15 +95,14 @@ Message message = update.message();
12995```
13096#### Get file
13197``` java
132- GetFileResponse getFileResponse = bot. getFile( " fileId" );
98+ GetFileResponse getFileResponse = bot. execute( new GetFile ( " fileId" ) );
13399File file = getFileResponse. file(); // com.pengrad.telegrambot.model.File
134100file. fileId();
135101file. filePath(); // relative path
136102file. fileSize();
137103```
138104To get downloading link as ` https://api.telegram.org/file/bot<token>/<file_path> `
139105``` java
140- String fullPath = bot. getFullFilePath(" fileId" );
141106String fullPath = bot. getFullFilePath(file); // com.pengrad.telegrambot.model.File
142107```
143108#### Inline mode
@@ -164,9 +129,16 @@ InlineQueryResult r5 = new InlineQueryResultVideo("id", "videoUrl", InlineQueryR
164129```
165130Send query
166131``` java
167- bot. answerInlineQuery( inlineQuery. id(), r1, r2, r3, r4, r5);
132+ bot. execute( new AnswerInlineQuery ( inlineQuery. id(), r1, r2, r3, r4, r5) );
168133// or full
169- bot. answerInlineQuery(inlineQuery. id(), new InlineQueryResult []{r1, r2, r3, r4, r5}, cacheTime, isPersonal, nextOffset);
134+ bot. execute(
135+ new AnswerInlineQuery (inlineQuery. id(), new InlineQueryResult []{r1, r2, r3, r4, r5})
136+ .cacheTime(cacheTime)
137+ .isPersonal(isPersonal)
138+ .nextOffset(" offset" )
139+ .switchPmParameter(" pmParam" )
140+ .switchPmText(" pmText" )
141+ );
170142```
171143
172144
0 commit comments