forked from pengrad/java-telegram-bot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelegramBot.java
More file actions
198 lines (162 loc) · 6.57 KB
/
TelegramBot.java
File metadata and controls
198 lines (162 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.pengrad.telegrambot;
import com.google.gson.Gson;
import com.pengrad.telegrambot.impl.FileApi;
import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.impl.SleepUpdatesHandler;
import com.pengrad.telegrambot.impl.UpdatesHandler;
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 com.pengrad.telegrambot.utility.BotUtils;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.TimeUnit;
import static com.pengrad.telegrambot.RequestPreprocessorKt.getEmptyRequestPreprocessor;
/**
* Stas Parshin
* 16 October 2015
*/
public class TelegramBot implements TelegramAware {
private final String token;
private final TelegramBotClient api;
private final FileApi fileApi;
private final UpdatesHandler updatesHandler;
private final RequestPreprocessor requestPreprocessor;
public TelegramBot(String botToken) {
this(new Builder(botToken));
}
TelegramBot(Builder builder) {
this.token = builder.botToken;
this.api = builder.api;
this.fileApi = builder.fileApi;
this.updatesHandler = builder.updatesHandler;
this.requestPreprocessor = builder.requestPreprocessor;
}
@NotNull
public <T extends BaseRequest<T, R>, R extends BaseResponse> R execute(@NotNull BaseRequest<T, R> request) {
requestPreprocessor.process(request);
return api.send(request);
}
public <T extends BaseRequest<T, R>, R extends BaseResponse> Cancellable execute(T request, Callback<T, R> callback) {
requestPreprocessor.process(request);
return api.send(request, callback);
}
public String getToken() {
return token;
}
public String getFullFilePath(File file) {
return fileApi.getFullFilePath(file.filePath());
}
public byte[] getFileContent(File file) throws IOException {
String fileUrl = getFullFilePath(file);
URLConnection connection = new URL(fileUrl).openConnection();
try (InputStream is = connection.getInputStream()) {
return BotUtils.getBytesFromInputStream(is);
}
}
public void setUpdatesListener(UpdatesListener listener) {
setUpdatesListener(listener, new GetUpdates());
}
public void setUpdatesListener(UpdatesListener listener, GetUpdates request) {
setUpdatesListener(listener, null, request);
}
public void setUpdatesListener(UpdatesListener listener, ExceptionHandler exceptionHandler) {
setUpdatesListener(listener, exceptionHandler, new GetUpdates());
}
public void setUpdatesListener(UpdatesListener listener, ExceptionHandler exceptionHandler, GetUpdates request) {
updatesHandler.stop();
updatesHandler.start(this, listener, exceptionHandler, request);
}
public void removeGetUpdatesListener() {
updatesHandler.stop();
}
public void shutdown() {
api.shutdown();
}
public static final class Builder {
static final String API_URL = "https://api.telegram.org/bot";
private final String botToken;
private FileApi fileApi;
private TelegramBotClient api;
private UpdatesHandler updatesHandler;
private RequestPreprocessor requestPreprocessor;
private OkHttpClient okHttpClient;
private String apiUrl;
private String fileApiUrl;
private boolean useTestServer = false;
public Builder(String botToken) {
this.botToken = botToken;
api = new TelegramBotClient(client(null), gson(), apiUrl(API_URL, botToken, useTestServer));
fileApi = new FileApi(botToken);
updatesHandler = new SleepUpdatesHandler(100);
requestPreprocessor = getEmptyRequestPreprocessor();
}
public Builder debug() {
okHttpClient = client(httpLoggingInterceptor());
return this;
}
public Builder okHttpClient(OkHttpClient client) {
okHttpClient = client;
return this;
}
public Builder apiUrl(String apiUrl) {
this.apiUrl = apiUrl;
return this;
}
public Builder fileApiUrl(String fileApiUrl) {
this.fileApiUrl = fileApiUrl;
return this;
}
public Builder updateListenerSleep(long millis) {
this.updatesHandler = new SleepUpdatesHandler(millis);
return this;
}
public Builder updateListener(UpdatesHandler updatesHandler) {
this.updatesHandler = updatesHandler;
return this;
}
public Builder useTestServer(boolean useTestServer) {
this.useTestServer = useTestServer;
return this;
}
public Builder requestPreprocessor(RequestPreprocessor requestPreprocessor) {
this.requestPreprocessor = requestPreprocessor;
return this;
}
public TelegramBot build() {
if (okHttpClient != null || apiUrl != null) {
OkHttpClient client = okHttpClient != null ? okHttpClient : client(null);
String baseUrl = apiUrl(apiUrl != null ? apiUrl : API_URL, botToken, useTestServer);
api = new TelegramBotClient(client, gson(), baseUrl);
}
if (fileApiUrl != null) {
fileApi = new FileApi(fileApiUrl, botToken);
}
return new TelegramBot(this);
}
private static OkHttpClient client(Interceptor interceptor) {
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(75, TimeUnit.SECONDS)
.writeTimeout(75, TimeUnit.SECONDS)
.readTimeout(75, TimeUnit.SECONDS);
if (interceptor != null) builder.addInterceptor(interceptor);
return builder.build();
}
private static Interceptor httpLoggingInterceptor() {
return new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
}
private static Gson gson() {
return BotUtils.GSON;
}
private static String apiUrl(String apiUrl, String botToken, boolean useTestServer) {
return apiUrl + botToken + (useTestServer ? "/test/" : "/");
}
}
}