public abstract class TelegramBot extends Object
| Constructor and Description |
|---|
TelegramBot(String botToken)
Convenience constructor for
TelegramBot(botToken, true) |
TelegramBot(String botToken,
boolean sendAsync)
Constructs a TelegramBot using the provided
botToken, If sendAsync is true,
the bot invokes all sendXXX methods asynchronously. |
| Modifier and Type | Method and Description |
|---|---|
ApiResponse<Message> |
forwardMessage(int chatId,
int fromChatId,
int messageId)
Forwards a message with ID
messageId from fromChatId to chatId. |
ApiResponse<User> |
getMe()
A simple method for testing your bot's auth token.
|
ApiResponse<UserProfilePhotos> |
getUserProfilePhotos(int userId) |
ApiResponse<UserProfilePhotos> |
getUserProfilePhotos(int userId,
OptionalArgs optionalArgs)
Returns a
UserProfilePhotos for a user. |
protected void |
notifyNewMessages(List<Message> messages)
This method is called by this class to process all new
Messages asynchronously. |
protected void |
onMessage(Message message)
This method is called when a new message has arrived.
|
protected void |
onStart() |
protected void |
onStop() |
protected ExecutorService |
provideExecutorService()
Instantiates and returns an
ExecutorService. |
ApiResponse<Message> |
replyTo(Message message,
String text)
Convenience method for
sendMessage(message.getChat().getId(), text, new OptionalArgs().replyToMessageId(message.getMessageId())) |
ApiResponse<Message> |
sendAudio(int chatId,
File audioFile) |
ApiResponse<Message> |
sendAudio(int chatId,
File audioFile,
OptionalArgs optionalArgs)
Use this method to send audio files, if you want Telegram clients to display them in the music player.
|
ApiResponse<Message> |
sendAudio(int chatId,
String audioFileId) |
ApiResponse<Message> |
sendAudio(int chatId,
String audioFileId,
OptionalArgs optionalArgs)
This version takes a file id instead of a
File, as a String argument. |
ApiResponse<Boolean> |
sendChatAction(int chatId,
ChatAction chatAction)
Use this method when you need to tell the user that something is happening on the bot's side.
|
ApiResponse<Message> |
sendDocument(int chatId,
File documentFile) |
ApiResponse<Message> |
sendDocument(int chatId,
File documentFile,
OptionalArgs optionalArgs)
Use this method to send general files.
|
ApiResponse<Message> |
sendDocument(int chatId,
String documentFileId) |
ApiResponse<Message> |
sendDocument(int chatId,
String documentFileId,
OptionalArgs optionalArgs)
This version takes a file id instead of a
File. |
ApiResponse<Message> |
sendLocation(int chatId,
float latitude,
float longitude) |
ApiResponse<Message> |
sendLocation(int chatId,
float latitude,
float longitude,
OptionalArgs optionalArgs)
Use this method to send point on the map.
|
ApiResponse<Message> |
sendMessage(int chatId,
String text) |
ApiResponse<Message> |
sendMessage(int chatId,
String text,
OptionalArgs optionalArgs)
Use this method to send text messages.
|
ApiResponse<Message> |
sendPhoto(int chatId,
File photoFile) |
ApiResponse<Message> |
sendPhoto(int chatId,
File photoFile,
OptionalArgs optionalArgs)
Use this method to send photos.
|
ApiResponse<Message> |
sendPhoto(int chatId,
String photoFileId) |
ApiResponse<Message> |
sendPhoto(int chatId,
String photoFileId,
OptionalArgs optionalArgs)
This version takes a file id instead of a
File |
ApiResponse<Message> |
sendSticker(int chatId,
File stickerFile) |
ApiResponse<Message> |
sendSticker(int chatId,
File stickerFile,
OptionalArgs optionalArgs)
Use this method to send .webp stickers.
|
ApiResponse<Message> |
sendSticker(int chatId,
String stickerFileId) |
ApiResponse<Message> |
sendSticker(int chatId,
String stickerFileId,
OptionalArgs optionalArgs)
This version takes a file id instead of a
File. |
ApiResponse<Message> |
sendVideo(int chatId,
File videoFile) |
ApiResponse<Message> |
sendVideo(int chatId,
File videoFile,
OptionalArgs optionalArgs)
Use this method to send video files,
Telegram clients support mp4 videos (other formats may be sent as Document (
sendDocument(int, File, OptionalArgs))). |
ApiResponse<Message> |
sendVideo(int chatId,
String videoFileId) |
ApiResponse<Message> |
sendVideo(int chatId,
String videoFileId,
OptionalArgs optionalArgs)
This version uses a file id rather than a
File |
ApiResponse<Message> |
sendVoice(int chatId,
File voiceFile) |
ApiResponse<Message> |
sendVoice(int chatId,
File voiceFile,
OptionalArgs optionalArgs)
Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message.
|
ApiResponse<Message> |
sendVoice(int chatId,
String voiceFileId)
This version uses a String rather than a File.
|
ApiResponse<Message> |
sendVoice(int chatId,
String voiceFileId,
OptionalArgs optionalArgs)
This version uses a String rather than a File
|
void |
start()
Starts the bot.
|
void |
stop()
Stops the bot and joins the polling
Thread. |
public TelegramBot(String botToken)
TelegramBot(botToken, true)public TelegramBot(String botToken, boolean sendAsync)
botToken, If sendAsync is true,
the bot invokes all sendXXX methods asynchronously.botToken - The token provided by @BotFathersendAsync - Whether this bot should invoke sendXXX methods asynchronously.public final void start()
ExecutorService by calling provideExecutorService().
If this instance is constructed with sendAsync set to true, it instantiates a asynchronous ApiRequestExecutor,
otherwise a synchronous version is used.
After this, a polling Thread is instantiated and the bot starts polling the Telegram API.protected void onStart()
public final void stop()
Thread.protected void onStop()
protected ExecutorService provideExecutorService()
ExecutorService.
By default, Executors.newCachedThreadPool() is used.
This method can safely be overridden to adjust this behaviour.
This method can safely be overridden to return null, but if you decide to do so, notifyNewMessages(List)
must be overridden to avoid a NPE.ExecutorServicepublic final ApiResponse<Message> forwardMessage(int chatId, int fromChatId, int messageId)
messageId from fromChatId to chatId.chatId - Unique identifier for the message recipient — User or GroupChat idfromChatId - Unique identifier for the chat where the original message was sent — User or GroupChat idmessageId - Unique message identifierApiResponse with the sent Messagepublic final ApiResponse<User> getMe()
User wrapped in a ApiResponsepublic final ApiResponse<UserProfilePhotos> getUserProfilePhotos(int userId)
getUserProfilePhotos(int, OptionalArgs)public final ApiResponse<UserProfilePhotos> getUserProfilePhotos(int userId, OptionalArgs optionalArgs)
UserProfilePhotos for a user.
For any optional arguments, refer to the Telegram documentation.userId - Unique identifier of the target useroptionalArgs - Any optional arguments for this methodUserProfilePhotosOptionalArgspublic final ApiResponse<Message> sendAudio(int chatId, File audioFile)
sendAudio(int, File, OptionalArgs)public final ApiResponse<Message> sendAudio(int chatId, File audioFile, OptionalArgs optionalArgs)
chatId - Unique identifier for the message recipient - User or GroupChat idaudioFile - Audio File to sendoptionalArgs - Any optional arguments for this methodMessagepublic final ApiResponse<Message> sendAudio(int chatId, String audioFileId)
sendAudio(int, String, OptionalArgs)public final ApiResponse<Message> sendAudio(int chatId, String audioFileId, OptionalArgs optionalArgs)
File, as a String argument.sendAudio(int, File, OptionalArgs)public final ApiResponse<Boolean> sendChatAction(int chatId, ChatAction chatAction)
chatId - Unique identifier for the message recipient - User or GroupChat id.chatAction - The target ChatActionpublic final ApiResponse<Message> sendDocument(int chatId, File documentFile)
sendAudio(int, File, OptionalArgs)public final ApiResponse<Message> sendDocument(int chatId, File documentFile, OptionalArgs optionalArgs)
chatId - Unique identifier for the message recipient - User or GroupChat iddocumentFile - File to sendoptionalArgs - Any optional argumentsMessagepublic final ApiResponse<Message> sendDocument(int chatId, String documentFileId)
sendDocument(int, String, OptionalArgs)public final ApiResponse<Message> sendDocument(int chatId, String documentFileId, OptionalArgs optionalArgs)
File.sendDocument(int, File, OptionalArgs)public final ApiResponse<Message> sendLocation(int chatId, float latitude, float longitude)
public final ApiResponse<Message> sendLocation(int chatId, float latitude, float longitude, OptionalArgs optionalArgs)
chatId - Unique identifier for the message recipient - User or GroupChat idlatitude - Latitude of locationlongitude - Longitude of locationoptionalArgs - Any optional argumentsMessagepublic final ApiResponse<Message> sendMessage(int chatId, String text)
sendMessage(int, String, OptionalArgs)public final ApiResponse<Message> sendMessage(int chatId, String text, OptionalArgs optionalArgs)
chatId - Unique identifier for the message recipient - User or GroupChat idtext - Text of the message to be sentoptionalArgs - Any optional argumentsMessagepublic final ApiResponse<Message> sendPhoto(int chatId, File photoFile)
sendPhoto(int, File, OptionalArgs)public final ApiResponse<Message> sendPhoto(int chatId, File photoFile, OptionalArgs optionalArgs)
chatId - Unique identifier for the message recipient - User or GroupChat idphotoFile - Photo to sendoptionalArgs - Any optional argumentsMessagepublic final ApiResponse<Message> sendPhoto(int chatId, String photoFileId)
sendPhoto(int, String, OptionalArgs)public final ApiResponse<Message> sendPhoto(int chatId, String photoFileId, OptionalArgs optionalArgs)
FilesendPhoto(int, File, OptionalArgs)public final ApiResponse<Message> sendSticker(int chatId, File stickerFile)
sendSticker(int, File, OptionalArgs)public final ApiResponse<Message> sendSticker(int chatId, File stickerFile, OptionalArgs optionalArgs)
chatId - Unique identifier for the message recipient - User or GroupChat idstickerFile - Sticker to send.optionalArgs - Any optional argumentsMessagepublic final ApiResponse<Message> sendSticker(int chatId, String stickerFileId)
sendSticker(int, String, OptionalArgs)public final ApiResponse<Message> sendSticker(int chatId, String stickerFileId, OptionalArgs optionalArgs)
File.sendSticker(int, File, OptionalArgs)public final ApiResponse<Message> sendVideo(int chatId, File videoFile)
sendVideo(int, File, OptionalArgs)public final ApiResponse<Message> sendVideo(int chatId, File videoFile, OptionalArgs optionalArgs)
sendDocument(int, File, OptionalArgs))).
Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future.
For any optional arguments, refer to the Telegram documentation.chatId - Unique identifier for the message recipient - User or GroupChat idvideoFile - Video to sendoptionalArgs - Any optional arguments.Messagepublic final ApiResponse<Message> sendVideo(int chatId, String videoFileId)
sendVideo(int, File, OptionalArgs)public final ApiResponse<Message> sendVideo(int chatId, String videoFileId, OptionalArgs optionalArgs)
FilesendVideo(int, File, OptionalArgs)public final ApiResponse<Message> sendVoice(int chatId, File voiceFile)
sendVoice(int, File, OptionalArgs)public final ApiResponse<Message> sendVoice(int chatId, File voiceFile, OptionalArgs optionalArgs)
chatId - Unique identifier for the message recipient — User or GroupChat idvoiceFile - Audio file to send. You can either pass a file_id as String to resend an audio
that is already on the Telegram servers, or upload a new audio file using multipart/form-data.optionalArgs - Any optional argumentsMessagepublic final ApiResponse<Message> sendVoice(int chatId, String voiceFileId)
sendVoice(int, File, OptionalArgs)public final ApiResponse<Message> sendVoice(int chatId, String voiceFileId, OptionalArgs optionalArgs)
sendVoice(int, File, OptionalArgs)public final ApiResponse<Message> replyTo(Message message, String text)
sendMessage(message.getChat().getId(), text, new OptionalArgs().replyToMessageId(message.getMessageId()))protected void onMessage(Message message)
message - The newly arrived Messageprotected void notifyNewMessages(List<Message> messages)
Messages asynchronously.
It must be overridden if provideExecutorService() is overridden to return null, otherwise
a NullPointerException may be thrown.messages - The newly arrived Messages