use of com.pengrad.telegrambot.model.Message in project anton-pavlovich-bot by wyvie.
the class MessageReader method readMessages.
@Scheduled(fixedDelay = 200)
public void readMessages() {
GetUpdates getUpdates = new GetUpdates().limit(telegramProperties.getUpdateLimit()).offset(lastOffset).timeout(0);
GetUpdatesResponse response = telegramBot.execute(getUpdates);
List<Update> updates = response.updates();
updates.forEach(update -> {
lastOffset = update.updateId() + 1;
Message message = update.message();
if (message != null && message.text() != null) {
logger.debug("Got message '" + message.text() + "' from chat_id " + message.chat().id());
persistUser(message.from());
if (validateCommmand(message))
commandProcessor.processCommand(message);
else {
String messageText = message.text().trim();
if (messageText.equals("+") || messageText.equals("-") || emojiHelper.isThumbsUp(messageText) || emojiHelper.isThumbsDown(messageText))
commandProcessor.processKarma(message);
}
lastOffset = update.updateId() + 1;
}
});
}
use of com.pengrad.telegrambot.model.Message in project anton-pavlovich-bot by wyvie.
the class SlapCommand method handle.
@Override
public void handle(Message message, String args) {
String username = null;
String object = DEFAULT_OBJECT;
Message replied = message.replyToMessage();
if (replied != null) {
username = toUsername(replied.from());
if (!StringUtils.isEmpty(args))
object = args;
} else if (!StringUtils.isEmpty(args)) {
String argun = args;
if (argun.startsWith("@"))
argun = argun.substring(1);
if (argun.contains(" ")) {
int position = argun.indexOf(' ');
object = argun.substring(position + 1);
argun = argun.substring(0, position);
}
Optional<UserEntity> userEntity = userRepository.findByUsername(argun);
if (userEntity.isPresent())
username = "@" + argun;
}
if (username == null) {
logger.debug("Could not find username");
sendMessage(message.chat().id(), MESSAGE.replaceAll("%USER1%", toUsername(message.from())).replaceAll("%USER2%", "themselves").replaceAll("%OBJECT%", object));
return;
}
sendMessage(message.chat().id(), MESSAGE.replaceAll("%USER1%", toUsername(message.from())).replaceAll("%USER2%", username).replaceAll("%OBJECT%", object));
}
use of com.pengrad.telegrambot.model.Message in project anton-pavlovich-bot by wyvie.
the class SlovnikCommand method parseHtml.
private List<List<String>> parseHtml(String source) {
List<List<String>> blocks = new ArrayList<>();
Document document = Jsoup.parse(source);
Elements results = document.select("div#results");
// #results > div > h1 - subject itself or nothing have been found message
List<String> headFastDefLine = new ArrayList<>();
headFastDefLine.add(String.format("<b>%s</b>", results.select("h1").text()));
results.select("#fastMeanings a").forEach(element -> {
String hrefNew = BASE_SLOVNIK_URL + element.attr("href");
element = element.attr("href", hrefNew);
headFastDefLine.add(element.toString());
});
// extended grammatics
List<String> extDefs = new ArrayList<>();
results.select("ol li dl").forEach(dl -> {
Element dt = dl.selectFirst("dt");
String def = dt.text();
String aTagsDefs = dt.select("a").stream().map(e -> {
String hrefNew = BASE_SLOVNIK_URL + e.attr("href");
return e.attr("href", hrefNew).toString();
}).collect(Collectors.joining(","));
extDefs.add(String.format("%s %s", aTagsDefs, def));
dl.select("dd").forEach(dd -> {
extDefs.add(dd.wholeText());
});
});
// additional definitions links
List<String> moreDefs = new ArrayList<>();
results.select("ul.moreResults li").forEach(li -> {
Element aTag = li.selectFirst("a");
li.selectFirst("a").remove();
String hrefNew = BASE_SLOVNIK_URL + aTag.attr("href");
aTag = aTag.attr("href", hrefNew);
String liText = li.text();
moreDefs.add(String.format("%s <i>%s</i>", aTag, liText));
});
// synonyms/antonyms
List<String> synDefs = new ArrayList<>();
results.select("div.other-meaning a").forEach(a -> {
String hrefNew = BASE_SLOVNIK_URL + a.attr("href");
synDefs.add(a.attr("href", hrefNew).toString());
});
// additional definitions links
List<String> addDefs = new ArrayList<>();
results.select("#fulltext li").forEach(li -> {
Element aTag = li.selectFirst("a");
li.selectFirst("a").remove();
String hrefNew = BASE_SLOVNIK_URL + aTag.attr("href");
aTag = aTag.attr("href", hrefNew);
String liText = li.text();
addDefs.add(String.format("%s <i>%s</i>", aTag, liText));
});
blocks.add(headFastDefLine);
blocks.add(extDefs);
blocks.add(synDefs);
blocks.add(moreDefs);
blocks.add(addDefs);
return blocks;
}
use of com.pengrad.telegrambot.model.Message in project anton-pavlovich-bot by wyvie.
the class AbstractKarmaCommand method processCommand.
void processCommand(Message message) {
int userId = message.from().id();
Message replied = message.replyToMessage();
if (replied == null) {
sendMessage(message.chat().id(), ERROR_NOT_REPLY);
return;
}
if (message.from().id().equals(replied.from().id())) {
sendMessage(message.chat().id(), ERROR_YOURSELF);
return;
}
if (!canUserUpdateNow(userId)) {
sendMessage(message.chat().id(), ERROR_TOO_EARLY);
return;
}
processKarma(replied.from());
updateLastSetKarma(message.from().id());
int newKarma = getUserKarma(replied.from().id());
String username = replied.from().username();
if (username == null || "".equals(username.trim())) {
String firstName = replied.from().firstName() == null ? "" : replied.from().firstName();
String lastName = replied.from().lastName() == null ? "" : replied.from().lastName();
username = (firstName + " " + lastName).trim();
} else
username = "@" + username.trim();
sendMessage(message.chat().id(), INFO_NEW_KARMA.replaceAll("%USER%", username).replaceAll("%KARMA%", Integer.toString(newKarma)));
}
Aggregations