/** * The short name is one of the following: * <ol> * <li>First name</li> * <li>Last name</li> * <li>Username</li> * </ol> * The method will try to return the first valid name in the specified order. * * @return the short name of the user */ public static String shortName(User user) { if (!isEmpty(user.getFirstName())) return user.getFirstName(); if (!isEmpty(user.getLastName())) return user.getLastName(); return user.getUserName(); }
private boolean isGroupAdmin(Update update, int id) { GetChatAdministrators admins = new GetChatAdministrators().setChatId(getChatId(update)); return silent.execute(admins) .orElse(new ArrayList<>()).stream() .anyMatch(member -> member.getUser().getId() == id); }
Update addUser(Update update) { User endUser = AbilityUtils.getUser(update); users().compute(endUser.getId(), (id, user) -> { if (user == null) { updateUserId(user, endUser); return endUser; } if (!user.equals(endUser)) { updateUserId(user, endUser); return endUser; } return user; }); db.commit(); return update; }
private void updateUserId(User oldUser, User newUser) { if (oldUser != null && oldUser.getUserName() != null) { // Remove old username -> ID userIds().remove(oldUser.getUserName()); } if (newUser.getUserName() != null) { // Add new mapping with the new username userIds().put(newUser.getUserName().toLowerCase(), newUser.getId()); } }
/** * The full name is identified as the concatenation of the first and last name, separated by a space. * This method can return an empty name if both first and last name are empty. * * @return the full name of the user * @param user */ public static String fullName(User user) { StringJoiner name = new StringJoiner(" "); if (!isEmpty(user.getFirstName())) name.add(user.getFirstName()); if (!isEmpty(user.getLastName())) name.add(user.getLastName()); return name.toString(); } }
@Override public void execute(AbsSender absSender, User user, Chat chat, String[] strings) { DatabaseManager databseManager = DatabaseManager.getInstance(); StringBuilder messageBuilder = new StringBuilder(); String userName = user.getFirstName() + " " + user.getLastName(); if (databseManager.getUserStateForCommandsBot(user.getId())) { messageBuilder.append("Hi ").append(userName).append("\n"); messageBuilder.append("i think we know each other already!"); } else { databseManager.setUserStateForCommandsBot(user.getId(), true); messageBuilder.append("Welcome ").append(userName).append("\n"); messageBuilder.append("this bot will demonstrate you the command feature of the Java TelegramBots API!"); } SendMessage answer = new SendMessage(); answer.setChatId(chat.getId().toString()); answer.setText(messageBuilder.toString()); try { absSender.execute(answer); } catch (TelegramApiException e) { BotLogger.error(LOGTAG, e); } } }
/** * Gets the user with the specified username. If user was not found, the bot will send a message on Telegram. * * @param username the username of the required user * @param ctx the message context with the originating user * @return the id of the user */ protected int getUserIdSendError(String username, MessageContext ctx) { try { return getUser(username).getId(); } catch (IllegalStateException ex) { silent.send(getLocalizedMessage(USER_NOT_FOUND, ctx.user().getLanguageCode(), username), ctx.chatId()); throw propagate(ex); } }
private Optional<Message> sendMd(String message, MessageContext ctx, String... args) { return silent.sendMd(getLocalizedMessage(message, ctx.user().getLanguageCode(), args), ctx.chatId()); }
public Optional<Session> getSession(Message message){ try { return Optional.of(sessionManager.getSession(chatIdConverter)); } catch (UnknownSessionException e) { SessionContext botSession = new DefaultChatSessionContext(message.getChatId(), message.getFrom().getUserName()); return Optional.of(sessionManager.start(botSession)); } }
@Override public void execute(AbsSender absSender, User user, Chat chat, String[] arguments) { DatabaseManager dbManager = DatabaseManager.getInstance(); if (dbManager.getUserStateForCommandsBot(user.getId())) { dbManager.setUserStateForCommandsBot(user.getId(), false); String userName = user.getFirstName() + " " + user.getLastName(); SendMessage answer = new SendMessage(); answer.setChatId(chat.getId().toString()); answer.setText("Good bye " + userName + "\n" + "Hope to see you soon!"); try { absSender.execute(answer); } catch (TelegramApiException e) { BotLogger.error(LOGTAG, e); } } } }
userId = ctx.user().getId(); bannedUser = isNullOrEmpty(ctx.user().getUserName()) ? addTag(ctx.user().getUserName()) : shortName(ctx.user()); } else { bannedUser = addTag(username);
.input(0) .action(ctx -> { Privacy privacy = getPrivacy(ctx.update(), ctx.user().getId()); commands = getLocalizedMessage(ABILITY_COMMANDS_NOT_FOUND, ctx.user().getLanguageCode());
private Optional<Message> send(String message, MessageContext ctx, String... args) { return silent.send(getLocalizedMessage(message, ctx.user().getLanguageCode(), args), ctx.chatId()); }
boolean checkBlacklist(Update update) { Integer id = AbilityUtils.getUser(update).getId(); return id == creatorId() || !blacklist().contains(id); }
@Override public void execute(AbsSender absSender, User user, Chat chat, String[] arguments) { if (!DatabaseManager.getInstance().getUserStateForCommandsBot(user.getId())) { return; } String userName = chat.getUserName(); if (userName == null || userName.isEmpty()) { userName = user.getFirstName() + " " + user.getLastName(); } StringBuilder messageTextBuilder = new StringBuilder("Hello ").append(userName); if (arguments != null && arguments.length > 0) { messageTextBuilder.append("\n"); messageTextBuilder.append("Thank you so much for your kind words:\n"); messageTextBuilder.append(String.join(" ", arguments)); } SendMessage answer = new SendMessage(); answer.setChatId(chat.getId().toString()); answer.setText(messageTextBuilder.toString()); try { absSender.execute(answer); } catch (TelegramApiException e) { BotLogger.error(LOGTAG, e); } } }
boolean checkPrivacy(Trio<Update, Ability, String[]> trio) { Update update = trio.a(); User user = AbilityUtils.getUser(update); Privacy privacy; int id = user.getId(); privacy = getPrivacy(update, id); boolean isOk = privacy.compareTo(trio.b().privacy()) >= 0; if (!isOk) silent.send( getLocalizedMessage( CHECK_PRIVACY_FAIL, AbilityUtils.getUser(trio.a()).getLanguageCode()), getChatId(trio.a())); return isOk; }
.sorted() .reduce((a, b) -> format("%s%n%s", a, b)) .orElse(getLocalizedMessage(ABILITY_COMMANDS_NOT_FOUND, ctx.user().getLanguageCode()));
/** * Fetches the direct chat ID of the specified update. * * @param update a Telegram {@link Update} * @return the originating chat ID * @throws IllegalStateException if the chat ID could not be found */ public static Long getChatId(Update update) { if (MESSAGE.test(update)) { return update.getMessage().getChatId(); } else if (CALLBACK_QUERY.test(update)) { return update.getCallbackQuery().getMessage().getChatId(); } else if (INLINE_QUERY.test(update)) { return (long) update.getInlineQuery().getFrom().getId(); } else if (CHANNEL_POST.test(update)) { return update.getChannelPost().getChatId(); } else if (EDITED_CHANNEL_POST.test(update)) { return update.getEditedChannelPost().getChatId(); } else if (EDITED_MESSAGE.test(update)) { return update.getEditedMessage().getChatId(); } else if (CHOSEN_INLINE_QUERY.test(update)) { return (long) update.getChosenInlineQuery().getFrom().getId(); } else { throw new IllegalStateException("Could not retrieve originating chat ID from update"); } }
private Optional<Message> send(String message, Update upd) { Long chatId = upd.getMessage().getChatId(); return silent.send(getLocalizedMessage(message, AbilityUtils.getUser(upd).getLanguageCode()), chatId); }
private void handleIncomingMessage(Message message) throws InvalidObjectException { int state = userState.getOrDefault(message.getFrom().getId(), 0); switch(state) { case WAITINGCHANNEL: onWaitingChannelMessage(message); break; default: sendHelpMessage(message.getChatId(), message.getMessageId(), null); userState.put(message.getFrom().getId(), WAITINGCHANNEL); break; } }