Overview
TheBotServiceImpl class provides the main interface for bot operations on OpenChat. It handles bot initialization, avatar management, group/community/channel operations, and message management.
Constructor
The bot’s data model containing status, name, groups, and saved messages
Service for making OpenChat API calls
Service for logging bot operations
Bot Initialization
initBot
Initializes the bot by registering it with the OpenChat platform. Requires 10T cycles for registration fee.The bot’s username (must be unique on OpenChat)
Optional display name for the bot
Returns
#ok() on success, or #err(message) with error details:"Initializing"- Bot is currently initializing"Initialized"- Bot is already initialized"AlreadyRegistered"- Username is already registered"Not enough cycles. Required: {n}"- Insufficient cycles provided"Error: {msg}"- Other registration errors
#NotInitialized before calling.
Bot Configuration
setAvatar
Sets the bot’s avatar image. Maximum size is 800KB.Avatar configuration containing the document to use as avatar
#ok(#Success)- Avatar set successfully#err("Avatar too big")- Avatar exceeds 800KB limit#err("Avatar not found")- No avatar provided in args#err("Error: {msg}")- OpenChat API error
getBotStatus
Returns the current initialization status of the bot.One of:
#NotInitialized- Bot has not been initialized#Initializing- Bot initialization in progress#Initialized- Bot is ready to use
setBotStatus
Manually sets the bot status to initialized.initBot.
Group Operations
joinGroup
Joins an OpenChat group.The principal/canister ID of the group to join
Optional invite code for private groups
#ok("OK")- Successfully joined group#err("Already in group")- Bot is already a member#err("Error")- Join operation failed#err(msg)- Other errors from OpenChat
sendGroupMessage
Sends a message to a group with any content type.The group’s canister ID
Message content (text, image, poll, etc.)
Optional thread root message index for threaded replies
On success, returns
#ok(#Success({ event_index; message_index; message_id }))Possible error variants:#MessageEmpty- Content is empty#TextTooLong(maxLength)- Text exceeds maximum length#NotAuthorized- Bot lacks permission#UserNotInChannel- Bot is not a group member#UserSuspended- Bot is suspended#RulesNotAccepted- Group rules must be accepted
sendTextGroupMessage
Convenience function to send a text message to a group.The group’s canister ID
Plain text message content
Optional thread root message index
editGroupMessage
Edits a previously sent group message.The group’s canister ID
The ID of the message to edit (from send response)
Thread root index if editing a threaded message
New message content
editTextGroupMessage
Convenience function to edit a text message in a group.getGroupMessagesByIndex
Retrieves specific messages from a group by their indexes.The group’s canister ID
Array of message indexes to retrieve
Optional timestamp of latest known update for caching
Returns the requested messages with their full content and metadata
getLatestGroupMessageIndex
Gets the index of the most recent message in a group.The group’s canister ID
Returns the latest message index, or
null if unavailableCommunity Operations
joinCommunity
Joins an OpenChat community.The community’s canister ID
Optional invite code for private communities
The bot’s principal ID
#ok("OK")- Successfully joined community#err("Already in community")- Bot is already a member#err("GateCheckFailed")- Bot doesn’t meet community requirements#err("NotInvited")- Invite required but not provided#err("UserBlocked")- Bot is blocked from community#err("MemberLimitReached")- Community is full#err("CommunityFrozen")- Community is frozen#err("PrivateCommunity")- Cannot access private community info
joinChannel
Joins a channel within a community.The community’s canister ID
The channel ID within the community
Optional invite code for private channels
Similar error cases to
joinCommunity, plus:#err("UserNotInCommunity")- Must join community first#err("UserSuspended")- Bot is suspended from community#err("ChannelNotFound")- Channel doesn’t exist
sendChannelMessage
Sends a message to a community channel.The community’s canister ID
The channel ID
Message content
Optional thread root message index
Same response format as
sendGroupMessage, with additional variants:#ChannelNotFound- Channel doesn’t exist#UserNotInCommunity- Bot not in community#UserNotInChannel- Bot not in channel#CommunityFrozen- Community is frozen#CommunityRulesNotAccepted- Community rules must be accepted
editChannelMessage
Edits a message in a community channel.The community’s canister ID
The channel ID
The message ID to edit
Thread root index if editing a threaded message
New message content
Message Storage
The bot service provides key-value storage for message IDs, useful for tracking messages you want to edit later.saveMessageId
Saves a message ID with an associated key.Unique identifier for this message
The message ID to save
getMessageId
Retrieves a previously saved message ID.The key used when saving the message ID
Returns the message ID if found, or
null if not founddeleteMessageId
Deletes a saved message ID.The key of the message ID to delete
deleteAllMessageIds
Clears all saved message IDs.Utility Functions
getNNSProposalMessageData
Extracts NNS proposal information from a message event.A message event from OpenChat
#ok({ proposalId; messageIndex })- Successfully extracted proposal data#err("Not a governance proposal")- Message doesn’t contain a proposal#err("Not a NNS proposal")- Proposal is SNS, not NNS
Constants
- BOT_REGISTRATION_FEE: 10T cycles (10,000,000,000,000)
- MAX_AVATAR_SIZE: 800KB (819,200 bytes)
- USER_INDEX_CANISTER:
"4bkt6-4aaaa-aaaaf-aaaiq-cai"
Error Handling
All async functions in BotService returnResult.Result<T, Text> types. Always handle both success and error cases:
SendMessageResponse), check the specific variant: