import type { HttpClient } from '../http'; import { InboxMedia } from './audience/inbox-media'; import { type CampaignReceiver, type ReceiverEncoding } from '../internal/receiver-file'; import { type BulkAudienceInput, type ResolvedAudience } from './campaign-audience'; import type { Campaign, CampaignNumbersFilter, CreateCampaignInput, ListCampaignsQuery, TriggerCampaignInput, UpdateCampaignInput } from '../types/campaign'; import type { Page } from '../types/common'; /** Everything `createBulk` needs: a normal campaign, with an audience instead of `filters`. */ export interface CreateBulkCampaignInput extends Omit { /** The recipients. Numbers, CSV text, parsed rows, or receivers you built. */ audience: BulkAudienceInput; /** Overrides the dispatch route. Defaults to `bulk_campaign`; rarely needed. */ type?: string; /** Aborts the upload. The campaign is not created if the upload is cancelled. */ signal?: AbortSignal; } /** * What `createBulk` did, beyond creating the campaign. * * `receivers` is a **count**, deliberately matching what the same field means on * a {@link Campaign} rather than carrying the resolved list. Two reasons: the * caller already holds the data they passed in, and a name that means a number in * one place and an array in another is the kind of thing that reads fine and then * renders `[object Object]` on someone's screen. */ export interface BulkCampaignResult extends Omit { campaign: Campaign; /** * Recipients actually written into the file. * * This -- not `campaign.receivers`, which is `0` until the campaign is sent -- * is the number to show a user now. */ receivers: number; /** Public URL of the encrypted receiver file, as stored on the campaign. */ fileUrl: string; } /** An uploader, so the receiver file's destination can be swapped or faked in tests. */ export type ReceiverUploader = Pick; /** * One bucket of a group campaign: a slice of the recipients, and what should * send it. * * Which of `channel` and `pairIndex` applies depends on the campaign's * `distributeBy`. Neither is validated here -- a bucket with the wrong one is a * mistake the sending service will report, and guessing which the caller meant * would be worse than passing it along. */ export interface GroupBucket { /** This bucket's recipients. Any of the four audience shapes. */ audience: BulkAudienceInput; /** `distributeBy: 'channel'` -- the channel id this bucket is sent through. */ channel?: string; /** `distributeBy: 'template'` -- which message in `messages` renders this bucket. */ pairIndex?: number; /** * Extra keys carried onto the filter entry verbatim, beside `numbers` and * `hash`. The campaign contract passes anything it does not recognise straight * through to the sending service, so this is the seam for distribution metadata * this SDK does not model. */ extra?: Record; } export interface CreateGroupCampaignInput extends Omit { /** * What the buckets are split by. * * `channel` sends each bucket through a different channel; `template` renders * each with a different message. Every bucket is stamped with this, and the * contract refuses a campaign whose buckets disagree. */ distributeBy: 'channel' | 'template'; buckets: readonly GroupBucket[]; /** Overrides the stored type. Defaults to `group_campaign`; rarely needed. */ type?: string; signal?: AbortSignal; } /** One bucket's outcome. Same order as the buckets given. */ export interface GroupBucketResult { receivers: number; skipped: number; duplicates: number; /** Public URL of this bucket's encrypted file. */ fileUrl: string; channel?: string; pairIndex?: number; } export interface GroupCampaignResult { campaign: Campaign; /** Buckets that had recipients and were uploaded. Empty ones are not here. */ buckets: GroupBucketResult[]; /** Totals across every bucket. */ receivers: number; skipped: number; duplicates: number; } export declare class Campaigns { private readonly http; private readonly media; constructor(http: HttpClient, media?: ReceiverUploader); list(query?: Partial): Promise>; get(id: string): Promise; /** * Creates a campaign from a payload you have already assembled. * * **Creating does not send.** The campaign comes back `pending` with no * `scheduledAt`: a draft you can keep editing, invisible to the sender until * {@link trigger} arms it. * * ```ts * const campaign = await crm.campaigns.create({ name: 'Diwali offer', messages }); * // ...show it for review... * await crm.campaigns.trigger(campaign.id); // send now * await crm.campaigns.trigger(campaign.id, { scheduledAt }); // or then * ``` * * For a bulk recipient list, prefer {@link createBulk} -- it does the * encrypting and uploading that a file-backed audience requires, which is not * something a caller should have to reproduce. */ create(input: CreateCampaignInput): Promise; /** * Creates a campaign addressed to a list of numbers you supply directly. * * This is the "bulk campaign" path, and the SDK does the whole of it: the * recipient list never travels as part of the campaign, because a million * numbers is not a request body. Instead it is encrypted into a file, uploaded * to storage, and the campaign stores a pointer to it -- which the sending * service fetches and decrypts when the campaign runs. * * ```ts * // Pasted numbers, no variables * const { campaign, receivers } = await crm.campaigns.createBulk({ * name: 'Diwali offer', * audience: { numbers: '919000000001, 919000000002\n919000000003' }, * messages: [{ channel: { id: channelId }, message: template }], * }); * * // A CSV, with every column available as a template variable * await crm.campaigns.createBulk({ * name: 'Renewal reminder', * audience: { csv: await file.text(), recipientColumn: 'phone' }, * messages: [{ channel: { id: channelId }, message: template }], * }); * * // Neither of the above has been sent yet. Arm it when you are ready: * await crm.campaigns.trigger(campaign.id, { scheduledAt: '2026-11-01T04:30:00.000Z' }); * ``` * * ## What it does, in order * * 1. **Resolves the audience.** Numbers are reduced to digits; CSV or TSV text * is parsed with quoted fields honoured; every column becomes a per-recipient * variable. Rows with no usable number are skipped and duplicates dropped, * both counted in the result rather than passed over in silence. * 2. **Encrypts the list.** AES-256-CBC under a secret generated fresh for this * one file. See `internal/receiver-file.ts` for the exact scheme. * 3. **Uploads it** as a `document` through the media endpoint: a presigned URL * is issued and the bytes go straight to storage, so the file never passes * through the API. * 4. **Creates the campaign** with `type: 'bulk_campaign'` and * `filters: [{numbers: , hash: }]`. * * ## Two things worth knowing * * **The returned `campaign.receivers` is `0`, and that is correct.** The * audience is resolved when the campaign is sent, not when it is created. Use * the `receivers` on this result -- the number actually written into the file -- * for anything you show the user now. * * **The file is readable by anyone holding both the URL and the `hash`.** The * URL is a public storage link, so the secret stored alongside it on the * campaign is the only thing protecting the list. It is a credential: do not log * it, and do not copy a `filters` entry from one campaign into another system. * * Throws `validation_failed` if the audience resolves to no recipients, or to * more than a million. */ createBulk(input: CreateBulkCampaignInput): Promise; /** * Creates a campaign whose recipients are split into buckets, each sent through * a different channel or rendered with a different message. * * ```ts * const { campaign, buckets, receivers } = await crm.campaigns.createGroup({ * name: 'Diwali offer', * distributeBy: 'channel', * buckets: [ * { channel: channelA, audience: { rows: north, recipientColumn: 'phone' } }, * { channel: channelB, audience: { rows: south, recipientColumn: 'phone' } }, * ], * messages: [{ channel: { id: channelA }, message: template }], * }); * ``` * * ## How this differs from `createBulk` * * Three ways, and each of them is a thing that would be wrong if you built the * payload by hand: * * - **One file per bucket, not one per campaign.** Each bucket is encrypted * under its own secret and uploaded separately, and `filters` carries one * entry per bucket. This is the only audience mode where `filters` has more * than one element. * - **A different file format.** A bucket file is a `||`-delimited table with a * header row, not the JSON array a bulk file holds. The reading side chooses * by campaign type, so the wrong encoding decrypts cleanly and then parses to * nothing. * - **Columns are fixed by the first recipient.** A table has one header, so * every row carries the same variables in the same order. A bucket whose rows * have differing columns will have the missing ones filled in as empty. * * Everything else is the same: numbers are reduced to digits, CSV is parsed * with quoted fields honoured, duplicates and unusable rows are dropped and * counted -- per bucket, and summed on the result. * * ## Empty buckets are dropped, not refused * * A bucket whose audience resolves to nobody is left out of `filters` * altogether, matching the panel: distributing a list across six channels * routinely leaves one or two with nothing in them, and that is not an error. * The call fails only when **every** bucket is empty. * * De-duplication is per bucket. A number appearing in two buckets is sent to * twice -- which is usually the intent, since the buckets are different * channels, and is not something this can second-guess. */ createGroup(input: CreateGroupCampaignInput): Promise; /** * Encrypts and uploads a receiver list, answering with the audience filter it * produced. * * The file half of {@link createBulk}, exposed because the two halves can fail * for unrelated reasons. Holding the filter lets you retry a failed campaign * creation -- or create several campaigns against one uploaded list -- without * re-uploading, which matters when the list is large. * * The returned `hash` is the decryption secret. Keep it with the URL or the * file becomes unreadable; there is no copy of it anywhere else. */ uploadReceivers(receivers: readonly CampaignReceiver[], signal?: AbortSignal, /** * How the list is encoded inside the file. `json` for a bulk campaign's * single file, `table` for a group campaign's bucket. The reading side picks * by campaign type, so this has to match the campaign it is for. */ encoding?: ReceiverEncoding): Promise; update(id: string, input: UpdateCampaignInput): Promise; /** * Arms a campaign -- the only call that makes one actually go out. * * ```ts * await crm.campaigns.trigger(id); // now * await crm.campaigns.trigger(id, { scheduledAt: isoInstant }); // then * ``` * * "Now" means a minute from now by default, which is the window `cancel()` * works in. The server stamps `triggerInfo` -- the caller, their address, the * moment -- as part of this call, so every send has one authorising act behind * it; nothing you pass can set it. * * Only a `pending` campaign can be armed; anything else answers `409`. * Triggering one that is armed but not yet picked up simply re-arms it. */ trigger(id: string, input?: TriggerCampaignInput): Promise; /** {@link trigger} with a required time, under the name the panel uses. */ schedule(id: string, scheduledAt: string): Promise; cancel(id: string): Promise; delete(id: string): Promise; }