-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartNazoBot.java
executable file
·316 lines (246 loc) · 9.69 KB
/
SmartNazoBot.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// camel-k: language=java dependency=camel-telegram dependency=camel-quarkus-http
// camel-k: dependency=camel-jackson dependency=camel-jsonpath dependency=camel-kafka
import org.apache.camel.Exchange;
import org.apache.camel.Predicate;
import org.apache.camel.PropertyInject;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.telegram.model.OutgoingTextMessage;
import org.apache.camel.component.telegram.model.InlineKeyboardButton;
import org.apache.camel.component.telegram.model.ReplyKeyboardMarkup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class SmartNazoBot extends RouteBuilder {
private static final Logger logger = LoggerFactory.getLogger(SmartNazoBot.class);
// Países
@PropertyInject("smart-nazo.countries")
private String countries;
@PropertyInject(value = "smart-nazo.welcome.command", defaultValue="/start")
private String welcomeCommand;
// Commando inicial
@PropertyInject(value = "smart-nazo.start.command")
private String startCommand;
/**********
* Mensagens: configure-as no arquivo application.properties
**********/
// Mensagem de boas vindas
@PropertyInject("smart-nazo.msg.welcome")
private String welcomeMessage;
// Mensagem de inicio
@PropertyInject(value = "smart-nazo.msg.start")
private String startMessage;
// Mensagem de comando inválido
@PropertyInject("smart-nazo.msg.warning.invalid")
private String invalidCommandMessage;
// Mensagem de localização não encontrada
@PropertyInject("smart-nazo.msg.warning.not-found")
private String invalidLocation;
// Mensagem enviada com os valores de poluição do ar
@PropertyInject("smart-nazo.msg.values")
private String messageValues;
private final Map<String, List<OutgoingTextMessage>> cache = new HashMap<>();
private InlineKeyboardButton buttonBuilder(Object object) {
return InlineKeyboardButton.builder().text(object.toString()).build();
}
private InlineKeyboardButton resetButton() {
return InlineKeyboardButton.builder().text(startCommand).build();
}
// Processa o comando inicial e configura o botão com a lista de países
private void processStart(Exchange exchange) {
OutgoingTextMessage msg = new OutgoingTextMessage();
// Cria uma lista de botões, uma para cada país
List<InlineKeyboardButton> buttons = Arrays.stream(countries.split(","))
.map(c -> buttonBuilder(c))
.collect(Collectors.toList());
// O botão de começo sempre no início da lista
buttons.add(0, resetButton());
ReplyKeyboardMarkup replyMarkup = ReplyKeyboardMarkup.builder()
.keyboard()
.addOneRowByEachButton(buttons)
.close()
.oneTimeKeyboard(false)
.build();
msg.setReplyMarkup(replyMarkup);
msg.setText(welcomeMessage);
exchange.getIn().setBody(msg);
}
private void processCity(Exchange exchange, List<?> cities) {
if (cities == null || cities.isEmpty()) {
exchange.getIn().setBody(invalidLocation);
return;
}
OutgoingTextMessage msg = new OutgoingTextMessage();
// Lista de botões, uma para cada cidade
List<InlineKeyboardButton> buttons = cities.stream()
.map(this::buttonBuilder)
.collect(Collectors.toList());
buttons.add(0, resetButton());
ReplyKeyboardMarkup replyMarkup = ReplyKeyboardMarkup.builder()
.keyboard()
.addOneRowByEachButton(buttons)
.close()
.oneTimeKeyboard(false)
.build();
msg.setReplyMarkup(replyMarkup);
msg.setText(startMessage);
exchange.getIn().setBody(msg);
}
private void processCountryCitiesText(Exchange exchange) throws IOException {
List<?> body = exchange.getMessage().getBody(List.class);
String country = exchange.getMessage().getHeader("country", String.class);
// Para cada cidade, muda o texto para o formato cidade@país.
List<String> cities = body.stream()
.map(o -> o + "@" + country)
.collect(Collectors.toList());
// Processa botões e resposta
processCity(exchange, cities);
}
private void saveToCache(String country, String city, OutgoingTextMessage msg) {
cache.computeIfAbsent(fullLocation(country, city), k -> new ArrayList<>()).add(msg);
}
private String fullLocation(String country, String city) {
return city + "@" + country;
}
// Pega cidades pré-existentes no cache
private void processCitiesTextFromCache(Exchange exchange) throws IOException {
List<OutgoingTextMessage> cachedMessages = cache.get(exchange.getMessage().getBody(String.class));
exchange.getMessage().setBody(cachedMessages);
}
private void processCitiesText(Exchange exchange) throws IOException {
String value = exchange.getMessage().getBody(String.class);
String parameter = exchange.getMessage().getHeader("parameter", String.class);
String unit = exchange.getMessage().getHeader("unit", String.class);
String city = exchange.getMessage().getHeader("city", String.class);
String country = exchange.getMessage().getHeader("country", String.class);
// Telegram reclama de ter o ponto em alguns valores
String text = String.format(messageValues, parameter, city, value.replace(".", ","), unit);
logger.info(text);
// Formata a mensagem de reposta
OutgoingTextMessage msg = OutgoingTextMessage.builder()
.parseMode("MarkdownV2")
.text(text)
.build();
// Insere o botão de início
List<InlineKeyboardButton> buttons = new ArrayList<>();
buttons.add(resetButton());
ReplyKeyboardMarkup replyMarkup = ReplyKeyboardMarkup.builder()
.keyboard()
.addOneRowByEachButton(buttons)
.close()
.oneTimeKeyboard(true)
.resizeKeyboard(true)
.build();
msg.setReplyMarkup(replyMarkup);
saveToCache(country, city, msg);
exchange.getMessage().setBody(msg);
}
// Formata manualmente os alertas em formato JSON para mandar para o Kafka
private void processCitiesAlerts(Exchange exchange) throws IOException {
String value = exchange.getMessage().getBody(String.class);
String parameter = exchange.getMessage().getHeader("parameter", String.class);
String unit = exchange.getMessage().getHeader("unit", String.class);
String city = exchange.getMessage().getHeader("city", String.class);
String country = exchange.getMessage().getHeader("country", String.class);
String text = String.format("{ \"country\": \"%s\", \"city\": \"%s\", \"parameter\": \"%s\", \"value\": \"%s %s\"}",
country, city, parameter, value, unit);
exchange.getMessage().setBody(text);
}
public static String getCitiesInCountry(String body) {
logger.debug("Computing the router for country {}", body);
return "{{smart-nazo.openaq.base.url}}/cities?country=" + body;
}
private Predicate isCityCached() {
return p -> {
String body = p.getMessage().getBody(String.class);
logger.debug("Checking if {} is cached", body);
return cache.containsKey(body);
};
}
public static String refreshLastMeasurementForCity(String body) {
logger.debug("Computing the route for city {}", body);
String city = body.split("@")[0];
String country = body.split("@")[1];
return String.format("{{smart-nazo.openaq.base.url}}/latest?city=%s&country=%s", city, country);
}
private Predicate isKnownCountry() {
return p -> Arrays.asList(countries.split(",")).contains(p.getMessage().getBody(String.class));
}
private Predicate isKnownCity() {
return p -> p.getMessage().getBody(String.class).contains("@") ;
}
@Override
public void configure() throws Exception {
// Logging
from("direct:openaq-tap")
.log("${body}");
// Bot
from("telegram:bots?authorizationToken={{smart-nazo.telegram.token}}")
.wireTap("direct:openaq-tap")
.choice()
.when(body().isEqualTo(welcomeCommand))
.process(this::processStart)
.to("direct:telegram")
.when(body().isEqualTo(startCommand))
.process(this::processStart)
.to("direct:telegram")
.when(body().in(isKnownCountry()))
.to("direct:destinationCountryResolver")
.when(body().in(isKnownCity()))
.to("direct:queryCityMeasurements")
.otherwise()
.setBody(constant(invalidCommandMessage))
.to("direct:telegram");
// Cidades
from("direct:destinationCountryResolver")
.streamCaching()
.routingSlip(method(SmartNazoBot.class, "getCitiesInCountry"))
.setHeader("country", jsonpath("$.results[0].country"))
.setBody(jsonpath("$.results[*].city"))
.process(this::processCountryCitiesText)
.to("direct:telegram");
// Medições: rota pai
from("direct:queryCityMeasurements")
.streamCaching()
.choice()
.when(isCityCached())
.process(this::processCitiesTextFromCache)
.to("direct:cached")
.otherwise()
.routingSlip(method(SmartNazoBot.class, "refreshLastMeasurementForCity"))
.setHeader("country", jsonpath("$.results[0].country"))
.setHeader("city", jsonpath("$.results[0].city"))
.split().jsonpathWriteAsString("$.results[*].measurements[*]")
.multicast().parallelProcessing().to("direct:measurements", "direct:alerts");
// Medições: dados do cache
from("direct:cached")
.split(body())
.to("direct:telegram");
// Medições: dados do OpenAQ
from("direct:measurements")
.streamCaching()
.setHeader("parameter", jsonpath("$.parameter"))
.setHeader("unit", jsonpath("$.unit"))
.setBody(jsonpath("$.value"))
.process(this::processCitiesText)
.to("direct:telegram");
// Alertas
from("direct:alerts")
.streamCaching()
.setHeader("parameter", jsonpath("$.parameter"))
.setHeader("unit", jsonpath("$.unit"))
.setBody(jsonpath("$.value"))
.process(this::processCitiesAlerts)
.to("kafka:{{smart-nazo.alert.topic}}");
// Telegram
from("direct:telegram")
.streamCaching()
.wireTap("direct:openaq-tap")
.to("telegram:bots?authorizationToken={{smart-nazo.telegram.token}}");;
}
}