@dawntech/blip-tools 0.1.6 → 0.1.8

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.
@@ -1,4 +1,4 @@
1
1
  export default class BlipError extends Error {
2
2
  code: number;
3
- constructor(message: string, code: number);
3
+ constructor(message: string, code?: number);
4
4
  }
@@ -1,6 +1,6 @@
1
1
  export default class BlipError extends Error {
2
2
  code;
3
- constructor(message, code) {
3
+ constructor(message, code = 0) {
4
4
  super(message);
5
5
  this.code = code;
6
6
  }
@@ -0,0 +1,2 @@
1
+ import { BlipConstructor } from '../types/blip-constructor.js';
2
+ export default function validateInstance(params: BlipConstructor): void;
@@ -0,0 +1,9 @@
1
+ import BlipError from '../exceptions/blip-error.js';
2
+ export default function validateInstance(params) {
3
+ if (!params.contract) {
4
+ throw new BlipError('A contract must be specified.');
5
+ }
6
+ if (!params.authToken) {
7
+ throw new BlipError('An authToken must be provided.');
8
+ }
9
+ }
package/dist/index.d.ts CHANGED
@@ -1,15 +1,17 @@
1
1
  import { AxiosInstance } from 'axios';
2
2
  import { Template } from './types/template.js';
3
3
  import { Contact } from './types/contact.js';
4
+ import { BlipConstructor } from './types/blip-constructor.js';
5
+ import BlipError from './exceptions/blip-error.js';
6
+ export { BlipError };
4
7
  export default class Blip {
5
8
  api: AxiosInstance;
6
- constructor({ contract, authToken }: {
7
- contract: string;
8
- authToken: string;
9
- });
10
- sendTemplate({ campaignName, campaignType, masterState, flowId, stateId, channelType, phone, templateName, params, }: {
9
+ constructor(params: BlipConstructor);
10
+ /**
11
+ * Sends a template-based message to any user. For sending messages to clients who are active, use `sendTemplateMessage` instead.
12
+ */
13
+ sendIndividualActiveCampaign({ campaignName, masterState, flowId, stateId, channelType, phone, templateName, params, }: {
11
14
  campaignName: string;
12
- campaignType: string;
13
15
  masterState: string;
14
16
  flowId: string;
15
17
  stateId: string;
@@ -25,10 +27,16 @@ export default class Blip {
25
27
  getAvailableContextVariables({ identity }: {
26
28
  identity: string;
27
29
  }): Promise<Record<string, unknown>>;
30
+ /**
31
+ * Sends a template-based message to an active user. For sending messages to clients who have never interacted or are inactive, use `sendTemplate` instead.
32
+ */
28
33
  sendTemplateMessage({ identity, template }: {
29
34
  identity: string;
30
35
  template: Template;
31
36
  }): Promise<void>;
37
+ /**
38
+ * Sends a simple text message to an active user. For sending messages to clients who have never interacted or are inactive, use `sendTemplate` instead.
39
+ */
32
40
  sendSimpleMessage({ identity, content }: {
33
41
  identity: string;
34
42
  content: string;
@@ -40,9 +48,16 @@ export default class Blip {
40
48
  getContact({ identity }: {
41
49
  identity: string;
42
50
  }): Promise<Contact>;
43
- setVariable({ value, variableName, identity }: {
51
+ getWhatsAppTemplates(): Promise<Contact>;
52
+ setContextVariable({ value, variableName, identity, }: {
44
53
  variableName: string;
45
54
  value: unknown;
46
55
  identity: string;
47
56
  }): Promise<void>;
57
+ getWhatsAppIdentity(phone: string): Promise<string>;
58
+ createTrackEvent({ category, action, identity }: {
59
+ category: string;
60
+ action: string;
61
+ identity?: string;
62
+ }): Promise<void>;
48
63
  }
package/dist/index.js CHANGED
@@ -3,18 +3,25 @@ import { randomUUID } from 'crypto';
3
3
  import validateResponse from './helpers/validate-response.js';
4
4
  import handleError from './helpers/handle-error.js';
5
5
  import formatTemplateParams from './helpers/format-template-params.js';
6
+ import validateInstance from './helpers/validate-instance.js';
7
+ import BlipError from './exceptions/blip-error.js';
8
+ export { BlipError };
6
9
  export default class Blip {
7
10
  api;
8
- constructor({ contract, authToken }) {
11
+ constructor(params) {
12
+ validateInstance(params);
9
13
  this.api = axios.create({
10
- baseURL: `https://${contract}.http.msging.net`,
14
+ baseURL: `https://${params.contract}.http.msging.net`,
11
15
  headers: {
12
- Authorization: `Key ${authToken}`,
16
+ Authorization: `Key ${params.authToken}`,
13
17
  },
14
18
  timeout: 30000,
15
19
  });
16
20
  }
17
- async sendTemplate({ campaignName, campaignType, masterState, flowId, stateId, channelType, phone, templateName, params, }) {
21
+ /**
22
+ * Sends a template-based message to any user. For sending messages to clients who are active, use `sendTemplateMessage` instead.
23
+ */
24
+ async sendIndividualActiveCampaign({ campaignName, masterState, flowId, stateId, channelType, phone, templateName, params, }) {
18
25
  try {
19
26
  const response = await this.api.post('/commands', {
20
27
  id: randomUUID(),
@@ -25,7 +32,7 @@ export default class Blip {
25
32
  resource: {
26
33
  campaign: {
27
34
  name: campaignName,
28
- campaignType,
35
+ campaignType: 'Individual',
29
36
  masterState,
30
37
  flowId,
31
38
  stateId,
@@ -78,6 +85,9 @@ export default class Blip {
78
85
  throw handleError(error);
79
86
  }
80
87
  }
88
+ /**
89
+ * Sends a template-based message to an active user. For sending messages to clients who have never interacted or are inactive, use `sendTemplate` instead.
90
+ */
81
91
  async sendTemplateMessage({ identity, template }) {
82
92
  try {
83
93
  const response = await this.api.post('/messages', {
@@ -95,6 +105,9 @@ export default class Blip {
95
105
  handleError(error);
96
106
  }
97
107
  }
108
+ /**
109
+ * Sends a simple text message to an active user. For sending messages to clients who have never interacted or are inactive, use `sendTemplate` instead.
110
+ */
98
111
  async sendSimpleMessage({ identity, content }) {
99
112
  try {
100
113
  const response = await this.api.post('/messages', {
@@ -143,13 +156,29 @@ export default class Blip {
143
156
  throw handleError(error);
144
157
  }
145
158
  }
146
- async setVariable({ value, variableName, identity }) {
159
+ async getWhatsAppTemplates() {
147
160
  try {
161
+ const response = await this.api.post('/commands', {
162
+ id: randomUUID(),
163
+ to: 'postmaster@wa.gw.msging.net',
164
+ method: 'get',
165
+ uri: '/message-templates',
166
+ });
167
+ validateResponse(response);
168
+ return response.data.resource.data;
169
+ }
170
+ catch (error) {
171
+ throw handleError(error);
172
+ }
173
+ }
174
+ async setContextVariable({ value, variableName, identity, }) {
175
+ try {
176
+ const resourceType = typeof value === 'object' ? 'application/vnd.lime.contact+json' : 'text/plain';
148
177
  const response = await this.api.post('/commands', {
149
178
  id: randomUUID(),
150
179
  method: 'set',
151
180
  uri: `/contexts/${identity}/${variableName}`,
152
- type: 'application/vnd.lime.contact+json',
181
+ type: resourceType,
153
182
  resource: value,
154
183
  });
155
184
  validateResponse(response);
@@ -158,4 +187,42 @@ export default class Blip {
158
187
  handleError(error);
159
188
  }
160
189
  }
190
+ async getWhatsAppIdentity(phone) {
191
+ try {
192
+ const response = await this.api.post('/commands', {
193
+ id: randomUUID(),
194
+ to: 'postmaster@wa.gw.msging.net',
195
+ method: 'get',
196
+ uri: `lime://wa.gw.msging.net/accounts/+${phone}`,
197
+ });
198
+ validateResponse(response);
199
+ return response.data.resource.identity;
200
+ }
201
+ catch (error) {
202
+ throw handleError(error);
203
+ }
204
+ }
205
+ async createTrackEvent({ category, action, identity }) {
206
+ try {
207
+ const body = {
208
+ id: randomUUID(),
209
+ to: 'postmaster@analytics.msging.net',
210
+ method: 'set',
211
+ type: 'application/vnd.iris.eventTrack+json',
212
+ uri: '/event-track',
213
+ resource: {
214
+ category,
215
+ action,
216
+ },
217
+ };
218
+ if (identity && body.resource) {
219
+ body.resource.contact = { identity };
220
+ }
221
+ const response = await this.api.post('/commands', body);
222
+ validateResponse(response);
223
+ }
224
+ catch (error) {
225
+ throw handleError(error);
226
+ }
227
+ }
161
228
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dawntech/blip-tools",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Node package for Blip API",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",