-
Notifications
You must be signed in to change notification settings - Fork 4
/
LongPollingBot.java
144 lines (118 loc) · 4.62 KB
/
LongPollingBot.java
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
package chat.tamtam.bot.longpolling;
import java.lang.invoke.MethodHandles;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import chat.tamtam.bot.TamTamBot;
import chat.tamtam.bot.TamTamBotBase;
import chat.tamtam.bot.exceptions.TamTamBotException;
import chat.tamtam.botapi.client.TamTamClient;
import chat.tamtam.botapi.exceptions.APIException;
import chat.tamtam.botapi.exceptions.ClientException;
import chat.tamtam.botapi.model.Subscription;
import chat.tamtam.botapi.model.Update;
import chat.tamtam.botapi.model.UpdateList;
import chat.tamtam.botapi.queries.GetSubscriptionsQuery;
import chat.tamtam.botapi.queries.GetUpdatesQuery;
import chat.tamtam.botapi.queries.UnsubscribeQuery;
/**
* @author alexandrchuprin
*/
public class LongPollingBot extends TamTamBotBase implements TamTamBot {
private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private final Thread poller;
private final LongPollingBotOptions options;
public LongPollingBot(String accessToken, Object... handlers) {
this(accessToken, LongPollingBotOptions.DEFAULT, handlers);
}
public LongPollingBot(String accessToken, LongPollingBotOptions options, Object... handlers) {
this(TamTamClient.create(accessToken), options, handlers);
}
public LongPollingBot(TamTamClient client, LongPollingBotOptions options, Object... handlers) {
super(client, handlers);
this.poller = new Thread(this::poll, "tamtam-bot-poller-" + getClass().getSimpleName());
this.options = options;
}
public void start() throws TamTamBotException {
try {
checkWebhook();
} catch (Exception e) {
throw new TamTamBotException("Failed to check webhook subscription", e);
}
poller.start();
}
public void stop() {
poller.interrupt();
try {
poller.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
protected void handleUpdates(List<Update> updates) {
for (Update update : updates) {
try {
onUpdate(update);
} catch (Exception e) {
LOG.error("Failed to handle update: {}", update, e);
}
}
}
protected UpdateList pollOnce(Long marker) throws APIException, ClientException {
return new GetUpdatesQuery(getClient())
.marker(marker)
.timeout(options.getRequestTimeout())
.types(options.getUpdateTypes())
.limit(options.getLimit())
.execute();
}
private void checkWebhook() throws APIException, ClientException {
List<Subscription> subscriptions = new GetSubscriptionsQuery(getClient()).execute().getSubscriptions();
if (subscriptions.isEmpty()) {
return;
}
if (!options.shouldRemoveWebhook()) {
LOG.warn("Bot {} has webhook subscriptions: {}. " +
"Long polling will not receive updates in this case." +
"Remove it manually or set `shouldRemoveWebhook` to `true` in options.", this, subscriptions);
return;
}
for (Subscription subscription : subscriptions) {
new UnsubscribeQuery(getClient(), subscription.getUrl()).execute();
}
}
private void poll() {
Long marker = null;
int error = 0;
while (true) {
UpdateList updateList;
try {
updateList = pollOnce(marker);
error = 0;
} catch (APIException | ClientException e) {
if (e.getCause() instanceof InterruptedException) {
Thread.currentThread().interrupt();
break;
}
error = Math.min(++error, 5);
LOG.error("Failed to get updates with marker {}. Will retry in {} second(s)…", marker, error, e);
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(error));
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
break;
}
continue;
}
if (Thread.currentThread().isInterrupted()) {
// Bot is stopped, will not handle updates
break;
}
List<Update> updates = updateList.getUpdates();
handleUpdates(updates);
marker = updateList.getMarker();
}
LOG.info("Polling thread stopped");
}
}