import { CEvt, ChatEvent, ChatResponse, T } from "@simplex-chat/types"; import * as core from "./core"; export declare class ChatCommandError extends Error { message: string; response: ChatResponse; constructor(message: string, response: ChatResponse); } /** * Connection request types. * @enum {string} */ export declare enum ConnReqType { Invitation = "invitation", Contact = "contact" } /** * Bot address settings. */ export interface BotAddressSettings { /** * Automatically accept contact requests. * @default true */ autoAccept?: boolean; /** * Optional welcome message to show before connection to the users. * @default undefined (no welcome message) */ welcomeMessage?: T.MsgContent | string | undefined; /** * Business contact address. * For all requests business chats will be created where other participants can be added. * @default false */ businessAddress?: boolean; } export declare const defaultBotAddressSettings: BotAddressSettings; export type EventSubscriberFunc = (event: ChatEvent & { type: K; }) => void | Promise; export type EventSubscribers = { [K in CEvt.Tag]?: EventSubscriberFunc; }; /** * Database configuration. The native library is built against exactly one * backend (see `simplex_backend` / `SIMPLEX_BACKEND` at install time); this * type makes the caller state which one they are targeting so field names * can't lie about their meaning. */ export type DbConfig = { /** SQLite backend (default). */ type: "sqlite"; /** File prefix — two schema files are named `_chat.db` and `_agent.db`. */ filePrefix: string; /** Optional SQLCipher encryption key. Empty/omitted = unencrypted. */ encryptionKey?: string; } | { /** PostgreSQL backend (Linux x86_64 only, libpq5 required). */ type: "postgres"; /** Schema prefix used to namespace tables. Defaults to `"simplex_v1"` when omitted. */ schemaPrefix?: string; /** PostgreSQL connection string (e.g. `postgres://user:pass@host/db`). */ connectionString: string; }; /** * Main API class for interacting with the chat core library. */ export declare class ChatApi { protected ctrl_: bigint | undefined; private receiveEvents; private eventsLoop; private subscribers; private receivers; private constructor(); /** * Initializes the ChatApi. * @param {DbConfig} db - Database configuration (sqlite or postgres). * @param {core.MigrationConfirmation} [confirm=core.MigrationConfirmation.YesUp] - Migration confirmation mode. */ static init(db: DbConfig, confirm?: core.MigrationConfirmation): Promise; /** * Start chat controller. Must be called with the existing user profile. */ startChat(): Promise; /** * Stop chat controller. * Must be called before closing the database. * Usually doesn't need to be called in chat bots. */ stopChat(): Promise; /** * Close chat database. * Usually doesn't need to be called in chat bots. */ close(): Promise; private runEventsLoop; /** * Subscribe multiple event handlers at once. * @param subscribers - An object mapping event types (CEvt.Tag) to their subscriber functions. * @throws {Error} If the same function is subscribed to event. */ on(subscribers: EventSubscribers): void; /** * Subscribe a handler to a specific event. * @param {CEvt.Tag} event - The event type to subscribe to. * @param subscriber - The subscriber function for the event. * @throws {Error} If the same function is subscribed to event. */ on(event: K, subscriber: EventSubscriberFunc): void; private on_; /** * Subscribe a handler to any event. * @param receiver - The receiver function for any event. * @throws {Error} If the same function is subscribed to event. */ onAny(receiver: EventSubscriberFunc): void; /** * Subscribe a handler to a specific event to be delivered one time. * @param {CEvt.Tag} event - The event type to subscribe to. * @param subscriber - The subscriber function for the event. * @throws {Error} If the same function is subscribed to event. */ once(event: K, subscriber: EventSubscriberFunc): void; /** * Waits for specific event, with an optional predicate. * Returns `undefined` on timeout if specified. */ wait(event: K): Promise; wait(event: K, predicate: ((event: ChatEvent & { type: K; }) => boolean) | undefined): Promise; wait(event: K, timeout: number): Promise; wait(event: K, predicate: ((event: ChatEvent & { type: K; }) => boolean) | undefined, timeout: number): Promise; /** * Unsubscribe all or a specific handler from a specific event. * @param {CEvt.Tag} event - The event type to unsubscribe from. * @param subscriber - An optional subscriber function for the event. */ off(event: K, subscriber?: EventSubscriberFunc | undefined): void; /** * Unsubscribe all or a specific handler from any events. * @param receiver - An optional subscriber function for the event. */ offAny(receiver?: EventSubscriberFunc | undefined): void; /** * Chat controller is initialized */ get initialized(): boolean; /** * Chat controller is started */ get started(): boolean; /** * Chat controller reference */ get ctrl(): bigint; sendChatCmd(cmd: string): Promise; recvChatEvent(wait?: number): Promise; /** * Create bot address. * Network usage: interactive. */ apiCreateUserAddress(userId: number): Promise; /** * Deletes a user address. * Network usage: background. */ apiDeleteUserAddress(userId: number): Promise; /** * Get bot address and settings. * Network usage: no. */ apiGetUserAddress(userId: number): Promise; /** * Add address to bot profile. * Network usage: interactive. */ apiSetProfileAddress(userId: number, enable: boolean): Promise; /** * Set bot address settings. * Network usage: interactive. */ apiSetAddressSettings(userId: number, { autoAccept, welcomeMessage, businessAddress }: BotAddressSettings): Promise; /** * Send messages. * Network usage: background. */ apiSendMessages(chat: [T.ChatType, number] | T.ChatRef | T.ChatInfo, messages: T.ComposedMessage[], liveMessage?: boolean): Promise; /** * Send text message. * Network usage: background. */ apiSendTextMessage(chat: [T.ChatType, number] | T.ChatRef | T.ChatInfo, text: string, inReplyTo?: number): Promise; /** * Send text message in reply to received message. * Network usage: background. */ apiSendTextReply(chatItem: T.AChatItem, text: string): Promise; /** * Update message. * Network usage: background. */ apiUpdateChatItem(chatType: T.ChatType, chatId: number, chatItemId: number, msgContent: T.MsgContent, liveMessage: false): Promise; /** * Delete message. * Network usage: background. */ apiDeleteChatItems(chatType: T.ChatType, chatId: number, chatItemIds: number[], deleteMode: T.CIDeleteMode): Promise; /** * Moderate message. Requires Moderator role (and higher than message author's). * Network usage: background. */ apiDeleteMemberChatItem(groupId: number, chatItemIds: number[]): Promise; /** * Add/remove message reaction. * Network usage: background. */ apiChatItemReaction(chatType: T.ChatType, chatId: number, chatItemId: number, add: boolean, reaction: T.MsgReaction): Promise; /** * Receive file. * Network usage: no. */ apiReceiveFile(fileId: number): Promise; /** * Cancel file. * Network usage: background. */ apiCancelFile(fileId: number): Promise; /** * Add contact to group. Requires bot to have Admin role. * Network usage: interactive. */ apiAddMember(groupId: number, contactId: number, memberRole: T.GroupMemberRole): Promise; /** * Join group. * Network usage: interactive. */ apiJoinGroup(groupId: number): Promise; /** * Accept group member. Requires Admin role. * Network usage: background. */ apiAcceptMember(groupId: number, groupMemberId: number, memberRole: T.GroupMemberRole): Promise; /** * Set members role. Requires Admin role. * Network usage: background. */ apiSetMembersRole(groupId: number, groupMemberIds: number[], memberRole: T.GroupMemberRole): Promise; /** * Block members. Requires Moderator role. * Network usage: background. */ apiBlockMembersForAll(groupId: number, groupMemberIds: number[], blocked: boolean): Promise; /** * Remove members. Requires Admin role. * Network usage: background. */ apiRemoveMembers(groupId: number, memberIds: number[], withMessages?: boolean): Promise; /** * Leave group. * Network usage: background. */ apiLeaveGroup(groupId: number): Promise; /** * Get group members. * Network usage: no. */ apiListMembers(groupId: number): Promise; /** * Create group. * Network usage: no. */ apiNewGroup(userId: number, groupProfile: T.GroupProfile): Promise; /** * Update group profile. * Network usage: background. */ apiUpdateGroupProfile(groupId: number, groupProfile: T.GroupProfile): Promise; /** * Create group link. * Network usage: interactive. */ apiCreateGroupLink(groupId: number, memberRole: T.GroupMemberRole): Promise; /** * Set member role for group link. * Network usage: no. */ apiSetGroupLinkMemberRole(groupId: number, memberRole: T.GroupMemberRole): Promise; /** * Delete group link. * Network usage: background. */ apiDeleteGroupLink(groupId: number): Promise; /** * Get group link. * Network usage: no. */ apiGetGroupLink(groupId: number): Promise; apiGetGroupLinkStr(groupId: number): Promise; /** * Create 1-time invitation link. * Network usage: interactive. */ apiCreateLink(userId: number): Promise; /** * Determine SimpleX link type and if the bot is already connected via this link. * Network usage: interactive. */ apiConnectPlan(userId: number, connectionLink: string): Promise<[T.ConnectionPlan, T.CreatedConnLink]>; /** * Connect via prepared SimpleX link. The link can be 1-time invitation link, contact address or group link * Network usage: interactive. */ apiConnect(userId: number, incognito: boolean, preparedLink?: T.CreatedConnLink): Promise; /** * Connect via SimpleX link as string in the active user profile. * Network usage: interactive. */ apiConnectActiveUser(connLink: string): Promise; private handleConnectResult; /** * Accept contact request. * Network usage: interactive. */ apiAcceptContactRequest(contactReqId: number): Promise; /** * Reject contact request. The user who sent the request is **not notified**. * Network usage: no. */ apiRejectContactRequest(contactReqId: number): Promise; /** * Get contacts. * Network usage: no. */ apiListContacts(userId: number): Promise; /** * Get groups. * Network usage: no. */ apiListGroups(userId: number, contactId?: number, search?: string): Promise; /** * Get chat previews (paginated). * Network usage: no. * * Prefer this over apiListContacts / apiListGroups for any scan: those * methods load every record into memory in a single response and will fail * on large databases. */ apiGetChats(userId: number, pagination: T.PaginationByTime, query?: T.ChatListQuery, pendingConnections?: boolean): Promise; /** * Delete chat. * Network usage: background. */ apiDeleteChat(chatType: T.ChatType, chatId: number, deleteMode?: T.ChatDeleteMode): Promise; /** * Set group custom data. * Network usage: no. */ apiSetGroupCustomData(groupId: number, customData?: object): Promise; /** * Set contact custom data. * Network usage: no. */ apiSetContactCustomData(contactId: number, customData?: object): Promise; /** * Set auto-accept member contacts. * Network usage: no. */ apiSetAutoAcceptMemberContacts(userId: number, onOff: boolean): Promise; /** * Get chat items. * Network usage: no. */ apiGetChat(chatType: T.ChatType, chatId: number, count: number): Promise; /** * Get active user profile * Network usage: no. */ apiGetActiveUser(): Promise; /** * Create new user profile * Network usage: no. */ apiCreateActiveUser(profile?: T.Profile): Promise; /** * Get all user profiles * Network usage: no. */ apiListUsers(): Promise; /** * Set active user profile * Network usage: no. */ apiSetActiveUser(userId: number, viewPwd?: string): Promise; /** * Delete user profile. * Network usage: background. */ apiDeleteUser(userId: number, delSMPQueues: boolean, viewPwd?: string): Promise; /** * Update user profile. * Network usage: background. */ apiUpdateProfile(userId: number, profile: T.Profile): Promise; /** * Configure chat preference overrides for the contact. * Network usage: background. */ apiSetContactPrefs(contactId: number, preferences: T.Preferences): Promise; /** * Create a direct message contact with a group member. * Returns the created contact. * Network usage: interactive. */ apiCreateMemberContact(groupId: number, groupMemberId: number): Promise; /** * Send a direct message invitation to a group member contact. * The contact must have been created with {@link apiCreateMemberContact}. * Network usage: interactive. */ apiSendMemberContactInvitation(contactId: number, message?: T.MsgContent | string): Promise; }