Search in sources :

Example 1 with Message

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;
        }
    });
}
Also used : GetUpdates(com.pengrad.telegrambot.request.GetUpdates) Message(com.pengrad.telegrambot.model.Message) GetUpdatesResponse(com.pengrad.telegrambot.response.GetUpdatesResponse) Update(com.pengrad.telegrambot.model.Update) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 2 with Message

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));
}
Also used : Message(com.pengrad.telegrambot.model.Message) SendMessage(com.pengrad.telegrambot.request.SendMessage) Optional(java.util.Optional)

Example 3 with Message

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;
}
Also used : Arrays(java.util.Arrays) UrlHelper(org.wyvie.chehov.bot.commands.helper.UrlHelper) Logger(org.slf4j.Logger) ParseMode(com.pengrad.telegrambot.model.request.ParseMode) User(com.pengrad.telegrambot.model.User) SendMessage(com.pengrad.telegrambot.request.SendMessage) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) SendResponse(com.pengrad.telegrambot.response.SendResponse) List(java.util.List) Message(com.pengrad.telegrambot.model.Message) Service(org.springframework.stereotype.Service) Document(org.jsoup.nodes.Document) Element(org.jsoup.nodes.Element) Qualifier(org.springframework.beans.factory.annotation.Qualifier) CommandHandler(org.wyvie.chehov.bot.commands.CommandHandler) Jsoup(org.jsoup.Jsoup) Elements(org.jsoup.select.Elements) TelegramBot(com.pengrad.telegrambot.TelegramBot) StringUtils(org.springframework.util.StringUtils) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 4 with Message

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)));
}
Also used : SendMessage(com.pengrad.telegrambot.request.SendMessage) Message(com.pengrad.telegrambot.model.Message)

Aggregations

Message (com.pengrad.telegrambot.model.Message)4 SendMessage (com.pengrad.telegrambot.request.SendMessage)3 TelegramBot (com.pengrad.telegrambot.TelegramBot)1 Update (com.pengrad.telegrambot.model.Update)1 User (com.pengrad.telegrambot.model.User)1 ParseMode (com.pengrad.telegrambot.model.request.ParseMode)1 GetUpdates (com.pengrad.telegrambot.request.GetUpdates)1 GetUpdatesResponse (com.pengrad.telegrambot.response.GetUpdatesResponse)1 SendResponse (com.pengrad.telegrambot.response.SendResponse)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Jsoup (org.jsoup.Jsoup)1 Document (org.jsoup.nodes.Document)1 Element (org.jsoup.nodes.Element)1 Elements (org.jsoup.select.Elements)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1