forked from Viber/viber-bot-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViberBot.java
More file actions
287 lines (255 loc) · 10.8 KB
/
Copy pathViberBot.java
File metadata and controls
287 lines (255 loc) · 10.8 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package com.viber.bot.api;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ListenableFuture;
import com.viber.bot.Request;
import com.viber.bot.event.BotEventListener;
import com.viber.bot.event.Event;
import com.viber.bot.event.EventEmitter;
import com.viber.bot.event.callback.*;
import com.viber.bot.message.Message;
import com.viber.bot.message.TextMessage;
import com.viber.bot.middleware.DefaultMiddleware;
import com.viber.bot.middleware.Middleware;
import com.viber.bot.profile.BotProfile;
import com.viber.bot.profile.UserProfile;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.viber.bot.Preconditions.checkNotEmpty;
@ThreadSafe
public class ViberBot {
private static final String API_URL = "https://chatapi.viber.com/pa";
private final EventEmitter eventEmitter = new EventEmitter();
private final RegexMatcherRouter regexMatcherRouter = new RegexMatcherRouter();
private final ViberClient client;
private final MessageSender messageSender;
private final Middleware middleware;
private final BotProfile botProfile;
public ViberBot(final @Nonnull BotProfile botProfile, final @Nonnull String authToken) {
checkNotEmpty(authToken, "empty auth token");
this.botProfile = checkNotNull(botProfile);
client = new ViberClient(API_URL, authToken);
messageSender = new MessageSender(getBotProfile(), client);
middleware = new DefaultMiddleware(new RequestReceiverImpl(this, eventEmitter));
setupTextMessageReceivedHandler();
}
/**
* On incoming message from Viber, handles the message asynchronously.
* <p>
* This method returns a {@link ListenableFuture} with {@link InputStream} an promise.
* The returned InputStream should be used as a response to the incoming request from Viber.
* An InputStream is not guaranteed to be returned, and may be <code>null</code>.
* The url argument must specify an absolute {@link Request}. The name
* argument is a specifier that is relative to the url argument.
*
* @param request the Request object
* @return a future with nullable InputStream.
* @see Request
*/
public ListenableFuture<InputStream> incoming(final @Nonnull Request request) {
return middleware.incoming(request);
}
/**
* Sends an set webhook request to Viber
* The url must start with HTTPS, and must have valid ssl certificate.
*
* @param url the url to register as webhook
* @return a future with {@link ApiResponse} that may throw {@link ApiException}.
* @see ApiResponse
* @see ApiException
*/
public ListenableFuture<ApiResponse> setWebhook(final @Nonnull String url) {
return setWebhook(url, Lists.newArrayList(Event.values()));
}
/**
* Sends an set webhook request to Viber
* The url must start with HTTPS, and must have valid ssl certificate.
*
* @param url the url to register as webhook
* @param registerToEvents events to register to
* @return a future with {@link ApiResponse} that may throw {@link ApiException}.
* @see ApiResponse
* @see ApiException
*/
public ListenableFuture<ApiResponse> setWebhook(final @Nonnull String url, final @Nonnull List<Event> registerToEvents) {
return client.setWebhook(url, registerToEvents);
}
/**
* Sends messages in order to Viber user (via {@link UserProfile}).
* <p>
* This method ensures messages are sent in the order they are received.
* It will discard all keyboard in all messages expect the last message.
*
* @param to {@link UserProfile}
* @param messages collection of {@link Message}
* @return future which contains a collection of message tokens as strings, that may throw {@link ApiException}.
* @see Message
* @see UserProfile
* @see ApiException
*/
public ListenableFuture<Collection<String>> sendMessage(final @Nonnull UserProfile to, final @Nonnull Collection<Message> messages) {
final ListenableFuture<Collection<String>> messageTokens = messageSender.sendMessage(to, messages);
messageTokens.addListener(() -> messages.forEach(message -> eventEmitter.emit(Event.MESSAGE_SENT, to, message)), Runnable::run);
return messageTokens;
}
/**
* Sends one or more messages to a Viber user.
* Shorthand for {@link ViberBot#sendMessage(UserProfile, Collection)}
*
* @param to {@link UserProfile} to send messages to
* @param messages one or more messages to send
* @return future which contains a collection of message tokens as strings, that may throw {@link ApiException}.
* @see ViberBot#sendMessage(UserProfile, Collection)
*/
public ListenableFuture<Collection<String>> sendMessage(final @Nonnull UserProfile to, final @Nonnull Message... messages) {
return sendMessage(to, Lists.newArrayList(messages));
}
/**
* Returns the BOT public account info.
*
* @return a future with {@link ApiResponse} that may throw {@link ApiException}.
* @see ApiResponse
* @see ApiException
*/
public ListenableFuture<ApiResponse> getAccountInfo() {
return client.getAccountInfo();
}
/**
* Returns the BOT profile.
*
* @return {@link BotProfile}
*/
public BotProfile getBotProfile() {
return botProfile;
}
/**
* Returns user details of a specific Viber user.
*
* @param userId a string representing the user id, can be obtained from {@link UserProfile#getId}
* @return a future with {@link ApiResponse} that may throw {@link ApiException}.
* @see ApiResponse
* @see ApiException
*/
public ListenableFuture<ApiResponse> getUserDetails(final @Nonnull String userId) {
return client.getUserDetails(userId);
}
/**
* Returns the online status for Viber users.
*
* @param userIds a collection of strings representing the user ids, can be obtained from {@link UserProfile#getId}
* @return a future with {@link ApiResponse} that may throw {@link ApiException}.
* @see ApiResponse
* @see ApiException
*/
public ListenableFuture<ApiResponse> getOnlineStatus(final @Nonnull Collection<String> userIds) {
return client.getOnlineStatus(userIds);
}
/**
* Registers a callback for event {@link Event#MESSAGE_RECEIVED}.
* The listener will be called upon {@link ViberBot#incoming(Request)} of that event.
*
* @param listener an {@link OnMessageReceived} listener
* @see OnMessageReceived
*/
public void onMessageReceived(final @Nonnull OnMessageReceived listener) {
on(Event.MESSAGE_RECEIVED, listener);
}
/**
* Registers a callback for event {@link Event#MESSAGE_SENT}.
* The listener will be called when {@link ViberBot#sendMessage} successfully sends a message.
*
* @param listener an {@link OnMessageSent} listener
* @see OnMessageSent
*/
public void onMessageSent(final @Nonnull OnMessageSent listener) {
on(Event.MESSAGE_SENT, listener);
}
/**
* Registers a callback for a regular expression {@link TextMessage}.
* When a TextMessage arrives, will call listeners with matching pattern sequentially
* Call ordering is not gurenteed.
*
* @param pattern {@link Pattern}
* @param listener an {@link OnMessageReceived} listener
* @see OnMessageReceived
*/
public void onTextMessage(final @Nonnull Pattern pattern, final @Nonnull OnMessageReceived listener) {
regexMatcherRouter.newMatcher(pattern, listener);
}
/**
* Shorthand for {@link ViberBot#onTextMessage(Pattern, OnMessageReceived)}
* Creates a case insensitive {@link Pattern} object.
*
* @param regex regular expression
* @param listener an {@link OnMessageReceived} listener
* @see OnMessageReceived
* @see ViberBot#onTextMessage(Pattern, OnMessageReceived)
*/
public void onTextMessage(final @Nonnull String regex, final @Nonnull OnMessageReceived listener) {
regexMatcherRouter.newMatcher(Pattern.compile(regex, Pattern.CASE_INSENSITIVE), listener);
}
/**
* Registers a callback for event {@link Event#MESSAGE_SEEN}.
* The listener will be called upon {@link ViberBot#incoming(Request)} of that event.
*
* @param listener an {@link OnMessageSeen} listener
* @see OnMessageSeen
*/
public void onMessageSeen(final @Nonnull OnMessageSeen listener) {
on(Event.MESSAGE_SEEN, listener);
}
/**
* Registers a callback for event {@link Event#MESSAGE_DELIVERED}.
* The listener will be called upon {@link ViberBot#incoming(Request)} of that event.
*
* @param listener an {@link OnMessageDelivered} listener
* @see OnMessageDelivered
*/
public void onMessageDelivered(final @Nonnull OnMessageDelivered listener) {
on(Event.MESSAGE_DELIVERED, listener);
}
/**
* Registers a callback for event {@link Event#CONVERSATION_STARTED}.
* The listener will be called upon {@link ViberBot#incoming(Request)} of that event.
*
* @param listener an {@link OnConversationStarted} listener
* @see OnConversationStarted
*/
public void onConversationStarted(final @Nonnull OnConversationStarted listener) {
on(Event.CONVERSATION_STARTED, listener);
}
/**
* Registers a callback for event {@link Event#SUBSCRIBED}.
* The listener will be called upon {@link ViberBot#incoming(Request)} of that event.
*
* @param listener an {@link OnSubscribe} listener
* @see OnSubscribe
*/
public void onSubscribe(final @Nonnull OnSubscribe listener) {
on(Event.SUBSCRIBED, listener);
}
/**
* Registers a callback for event {@link Event#UNSUBSCRIBED}.
* The listener will be called upon {@link ViberBot#incoming(Request)} of that event.
*
* @param listener an {@link OnUnsubscribe} listener
* @see OnUnsubscribe
*/
public void onUnsubscribe(final @Nonnull OnUnsubscribe listener) {
on(Event.UNSUBSCRIBED, listener);
}
private void on(final @Nonnull Event event, final @Nonnull BotEventListener listener) {
eventEmitter.on(event, listener);
}
private void setupTextMessageReceivedHandler() {
on(Event.MESSAGE_RECEIVED, (OnMessageReceived) (event, message, response) -> {
if (!(message instanceof TextMessage)) return;
final TextMessage textMessage = (TextMessage) event.getMessage();
regexMatcherRouter.tryGetCallback(textMessage.getText()).forEach(callback -> callback.emit(event, message, response));
});
}
}