Class AbilityUtils

java.lang.Object
org.telegram.abilitybots.api.util.AbilityUtils

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

    Fields
    Modifier and Type
    Field
    Description
    static org.telegram.telegrambots.meta.api.objects.User
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    addTag(String username)
     
    Commits to DB.
    static String
    escape(String username)
     
    static String
    fullName(org.telegram.telegrambots.meta.api.objects.User user)
    The full name is identified as the concatenation of the first and last name, separated by a space.
    static Long
    getChatId(org.telegram.telegrambots.meta.api.objects.Update update)
    Fetches the direct chat ID of the specified update.
    static String
    getLocalizedMessage(String messageCode, String languageCode, Object... arguments)
     
    static String
    getLocalizedMessage(String messageCode, Locale locale, Object... arguments)
     
    static org.telegram.telegrambots.meta.api.objects.User
    getUser(org.telegram.telegrambots.meta.api.objects.Update update)
    Fetches the user who caused the update.
    static boolean
    isGroupUpdate(org.telegram.telegrambots.meta.api.objects.Update update)
    A "best-effort" boolean stating whether the update is a group message or not.
    static Predicate<org.telegram.telegrambots.meta.api.objects.Update>
     
    static boolean
    isSuperGroupUpdate(org.telegram.telegrambots.meta.api.objects.Update update)
    A "best-effort" boolean stating whether the update is a super-group message or not.
    static boolean
    isUserMessage(org.telegram.telegrambots.meta.api.objects.Update update)
     
    static boolean
    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.
    static boolean
    Checks if the passed String is a valid command name.
    static String
    shortName(org.telegram.telegrambots.meta.api.objects.User user)
    The short name is one of the following: First name Last name Username The method will try to return the first valid name in the specified order.
    static String
    stripTag(String username)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • EMPTY_USER

      public static org.telegram.telegrambots.meta.api.objects.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 org.telegram.telegrambots.meta.api.objects.User getUser(org.telegram.telegrambots.meta.api.objects.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(org.telegram.telegrambots.meta.api.objects.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(org.telegram.telegrambots.meta.api.objects.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(org.telegram.telegrambots.meta.api.objects.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(org.telegram.telegrambots.meta.api.objects.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<org.telegram.telegrambots.meta.api.objects.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(org.telegram.telegrambots.meta.api.objects.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(org.telegram.telegrambots.meta.api.objects.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