Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bot interface for TelegramBot is extracted #334

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions library/src/main/java/com/pengrad/telegrambot/Bot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.pengrad.telegrambot;

import com.pengrad.telegrambot.model.File;
import com.pengrad.telegrambot.request.BaseRequest;
import com.pengrad.telegrambot.request.GetUpdates;
import com.pengrad.telegrambot.response.BaseResponse;

import java.io.IOException;

public interface Bot {
<T extends BaseRequest<T, R>, R extends BaseResponse> R execute(BaseRequest<T, R> request);

<T extends BaseRequest<T, R>, R extends BaseResponse> void execute(T request, Callback<T, R> callback);

String getToken();

String getFullFilePath(File file);

byte[] getFileContent(File file) throws IOException;

void setUpdatesListener(UpdatesListener listener);

void setUpdatesListener(UpdatesListener listener, GetUpdates request);

void setUpdatesListener(UpdatesListener listener, ExceptionHandler exceptionHandler);

void setUpdatesListener(UpdatesListener listener, ExceptionHandler exceptionHandler, GetUpdates request);

void removeGetUpdatesListener();

void shutdown();
}
13 changes: 12 additions & 1 deletion library/src/main/java/com/pengrad/telegrambot/TelegramBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* Stas Parshin
* 16 October 2015
*/
public class TelegramBot {
public class TelegramBot implements Bot {

private final String token;
private final TelegramBotClient api;
Expand All @@ -40,22 +40,27 @@ public TelegramBot(String botToken) {
this.updatesHandler = builder.updatesHandler;
}

@Override
public <T extends BaseRequest<T, R>, R extends BaseResponse> R execute(BaseRequest<T, R> request) {
return api.send(request);
}

@Override
public <T extends BaseRequest<T, R>, R extends BaseResponse> void execute(T request, Callback<T, R> callback) {
api.send(request, callback);
}

@Override
public String getToken() {
return token;
}

@Override
public String getFullFilePath(File file) {
return fileApi.getFullFilePath(file.filePath());
}

@Override
public byte[] getFileContent(File file) throws IOException {
String fileUrl = getFullFilePath(file);
URLConnection connection = new URL(fileUrl).openConnection();
Expand All @@ -64,26 +69,32 @@ public byte[] getFileContent(File file) throws IOException {
}
}

@Override
public void setUpdatesListener(UpdatesListener listener) {
setUpdatesListener(listener, new GetUpdates());
}

@Override
public void setUpdatesListener(UpdatesListener listener, GetUpdates request) {
setUpdatesListener(listener, null, request);
}

@Override
public void setUpdatesListener(UpdatesListener listener, ExceptionHandler exceptionHandler) {
setUpdatesListener(listener, exceptionHandler, new GetUpdates());
}

@Override
public void setUpdatesListener(UpdatesListener listener, ExceptionHandler exceptionHandler, GetUpdates request) {
updatesHandler.start(this, listener, exceptionHandler, request);
}

@Override
public void removeGetUpdatesListener() {
updatesHandler.stop();
}

@Override
public void shutdown() {
api.shutdown();
}
Expand Down