Skip to content

canemacchina/telegram-bot-java-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

telegram-bot-java-api

telegram-bot-java-api is an unofficial JAVA implementation of Telegram Bot Api

Table of Contents

Getting Started

Get the library

At the time of writing this README, this library isn't published anywhere, so to use it:

  • download the latest release and use the source code as you will;
  • use Github as a package repository! Take look at JitPack, that can turn a Github repo into a maven repository or a Gradle repository!

Usage

To call a Telegram bot API method, first create a method executor:

MethodExecutor executor = new SimpleMethodExecutor("<BOT TOKEN HERE>");

Then, create the API method corresponding object and execute it!

Method method = //... construct an API Method
method.execute(executor);

(Almost) Every Method have a builder to construct it. The builder accept Telegram required parameters as constructors parameters, and then expose setters for optional arguments. See Telegram official documentation for a list of required and optional parameter for every method.

API Methods examples

getMe

GetMeMethod method = new GetMeMethod();
System.out.println(method.execute(executor));

sendMessage

SendMessageMethod method = new SendMessageMethod.Builder(<CHAT ID HERE>, <YOUR TEXT HERE>).build();
System.out.println(method.execute(executor));

forwardMessage

ForwardMessageMethod method = new ForwardMessageMethod(<CHAT ID HERE>, <FROM CHAT ID HERE>, <MESSAGE ID HERE>);
System.out.println(method.execute(executor));

sendPhoto

PhotoFile file = new PhotoFile(new File(<PATH TO MP3 FILE>));
SendPhotoMethod method = new SendPhotoMethod.Builder(<CHAT ID HERE>, file).build();
System.out.println(method.execute(executor));

or, if you want to resend a photo that is already on Telegram server:

SendPhotoMethod method = new SendPhotoMethod.Builder(<CHAT ID HERE>, <FILE ID HERE>).build();
System.out.println(method.execute(executor));

sendAudio

AudioFile file = new AudioFile(new File(<PATH TO MP3 FILE>));
SendAudioMethod method = new SendAudioMethod.Builder(<CHAT ID HERE>, file).build();
System.out.println(method.execute(executor));

or, if you want to resend an audio that is already on Telegram server:

SendAudioMethod method = new SendAudioMethod.Builder(<CHAT ID HERE>, <AUDIO ID HERE>).build();
System.out.println(method.execute(executor));

sendDocument

DocumentFile file = new DocumentFile(new File(<PATH TO MP3 FILE>));
SendDocumentMethod method = new SendDocumentMethod.Builder(<CHAT ID HERE>, file).build();
System.out.println(method.execute(executor));

or, if you want to resend a document that is already on Telegram server:

SendDocumentMethod method = new SendDocumentMethod.Builder(<CHAT ID HERE>, <DOCUMENT ID HERE>).build();
System.out.println(method.execute(executor));

sendSticker

StikerFile file = new StikerFile(new File(<PATH TO MP3 FILE>));
SendStickerMethod method = new SendStickerMethod.Builder(<CHAT ID HERE>, file).build();
System.out.println(method.execute(executor));

or, if you want to resend a sticker that is already on Telegram server:

SendStickerMethod method = new SendStickerMethod.Builder(<CHAT ID HERE>, <STICKER ID HERE>).build();
System.out.println(method.execute(executor));

sendVideo

VideoFile file = new VideoFile(new File(<PATH TO MP3 FILE>));
SendVideoMethod method = new SendVideoMethod.Builder(<CHAT ID HERE>, file).build();
System.out.println(method.execute(executor));

or, if you want to resend a video that is already on Telegram server:

SendVideoMethod method = new SendVideoMethod.Builder(<CHAT ID HERE>, <VIDEO ID HERE>).build();
System.out.println(method.execute(executor));

sendVoice

VoiceFile file = new VoiceFile(new File(<PATH TO MP3 FILE>));
SendVoiceMethod method = new SendVoiceMethod.Builder(<CHAT ID HERE>, file).build();
System.out.println(method.execute(executor));

or, if you want to resend a voice that is already on Telegram server:

SendVoiceMethod method = new SendVoiceMethod.Builder(<CHAT ID HERE>, <VOICE ID HERE>).build();
System.out.println(method.execute(executor));

sendLocation

SendLocationMethod method = new SendLocationMethod.Builder(<CHAT ID HERE>, <LAT>, <LONG>).build();
System.out.println(method.execute(executor));

sendChatAction

SendChatActionMethod method = new SendChatActionMethod(<CHAT ID HERE>, SendChatActionMethod.Actions.<TYPE>);
System.out.println(method.execute(executor));

getUserProfilePhotos

GetUserProfilePhotosMethod method = new GetUserProfilePhotosMethod.Builder(<USER ID HERE>).build();
System.out.println(method.execute(executor));

getUpdates

GetupdatesMethod method = new GetupdatesMethod.Builder().build();
Update[] updates = method.execute(executor);
for (Update update : updates) {
  System.out.println(update);
}

getFile

GetFileMethod method = new GetFileMethod(fileId);;
System.out.println(method.execute(executor));

Reply Markup

When a method support a Reply Keyboard Markup, you can construct it with the Method builder. So, for example:

// Create a keyboard with 2 row of buttons
//that will be hide as soon as the user use it:
ReplyMarkup m = new ReplyKeyboardMarkup.Builder()
  .row("A", "B")
  .row("C", "D")
  .setOneTimeKeyboard()
  .build();
// Construct SendMessageMethod and attach the keyboard:
SendMessageMethod method = new SendMessageMethod.Builder(<CHAT ID HERE>, <YOUR TEXT HERE>)
  .setReplyMarkup(m)
  .build();

TO DO

Javadocs. As soon as possible, I'll write some Javadoc, I promise!!

License

MIT. See the LICENSE file for more details.

About

Unofficial JAVA implementation of Telegram Bot Api

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages