@dinghy-chat/sdk 0.35.0 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -72,6 +72,32 @@ await dinghy.outboundMessages.cancel({
72
72
 
73
73
  The `dedupeKey` names the message, so the cancel needs no stored ID. `cancel` also takes `{ id }`. `get(id)` and `list({ visitorId, userId, dedupeKey, status })` read sent messages. `metadata` comes back on the message and its webhooks, and the visitor never sees it.
74
74
 
75
+ ## Run a campaign from your code
76
+
77
+ Support writes the campaign `onboarding-done` in the operator app. Your server picks the moment:
78
+
79
+ ```ts
80
+ await dinghy.campaigns.send("onboarding-done", {
81
+ visitor: { userId: user.id },
82
+ data: { plan: user.plan },
83
+ });
84
+ ```
85
+
86
+ `send` skips the campaign's audience and trigger, and keeps its frequency and the visitor's opt-out. `data` fills `{{ data.plan }}` in its text. A campaign is named by its key, or by `{ id }`. To keep campaigns in your repository, save each one with `dinghy.campaigns.upsert(definition)`, which never changes its status. See the [campaigns guide](https://dinghy.chat/docs/campaigns).
87
+
88
+ ## Track what happened
89
+
90
+ ```ts
91
+ await dinghy.track({
92
+ visitor: { userId: user.id },
93
+ name: "export.finished",
94
+ properties: { rows: 1200 },
95
+ id: exportJob.id,
96
+ });
97
+ ```
98
+
99
+ Each contact keeps a count and the first and last time for each name, which conditions read as `events.export.finished.count` and `events.export.finished.lastAt`. A campaign with the trigger `{ type: "event", name: "export.finished" }` runs for the contact, with `{{ event.properties.rows }}` in its text. Properties are never stored. A repeat of a recent `id` counts once.
100
+
75
101
  ## Sign users in to the chat
76
102
 
77
103
  `identity.sign` makes the token for `Dinghy.identify` on your page. It needs `signingSecret` and `publicKey`.
@@ -122,6 +148,7 @@ It reconnects when the server ends the stream or the connection drops, and resum
122
148
  | `conversations` | `list`, `search`, `messages`, `reply`, `note`, `upload`, `receipt`, `close`, `reopen`, `snooze`, `assign`, `addLabel`, `removeLabel`, `handoff`, `typing` |
123
149
  | `contacts` | `upsert` |
124
150
  | `outboundMessages` | `send`, `get`, `list`, `cancel` |
151
+ | `campaigns` | `list`, `get`, `create`, `upsert`, `update`, `pause`, `resume`, `end`, `results`, `send` |
125
152
  | `labels` | `list` |
126
153
  | `savedReplies` | `list` |
127
154
  | `analytics` | `get` |
@@ -129,7 +156,7 @@ It reconnects when the server ends the stream or the connection drops, and resum
129
156
  | `webhooks` | `list`, `create`, `update`, `delete`, `rotateSecret`, `test`, `deliveries`, `replay`, `verify` |
130
157
  | `setup` | `snippet`, `checkInstall`, `identitySetup`, `sites`, `addSite`, `removeSite`, `widget`, `updateWidget`, `businessHours`, `updateBusinessHours`, `testVisitorMessage`, `clearTestVisitors` |
131
158
 
132
- `webhooks` and `setup` need a key with Full access. `request` reaches any route a key may call, with the same types:
159
+ `webhooks`, `setup` and the campaign methods that change a campaign (`create`, `upsert`, `update`, `pause`, `resume`, `end`) need a key with Full access. `track` is a method of its own. `request` reaches any route a key may call, with the same types:
133
160
 
134
161
  ```ts
135
162
  await dinghy.request("get", "/v1/operator/workspaces/{workspaceId}/labels");
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { type StreamItem, type StreamOptions } from "./events.ts";
2
2
  import { type HostUser } from "./identity.ts";
3
3
  import { type Answer, type Fetch, type MethodOf, type RequestArgs, type WorkspacePath } from "./request.ts";
4
- import { analytics, contacts, conversations, labels, outboundMessages, savedReplies, setup, webhookRoutes, workspace } from "./resources.ts";
4
+ import { analytics, campaigns, contacts, conversations, labels, outboundMessages, savedReplies, setup, track, webhookRoutes, workspace } from "./resources.ts";
5
5
  import { type Received, type WebhookEvent } from "./webhooks.ts";
6
6
  export type { StreamItem, StreamOptions } from "./events.ts";
7
7
  export type { HostUser } from "./identity.ts";
@@ -30,6 +30,10 @@ export declare class Dinghy {
30
30
  readonly conversations: ReturnType<typeof conversations>;
31
31
  readonly contacts: ReturnType<typeof contacts>;
32
32
  readonly outboundMessages: ReturnType<typeof outboundMessages>;
33
+ /** Reading and sending need any key. Saving, starting, pausing and ending need a Full access key. */
34
+ readonly campaigns: ReturnType<typeof campaigns>;
35
+ /** Counts an event about a contact, such as `export.finished`, for campaigns and conditions. */
36
+ readonly track: ReturnType<typeof track>;
33
37
  readonly labels: ReturnType<typeof labels>;
34
38
  readonly savedReplies: ReturnType<typeof savedReplies>;
35
39
  readonly analytics: ReturnType<typeof analytics>;
package/dist/index.js CHANGED
@@ -199,6 +199,7 @@ var CONVERSATION = `${CONVERSATIONS}/{conversationId}`;
199
199
  var MESSAGES = `${CONVERSATION}/messages`;
200
200
  var CONTACT_BY_USER = `${IN_WORKSPACE}/contacts/by-user/{userId}`;
201
201
  var OUTBOUND = `${IN_WORKSPACE}/outbound-messages`;
202
+ var CAMPAIGNS = `${IN_WORKSPACE}/campaigns`;
202
203
  var WEBHOOKS = `${IN_WORKSPACE}/webhooks`;
203
204
  var WEBHOOK = `${WEBHOOKS}/{webhookId}`;
204
205
  var withClientId = (input) => ({
@@ -251,6 +252,46 @@ function outboundMessages(request) {
251
252
  cancel: (input) => "id" in input ? request("post", `${OUTBOUND}/{id}/cancel`, { path: { id: input.id } }) : request("post", `${OUTBOUND}/cancel`, { body: input })
252
253
  };
253
254
  }
255
+ var campaignAt = (ref, tail) => ({
256
+ at: typeof ref === "string" ? `${CAMPAIGNS}/by-key/{ref}${tail}` : `${CAMPAIGNS}/{ref}${tail}`,
257
+ path: { ref: typeof ref === "string" ? ref : ref.id }
258
+ });
259
+ function campaigns(request) {
260
+ const status = (action) => (ref) => {
261
+ const { at, path } = campaignAt(ref, action);
262
+ return request("post", at, { path });
263
+ };
264
+ return {
265
+ list: (query = {}) => request("get", CAMPAIGNS, { query }),
266
+ get: (ref) => {
267
+ const { at, path } = campaignAt(ref, "");
268
+ return request("get", at, { path });
269
+ },
270
+ /** A new campaign, as a draft. Its key must be free. */
271
+ create: (body) => request("post", CAMPAIGNS, { body }),
272
+ /** Creates the campaign with this key as a draft, or replaces it. Its status stays. */
273
+ upsert: ({ key, ...rest }) => request("put", `${CAMPAIGNS}/by-key/{key}`, { path: { key }, body: { key, ...rest } }),
274
+ /** Replaces the parts the patch gives. */
275
+ update: (ref, body) => {
276
+ const { at, path } = campaignAt(ref, "");
277
+ return request("patch", at, { path, body });
278
+ },
279
+ pause: status("/pause"),
280
+ /** Starts a draft, or resumes a paused campaign. */
281
+ resume: status("/resume"),
282
+ end: status("/end"),
283
+ results: (ref, query = {}) => {
284
+ const { at, path } = campaignAt(ref, "/results");
285
+ return request("get", at, { path, query });
286
+ },
287
+ /** Runs a live campaign for one person: it skips the audience and the trigger. */
288
+ send: (ref, input) => {
289
+ const { at, path } = campaignAt(ref, "/send");
290
+ return request("post", at, { path, body: withClientId(input) });
291
+ }
292
+ };
293
+ }
294
+ var track = (request) => (body) => request("post", `${IN_WORKSPACE}/track`, { body });
254
295
  function webhookRoutes(request) {
255
296
  const at = (webhookId) => ({ path: { webhookId } });
256
297
  return {
@@ -351,6 +392,10 @@ var Dinghy = class {
351
392
  conversations;
352
393
  contacts;
353
394
  outboundMessages;
395
+ /** Reading and sending need any key. Saving, starting, pausing and ending need a Full access key. */
396
+ campaigns;
397
+ /** Counts an event about a contact, such as `export.finished`, for campaigns and conditions. */
398
+ track;
354
399
  labels;
355
400
  savedReplies;
356
401
  analytics;
@@ -369,6 +414,8 @@ var Dinghy = class {
369
414
  this.conversations = conversations(request);
370
415
  this.contacts = contacts(request);
371
416
  this.outboundMessages = outboundMessages(request);
417
+ this.campaigns = campaigns(request);
418
+ this.track = track(request);
372
419
  this.labels = labels(request);
373
420
  this.savedReplies = savedReplies(request);
374
421
  this.analytics = analytics(request);