Overview
TheOCService module provides a high-level interface for interacting with OpenChat canisters. It wraps the low-level OpenChat APIs and provides error handling, making it easier to build bots that interact with OpenChat groups, communities, and channels.
This is the primary service layer you’ll use to integrate your bot with OpenChat.
OCServiceImpl Class
The main class that implements the OpenChat service interface.Bot Registration
registerBot
Registers a new bot with OpenChat.The canister ID of the OpenChat user index canister
The username for the bot (must be unique)
Optional display name for the bot
Returns
#ok(InitializeBotResponse) on success, or #err(Text) with error message on failuresetAvatar
Sets the avatar for the bot.The canister ID of the OpenChat user index canister
Avatar configuration with
avatar_id field (optional Nat)Returns
#Success or #UserNotFoundUser Information
userSummary
Retrieves summary information about a user.The canister ID of the OpenChat user index canister
Optional user ID to look up
Optional username to look up
Returns user summary information including username, display name, avatar, bot status, and diamond membership status
Group & Community Information
publicGroupSummary
Retrieves public information about a group.The canister ID of the group
Optional invite code if the group requires one
Returns
#Success with group summary or #NotAuthorizedpublicCommunitySummary
Retrieves public information about a community.The canister ID of the community
Optional invite code if required
Returns community summary with channels, permissions, and membership details
Joining Groups & Communities
joinGroup
Joins a group chat.The canister ID of the group to join
Join arguments including chat_id, invite_code, and correlation_id
joinCommunity
Joins a community.The canister ID of the community
Join arguments including community_id, user_id, principal, invite_code, and bot flags
Returns
#Success with community summary, or error variantjoinChannel
Joins a channel within a community.The canister ID of the community
Join arguments including community_id, channel_id, user_id, and other metadata
Returns channel summary on success, or error variant indicating the failure reason
Messaging
sendGroupMessage
Sends a message to a group chat.The canister ID of the group
The sender’s username
Optional display name
The message content (Text, Image, File, Poll, etc.)
Unique message identifier
Optional thread root message index if replying in a thread
Returns
#Success with event_index and message_index, or error variantsendChannelMessage
Sends a message to a community channel.The canister ID of the community
The channel ID within the community
The sender’s username
Optional display name
The message content
Unique message identifier
Optional thread root message index
Returns success with message indices or error variant
editGroupMessage
Edits an existing group message.The canister ID of the group
The ID of the message to edit
Optional thread root if editing a thread message
The new message content
Returns
#Success, #MessageNotFound, #CallerNotInGroup, #UserSuspended, or #ChatFrozeneditChannelMessage
Edits an existing channel message.The canister ID of the community
The channel ID
The ID of the message to edit
Optional thread root index
The new message content
Returns success or error variant indicating the failure reason
Message Retrieval
messagesByMessageIndex
Retrieves messages by their message indices.The canister ID of the group
Arguments including:
thread_root_message_index: Optional thread rootmessages: Array of message indices to retrievelatest_known_update: Optional timestamp for optimization
Returns
#Success with messages array, latest_event_index, and chat_last_updated timestampError Handling
All methods returnResult.Result<T, Text> types:
#ok(value)- Successful operation with result value#err(message)- Operation failed with error message