Class BaseAbilityBot
- java.lang.Object
-
- org.telegram.telegrambots.meta.bots.AbsSender
-
- org.telegram.telegrambots.bots.DefaultAbsSender
-
- org.telegram.abilitybots.api.bot.BaseAbilityBot
-
- All Implemented Interfaces:
AbilityExtension
- Direct Known Subclasses:
AbilityBot,AbilityWebhookBot
public abstract class BaseAbilityBot extends DefaultAbsSender implements AbilityExtension
The father of all ability bots. Bots that need to utilize abilities need to extend this bot.It's important to note that this bot strictly extends
TelegramLongPollingBot.All bots extending the
BaseAbilityBotget implicit abilities:- /claim - Claims this bot
- Sets the user as the
Privacy.CREATORof the bot - Only the user with the ID returned by
creatorId()can genuinely claim the bot - /report - reports all user-defined commands (abilities)
- The same format acceptable by BotFather
- /commands - returns a list of all possible bot commands based on the privacy of the requesting user
- /backup - returns a backup of the bot database
- /recover - recovers the database
- /promote
@username- promotes user to bot admin - /demote
@username- demotes bot admin to user - /ban
@username- bans the user from accessing your bot commands and features - /unban
@username- lifts the ban from the user
Additional information of the implicit abilities are present in the methods that declare them.
The two most important handles in the BaseAbilityBot are the
DBContextdband theMessageSendersender. All bots extending BaseAbilityBot can use both handles in their update consumers.- Author:
- Abbas Abou Daya
-
-
Field Summary
Fields Modifier and Type Field Description static StringADMINSprotected static StringBACKUPprotected static StringBANstatic StringBLACKLISTprotected static StringCLAIMprotected static StringCOMMANDSprotected DBContextdbprotected static StringDEFAULTprotected static StringDEMOTEprotected static StringPROMOTEprotected static StringRECOVERprotected static StringREPORTprotected MessageSendersenderprotected SilentSendersilentprotected static StringUNBANstatic StringUSER_IDstatic StringUSERS-
Fields inherited from class org.telegram.telegrambots.bots.DefaultAbsSender
exe
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedBaseAbilityBot(String botToken, String botUsername, DBContext db, DefaultBotOptions botOptions)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Map<String,Ability>abilities()protected Set<Integer>admins()AbilitybackupDB()This backup ability returns the object defined byDBContext.backup()as a message document.AbilitybanUser()Banned users are accumulated in the blacklist.protected Set<Integer>blacklist()protected booleancheckGlobalFlags(org.telegram.telegrambots.meta.api.objects.Update update)Test the update against the provided global flags.AbilityclaimCreator()Regular users and admins who try to claim the bot will get banned.Abilitycommands()Default format:abstract intcreatorId()AbilitydemoteAdmin()StringgetBotToken()StringgetBotUsername()protected org.telegram.telegrambots.meta.api.objects.UsergetUser(int id)Gets the user with the specified ID.protected org.telegram.telegrambots.meta.api.objects.UsergetUser(String username)Gets the user with the specified username.protected intgetUserIdSendError(String username, MessageContext ctx)Gets the user with the specified username.voidonUpdateReceived(org.telegram.telegrambots.meta.api.objects.Update update)This method contains the stream of actions that are applied on any update.AbilitypromoteAdmin()AbilityrecoverDB()Recovers the bot database usingDBContext.recover(Object).List<Reply>replies()AbilityreportCommands()Format of the report:AbilityunbanUser()Usage:/unban @usernameprotected Map<String,Integer>userIds()protected Map<Integer,org.telegram.telegrambots.meta.api.objects.User>users()-
Methods inherited from class org.telegram.telegrambots.bots.DefaultAbsSender
downloadFile, downloadFile, downloadFile, downloadFile, downloadFileAsStream, downloadFileAsStream, downloadFileAsync, downloadFileAsync, execute, execute, execute, execute, execute, execute, execute, execute, execute, execute, execute, execute, execute, execute, getBaseUrl, getOptions, sendApiMethod, sendApiMethodAsync
-
-
-
-
Field Detail
-
ADMINS
public static final String ADMINS
- See Also:
- Constant Field Values
-
USERS
public static final String USERS
- See Also:
- Constant Field Values
-
USER_ID
public static final String USER_ID
- See Also:
- Constant Field Values
-
BLACKLIST
public static final String BLACKLIST
- See Also:
- Constant Field Values
-
DEFAULT
protected static final String DEFAULT
- See Also:
- Constant Field Values
-
CLAIM
protected static final String CLAIM
- See Also:
- Constant Field Values
-
BAN
protected static final String BAN
- See Also:
- Constant Field Values
-
PROMOTE
protected static final String PROMOTE
- See Also:
- Constant Field Values
-
DEMOTE
protected static final String DEMOTE
- See Also:
- Constant Field Values
-
UNBAN
protected static final String UNBAN
- See Also:
- Constant Field Values
-
BACKUP
protected static final String BACKUP
- See Also:
- Constant Field Values
-
RECOVER
protected static final String RECOVER
- See Also:
- Constant Field Values
-
COMMANDS
protected static final String COMMANDS
- See Also:
- Constant Field Values
-
REPORT
protected static final String REPORT
- See Also:
- Constant Field Values
-
db
protected final DBContext db
-
sender
protected MessageSender sender
-
silent
protected SilentSender silent
-
-
Constructor Detail
-
BaseAbilityBot
protected BaseAbilityBot(String botToken, String botUsername, DBContext db, DefaultBotOptions botOptions)
-
-
Method Detail
-
creatorId
public abstract int creatorId()
-
users
protected Map<Integer,org.telegram.telegrambots.meta.api.objects.User> users()
- Returns:
- the map of
-
blacklist
protected Set<Integer> blacklist()
- Returns:
- a blacklist containing all the IDs of the banned users
-
onUpdateReceived
public void onUpdateReceived(org.telegram.telegrambots.meta.api.objects.Update update)
This method contains the stream of actions that are applied on any update.It will correctly handle addition of users into the DB and the execution of abilities and replies.
- Parameters:
update- the update received by Telegram's API
-
getBotToken
public String getBotToken()
- Specified by:
getBotTokenin classDefaultAbsSender
-
getBotUsername
public String getBotUsername()
-
checkGlobalFlags
protected boolean checkGlobalFlags(org.telegram.telegrambots.meta.api.objects.Update update)
Test the update against the provided global flags. The default implementation is a passthrough to all updates.This method should be overridden if the user wants to restrict bot usage to only certain updates.
- Parameters:
update- a TelegramUpdate- Returns:
- true if the update satisfies the global flags
-
getUser
protected org.telegram.telegrambots.meta.api.objects.User getUser(String username)
Gets the user with the specified username.- Parameters:
username- the username of the required user- Returns:
- the user
-
getUser
protected org.telegram.telegrambots.meta.api.objects.User getUser(int id)
Gets the user with the specified ID.- Parameters:
id- the id of the required user- Returns:
- the user
-
getUserIdSendError
protected int getUserIdSendError(String username, MessageContext ctx)
Gets the user with the specified username. If user was not found, the bot will send a message on Telegram.- Parameters:
username- the username of the required userctx- the message context with the originating user- Returns:
- the id of the user
-
reportCommands
public Ability reportCommands()
Format of the report:
[command1] - [description1]
[command2] - [description2]
...
Once you invoke it, the bot will send the available commands to the chat. This is a public ability so anyone can invoke it.
Usage:
/commands- Returns:
- the ability to report commands defined by the child bot.
-
commands
public Ability commands()
Default format:PUBLIC
[command1] - [description1]
[command2] - [description2]
GROUP_ADMIN
[command1] - [description1]
...
- Returns:
- the ability to print commands based on the privacy of the requesting user
-
backupDB
public Ability backupDB()
This backup ability returns the object defined byDBContext.backup()as a message document.This is a high-profile ability and is restricted to the CREATOR only.
Usage:
/backup- Returns:
- the ability to back-up the database of the bot
-
recoverDB
public Ability recoverDB()
Recovers the bot database usingDBContext.recover(Object).The bot recovery process hugely depends on the implementation of the recovery method of
DBContext.Usage:
/recover- Returns:
- the ability to recover the database of the bot
-
banUser
public Ability banUser()
Banned users are accumulated in the blacklist. UseDBContext.getSet(String)with name specified byBLACKLIST.Usage:
/ban @usernameNote that admins who try to ban the creator, get banned.
- Returns:
- the ability to ban the user from any kind of bot interaction
-
unbanUser
public Ability unbanUser()
Usage:/unban @username- Returns:
- the ability to unban a user
-
promoteAdmin
public Ability promoteAdmin()
- Returns:
- the ability to promote a user to a bot admin
-
demoteAdmin
public Ability demoteAdmin()
- Returns:
- the ability to demote an admin to a user
-
claimCreator
public Ability claimCreator()
Regular users and admins who try to claim the bot will get banned.- Returns:
- the ability to claim yourself as the master and creator of the bot
-
-