Skip to content

Permission System

This page documents all permission checking functions in Alita Robot.

  • Total Functions: 26
  • Location: alita/utils/chat_status/ (chat_status.go, access.go, permission_responder.go)
FunctionReturnsDescription
CanBotDeleteboolCanBotDelete checks if the bot has permission to delete m…
CanBotPinboolCanBotPin checks if the bot has permission to pin message…
CanBotPromoteboolCanBotPromote checks if the bot has permission to promote…
CanBotRestrictboolCanBotRestrict checks if the bot has permission to restri…
IsBotAdminboolIsBotAdmin checks if the bot has administrator privileges…
IsChannelIdboolIsChannelId checks if an ID represents a Telegram channel…
IsValidUserIdboolIsValidUserId checks if an ID represents a valid Telegram…
IsApprovedboolIsApproved checks if a user is in the approved whitelist …
RequireBotAdminboolRequireBotAdmin ensures the bot has administrator privile…
RequireGroupboolRequireGroup ensures the command is being used in a group…
RequirePrivateboolRequirePrivate ensures the command is being used in a pri…
RequireUserAdminboolRequireUserAdmin ensures a user has administrator privile…
RequireUserOwnerboolRequireUserOwner ensures a user is the chat creator/owner…
RequireUser*gotgbot.UserRequireUser extracts the effective user from the update context safely…
CanInviteboolCanInvite checks if the bot and user have permissions to …
CanUserChangeInfoboolCanUserChangeInfo checks if a user has permission to chan…
CanUserDeleteboolCanUserDelete checks if a user has permission to delete m…
CanUserPinboolCanUserPin checks if a user has permission to pin message…
CanUserPromoteboolCanUserPromote checks if a user has permission to promote…
CanUserRestrictboolCanUserRestrict checks if a user has permission to restri…
IsUserAdminboolIsUserAdmin checks if a user has administrator privileges…
IsUserBanProtectedboolIsUserBanProtected checks if a user is protected from bei…
IsUserInChatboolIsUserInChat checks if a user is currently a member of th…
GetEffectiveUser*gotgbot.UserGetEffectiveUser safely extracts the user from the context without nil panics…
GetChat*gotgbot.ChatGetChat safely retrieves a chat by ID with caching…
CheckDisabledCmdboolCheckDisabledCmd checks if a command is disabled in the c…
func CanBotDelete(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat) bool

CanBotDelete checks if the bot has permission to delete messages in the chat. Validates the bot’s CanDeleteMessages permission.

Parameters:

  • b
  • ctx
  • chat
func CanBotPin(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat) bool

CanBotPin checks if the bot has permission to pin messages in the chat. Validates the bot’s CanPinMessages permission.

Parameters:

  • b
  • ctx
  • chat
func CanBotPromote(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat) bool

CanBotPromote checks if the bot has permission to promote/demote members in the chat. Validates the bot’s CanPromoteMembers permission.

Parameters:

  • b
  • ctx
  • chat
func CanBotRestrict(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat) bool

CanBotRestrict checks if the bot has permission to restrict members in the chat. Validates the bot’s CanRestrictMembers permission.

Parameters:

  • b
  • ctx
  • chat
func IsBotAdmin(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat) bool

IsBotAdmin checks if the bot has administrator privileges in the specified chat. Returns true for private chats (bot is always “admin” in private). For groups, verifies the bot’s actual admin status.

Parameters:

  • b
  • ctx
  • chat

Located in alita/utils/chat_status/permission_responder.go.

Centralizes permission-failure messaging with support for callback-query answers and chat replies.

// Create a responder
responder := chat_status.NewPermissionResponder(b, ctx)
// Respond with default message
responder.Respond()
// Respond with a reply to the original message
responder.WithReply()
// Respond with fallback if reply fails
responder.WithReplyFallback()

Functions:

FunctionDescription
NewPermissionResponder()Creates a new responder instance
Respond()Sends permission failure message
WithReply()Sends as a reply to the original message
WithReplyFallback()Sends as reply, falls back to regular message if reply fails
func IsChannelId(id int64) bool

IsChannelId checks if an ID represents a Telegram channel. Channel IDs have the format -100XXXXXXXXXX (-100 prefix followed by 10+ digits).

Parameters:

  • id
func IsValidUserId(id int64) bool

IsValidUserId checks if an ID represents a valid Telegram user. User IDs are always positive (> 0). Channel IDs are negative with format -100XXXXXXXXXX (< -1000000000000). Regular chat/group IDs are negative but in a different range.

Parameters:

  • id
func IsApproved(b *gotgbot.Bot, chatID, userID int64) bool

IsApproved checks if a user is in the approved whitelist for a chat. Approved users are immune to anti-spam measures (antiflood, blacklists, locks, captcha, antispam). This is a simple delegation to the DB layer for consistent usage in watcher handlers.

Parameters:

  • b
  • chatID
  • userID
func RequireBotAdmin(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat) bool

RequireBotAdmin ensures the bot has administrator privileges in the chat. Uses IsBotAdmin internally to perform the check.

Parameters:

  • b
  • ctx
  • chat
func RequireGroup(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat) bool

RequireGroup ensures the command is being used in a group chat. Returns false for private chats. nolint:dupl // RequirePrivate/RequireGroup have symmetric logic

Parameters:

  • b
  • ctx
  • chat
func RequirePrivate(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat) bool

RequirePrivate ensures the command is being used in a private chat. Returns false for group chats and supergroups. nolint:dupl // RequirePrivate/RequireGroup have symmetric logic

Parameters:

  • b
  • ctx
  • chat
func RequireUserAdmin(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat, userId int64) bool

RequireUserAdmin ensures a user has administrator privileges in the chat. Uses IsUserAdmin internally to perform the check.

Parameters:

  • b
  • ctx
  • chat
  • userId
func RequireUserOwner(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat, userId int64) bool

RequireUserOwner ensures a user is the chat creator/owner. Checks for “creator” status specifically, not just administrator.

Parameters:

  • b
  • ctx
  • chat
  • userId
func RequireUser(b *gotgbot.Bot, ctx *ext.Context) *gotgbot.User

RequireUser extracts the effective user from the update context. Returns nil for channel messages where ctx.EffectiveSender is nil. Always check the return value before accessing fields.

Parameters:

  • b
  • ctx
func CanInvite(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat, msg *gotgbot.Message) bool

CanInvite checks if the bot and user have permissions to generate invite links. Returns true immediately if the chat has a public username. Validates both bot and user permissions for invite link generation.

Parameters:

  • b
  • ctx
  • chat
  • msg
func CanUserChangeInfo(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat, userId int64) bool

CanUserChangeInfo checks if a user has permission to change chat information. Handles anonymous admins and validates the CanChangeInfo permission.

Parameters:

  • b
  • ctx
  • chat
  • userId
func CanUserDelete(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat, userId int64) bool

CanUserDelete checks if a user has permission to delete messages in the chat. Handles anonymous admins and validates the CanDeleteMessages permission.

Parameters:

  • b
  • ctx
  • chat
  • userId
func CanUserPin(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat, userId int64) bool

CanUserPin checks if a user has permission to pin messages in the chat. Handles anonymous admins and validates the CanPinMessages permission.

Parameters:

  • b
  • ctx
  • chat
  • userId
func CanUserPromote(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat, userId int64) bool

CanUserPromote checks if a user has permission to promote/demote other members. Handles anonymous admins and validates the CanPromoteMembers permission.

Parameters:

  • b
  • ctx
  • chat
  • userId
func CanUserRestrict(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat, userId int64) bool

CanUserRestrict checks if a user has permission to restrict other members. Handles anonymous admins and validates the CanRestrictMembers permission.

Parameters:

  • b
  • ctx
  • chat
  • userId
func IsUserAdmin(b *gotgbot.Bot, chatID, userId int64) bool

IsUserAdmin checks if a user has administrator privileges in a chat. Uses caching system to avoid repeated API calls and handles special Telegram admin accounts. Returns true if the user is an admin, creator, or special Telegram account.

Parameters:

  • b
  • chatID
  • userId
func IsUserBanProtected(b *gotgbot.Bot, ctx *ext.Context, chat *gotgbot.Chat, userId int64) bool

IsUserBanProtected checks if a user is protected from being banned. Returns true for private chats, admins, and special Telegram accounts. Used to prevent banning of administrators and system accounts.

Parameters:

  • b
  • ctx
  • chat
  • userId
func IsUserInChat(b *gotgbot.Bot, chat *gotgbot.Chat, userId int64) bool

IsUserInChat checks if a user is currently a member of the specified chat. Returns false for special Telegram accounts and users with “left” or “kicked” status.

Parameters:

  • b
  • chat
  • userId
func GetEffectiveUser(ctx *ext.Context) *gotgbot.User

GetEffectiveUser safely extracts the user from the context without nil panics. Returns nil for channel messages where ctx.EffectiveSender is nil. Use this instead of direct ctx.EffectiveSender.User access to avoid nil pointer dereferences.

Parameters:

  • ctx
func CheckDisabledCmd(bot *gotgbot.Bot, msg *gotgbot.Message, cmd string) bool

CheckDisabledCmd checks if a command is disabled in the chat and handles deletion if configured. Returns true if the command should be blocked, false if it should proceed. Skips checks for private chats and admin users. If command is disabled for non-admin users, optionally deletes the message based on chat settings.

Parameters:

  • bot
  • msg
  • cmd
IDDescription
1087968824Anonymous Admin Bot (GroupAnonymousBot)
777000Telegram System Account
136817688SendAsChannel Bot (for users sending messages as channel)
func (m moduleStruct) myCommand(b *gotgbot.Bot, ctx *ext.Context) error {
chat := ctx.EffectiveChat
user := ctx.EffectiveSender.User
// Check if user is admin
if !chat_status.RequireUserAdmin(b, ctx, chat, user.Id) {
return ext.EndGroups
}
// Check if bot can restrict
if !chat_status.CanBotRestrict(b, ctx, chat) {
return ext.EndGroups
}
// Proceed with action...
return ext.EndGroups
}