@Override public String toString() { return "BotStatus{" + "id=" + id + ", isEnabled=" + isEnabled + ", requester=" + from.toString() + ", timestamp='" + timestamp + '\'' + '}'; } }
@Override public String toString() { return "MessageUpdate{" + "updateId=" + updateId + ", message=" + message.toString() + ", additionalProperties=" + additionalProperties + '}'; } }
public String registerNotification(MessageUpdate message) { String channel = null; if (message.getMessage().getChat().getType().equals("group") || message.getMessage().getChat().getType().equals("supergroup")) { channel = message.getMessage().getChat().getTitle(); } else { channel = message.getMessage().getFrom().getFirstName(); } return repository.register(new PacktNotification(message.getMessage().getChat().getId(), channel)); }
/** * When a member join, left or gets excluded from an Telegram group a msg will be sent to the target group. * If the member added or removed is a bot, no message is sent. * @param update {@link MessageUpdate} * @return true if the message is to inform a new member or if a member left the chat */ private String chatMember(MessageUpdate update) { ObjectMapper mapper = new ObjectMapper(); final Message message = new Message(); for (Map.Entry<String, Object> entry : update.getMessage().getAdditionalProperties().entrySet()) { log.fine("Additional Properties: KEY + " + entry.getKey() + " - VALUE " + entry.getValue().toString()); if (entry.getKey().equals("new_chat_member") && !update.getMessage().getFrom().isIsBot()) { NewChatMember member = mapper.convertValue(entry.getValue(), NewChatMember.class); message.setText(String.format(WELCOME_MESSAGE, member.getFirst_name(), update.getMessage().getChat().getTitle())); } else if (entry.getKey().equals("left_chat_participant") && !update.getMessage().getFrom().isIsBot()) { LeftChatMember member = mapper.convertValue(entry.getValue(), LeftChatMember.class); message.setText(String.format(GOODBYE_MESSAGE, member.getFirst_name())); } } return message.getText(); } }
@Override public String process(MessageUpdate update) { StringBuilder response = new StringBuilder(); try { if (canProcess(update.getMessage().getText()) && !update.isEdited()) { List<String> itens = Arrays.asList(update.getMessage().getText().replaceAll("\\r|\\n", " ").split(" ")); HashMap<String, String> finalTargets = new HashMap<>(); String username = update.getMessage().getFrom().getUsername() != null ? update.getMessage().getFrom().getUsername() : update.getMessage().getFrom().getFirstName().toLowerCase(); itens.stream().distinct().forEach(item -> { if ((KARMA_PATTERN.matcher(item).find())) { finalTargets.putIfAbsent(item.substring(0, item.length() - 2).toLowerCase(), item.substring(item.length() - 2)); } }); for (Map.Entry<String, String> entry : finalTargets.entrySet()) { response.append(processKarma(entry.getValue(), entry.getKey(), username)); } } else { log.fine("Message " + update.getMessage().getText() + " is a updated message, ignoring..."); } } catch (final Exception e) { e.printStackTrace(); log.warning(e.getMessage()); } return response.toString(); }
/** * only commands redirected to this bot will be processed * * @param messageUpdate message containing the command * @param botUserId bot id, this will be used to make sure that this bot can process the given command * @return true - processable command * or * false - non processable command */ default boolean canProcessCommand(MessageUpdate messageUpdate, String botUserId) { return true ? messageUpdate.getMessage().getText().startsWith("/") && (messageUpdate.getMessage().getText().contains("@" + botUserId) || !messageUpdate.getMessage().getText().contains("@")) && extractCommand(messageUpdate.getMessage().getText(), botUserId).equals(name()) : false; }
@Override public String toString() { return "EditedMessage{" + "messageId=" + messageId + ", from=" + from.toString() + ", chat=" + chat.toString() + ", date=" + date + ", editDate=" + editDate + ", text='" + text + '\'' + ", entities=" + entities.toString() + ", additionalProperties=" + additionalProperties + '}'; } }
public String unregisterNotification(MessageUpdate message) { String channel = null; if (message.getMessage().getChat().getType().equals("group") || message.getMessage().getChat().getType().equals("supergroup")) { channel = message.getMessage().getChat().getTitle(); } else { channel = message.getMessage().getFrom().getFirstName(); } return repository.unregister(new PacktNotification(message.getMessage().getChat().getId(), channel)); }
/** * only commands redirected to this bot will be processed * * @param messageUpdate message containing the command * @param botUserId bot id, this will be used to make sure that this bot can process the given command * @return true - processable command * or * false - non processable command */ default boolean canProcessCommand(MessageUpdate messageUpdate, String botUserId) { if (null == messageUpdate.getMessage().getText()) { return false; } else { return true ? messageUpdate.getMessage().getText().startsWith("/") && (messageUpdate.getMessage().getText().contains("@" + botUserId) || !messageUpdate.getMessage().getText().contains("@")) && extractCommand(messageUpdate.getMessage().getText(), botUserId).equals(name()) : false; } } }