import qs from 'qs' import type { ApiClient } from '../../core/ApiClient' import { BaseCrudApi } from '../../core/BaseCrudApi' import type { Message, CreateMessagePayload, UpdateMessagePayload, ForwardMessagesPayload, ForwardMessagesResult, SendVcardsPayload, SendReactionPayload, MagicTextPayload, MagicTextResult, CheckSummaryPayload, CheckSummaryResult, BulkDestroyPayload, OutOfRangeResult, LoadAroundQuery, } from './types' export class MessagesApi extends BaseCrudApi { constructor(client: ApiClient) { super(client, '/messages') } /** Forwards one or more messages to one or more contacts (max 20 messages to 5 contacts). */ forward( body: ForwardMessagesPayload, headers?: Record, ): Promise { return this.client.post( `${this.basePath}/forward`, body, headers, ) } /** Revokes (soft-deletes) a sent message so it is no longer visible to the recipient. */ revoke(id: string, headers?: Record): Promise { return this.client.post(`${this.basePath}/${id}/revoke`, undefined, headers) } /** Removes a previously sent reaction from a message. */ revokeReaction(id: string, headers?: Record): Promise { return this.client.put(`${this.basePath}/${id}/revoke-reaction`, undefined, headers) } /** Sends one or more contacts as vCard messages to a given contact. */ sendVcards(body: SendVcardsPayload, headers?: Record): Promise { return this.client.post( `${this.basePath}/send-vcards`, body, headers, ) } /** Sends an emoji reaction to a message. */ sendReaction( id: string, body: SendReactionPayload, headers?: Record, ): Promise { return this.client.post( `${this.basePath}/${id}/send-reaction`, body, headers, ) } /** Uses AI to rewrite or correct a message text with a given tone (e.g. "correct", "professional", "casual"). */ magicText(body: MagicTextPayload, headers?: Record): Promise { return this.client.post( `${this.basePath}/magic-text`, body, headers, ) } /** Checks whether an AI-generated summary already exists for a given ticket. */ checkSummary( body: CheckSummaryPayload, headers?: Record, ): Promise { return this.client.post( `${this.basePath}/check-summary`, body, headers, ) } /** * Deletes multiple messages matching the given where clause. * @permissions messages.destroy */ bulkDestroy(body: BulkDestroyPayload, headers?: Record): Promise { return this.client.request( 'DELETE', `${this.basePath}/many`, body, headers, ) } /** Triggers an AI-generated summary for the conversation associated with the given message. */ summarize(id: string, headers?: Record): Promise { return this.client.post(`${this.basePath}/${id}/summarize`, undefined, headers) } /** Checks whether a message is out of the visible range (i.e. there are more than `limit` messages after it). */ outOfRange( id: string, limit?: number, headers?: Record, ): Promise { const queryString = limit ? `?${qs.stringify({ limit })}` : '' return this.client.get( `${this.basePath}/${id}/out-of-range${queryString}`, headers, ) } /** Re-downloads and syncs the file attachment for a message from the messaging service. */ syncFile(id: string, headers?: Record): Promise { return this.client.post(`${this.basePath}/${id}/sync-file`, undefined, headers) } /** Loads messages surrounding a target message (N before and N after), useful for jumping to a specific point in a conversation. */ around( id: string, query?: LoadAroundQuery, headers?: Record, ): Promise { const queryString = query ? `?${qs.stringify(query)}` : '' return this.client.get(`${this.basePath}/${id}/around${queryString}`, headers) } }