Object
AbilityUtils

public final class AbilityUtils extends Object
Helper and utility methods
  • Field Details

    • EMPTY_USER

      public static User EMPTY_USER
  • Method Details

    • stripTag

      public static String stripTag(String username)
      Parameters:
      username - any username
      Returns:
      the username with the preceding "@" stripped off
    • commitTo

      public static Consumer<MessageContext> commitTo(DBContext db)
      Commits to DB.
      Parameters:
      db - the database to commit on
      Returns:
      a lambda consumer that takes in a MessageContext, used in post actions for abilities
    • getUser

      public static User getUser(Update update)
      Fetches the user who caused the update.
      Parameters:
      update - a Telegram Update
      Returns:
      the originating user
      Throws:
      IllegalStateException - if the user could not be found
    • isGroupUpdate

      public static boolean isGroupUpdate(Update update)
      A "best-effort" boolean stating whether the update is a group message or not.
      Parameters:
      update - a Telegram Update
      Returns:
      whether the update is linked to a group
    • isSuperGroupUpdate

      public static boolean isSuperGroupUpdate(Update update)
      A "best-effort" boolean stating whether the update is a super-group message or not.
      Parameters:
      update - a Telegram Update
      Returns:
      whether the update is linked to a group
    • getChatId

      public static Long getChatId(Update update)
      Fetches the direct chat ID of the specified update.
      Parameters:
      update - a Telegram Update
      Returns:
      the originating chat ID
      Throws:
      IllegalStateException - if the chat ID could not be found
    • isUserMessage

      public static boolean isUserMessage(Update update)
      Parameters:
      update - a Telegram Update
      Returns:
      true if the update contains contains a private user message
    • addTag

      public static String addTag(String username)
      Parameters:
      username - the username to add the tag to
      Returns:
      the username prefixed with the "@" tag.
    • isReplyTo

      public static Predicate<Update> isReplyTo(String msg)
      Parameters:
      msg - the message to be replied to
      Returns:
      a predicate that asserts that the update is a reply to the specified message.
    • getLocalizedMessage

      public static String getLocalizedMessage(String messageCode, Locale locale, Object... arguments)
    • getLocalizedMessage

      public static String getLocalizedMessage(String messageCode, String languageCode, Object... arguments)
    • shortName

      public static String shortName(User user)
      The short name is one of the following:
      1. First name
      2. Last name
      3. Username
      The method will try to return the first valid name in the specified order.
      Returns:
      the short name of the user
    • fullName

      public static String fullName(User user)
      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.
      Parameters:
      user - User to use
      Returns:
      the full name of the user
    • escape

      public static String escape(String username)
    • isValidCommand

      public static boolean isValidCommand(String command)
      Checks if the passed string is a valid bot command according to the requirements of Telegram Bot API: "A command must always start with the '/' symbol and may not be longer than 32 characters. Commands can use latin letters, numbers and underscores." (https://core.telegram.org/bots#commands)
      Parameters:
      command - String representation of a command to be checked for validity
      Returns:
      whether the command is valid
    • isValidCommandName

      public static boolean isValidCommandName(String commandName)
      Checks if the passed String is a valid command name. Command name is text of a command without leading '/'
      Parameters:
      commandName - the command name to be checked for validity
      Returns:
      whether the command name is valid