@dawntech/blip-tools 0.2.0 → 0.3.1
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/dist/index.d.ts +29 -3
- package/dist/index.js +41 -3
- package/dist/modules/blip-media.d.ts +17 -0
- package/dist/modules/blip-media.js +55 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -7,10 +7,12 @@ import { Ticket } from './types/ticket.js';
|
|
|
7
7
|
import { CampaignNotification } from './types/campaign-notification.js';
|
|
8
8
|
import { CampaignNotificationStatus } from './types/campaign-notification-status.js';
|
|
9
9
|
import BlipBucket from './modules/blip-bucket.js';
|
|
10
|
+
import BlipMedia from './modules/blip-media.js';
|
|
10
11
|
export { BlipError, type Ticket, type AttendanceHourContainer, type Contact, type Template };
|
|
11
12
|
export default class Blip {
|
|
12
13
|
private api;
|
|
13
14
|
bucket: BlipBucket;
|
|
15
|
+
media: BlipMedia;
|
|
14
16
|
constructor(params: BlipConstructor);
|
|
15
17
|
/**
|
|
16
18
|
* Sends a template-based message to any user. For sending messages to clients who are active, use `sendTemplateMessage` instead.
|
|
@@ -38,21 +40,39 @@ export default class Blip {
|
|
|
38
40
|
/**
|
|
39
41
|
* Sends a template-based message to an active user. For sending messages to clients who have never interacted or are inactive, use `sendTemplate` instead.
|
|
40
42
|
*/
|
|
41
|
-
sendTemplateMessage({ identity, template }: {
|
|
43
|
+
sendTemplateMessage({ identity, template, metadata, }: {
|
|
42
44
|
identity: string;
|
|
43
45
|
template: Template;
|
|
46
|
+
metadata?: string;
|
|
44
47
|
}): Promise<void>;
|
|
45
48
|
/**
|
|
46
49
|
* Sends a simple text message to an active user. For sending messages to clients who have never interacted or are inactive, use `sendTemplate` instead.
|
|
47
50
|
*/
|
|
48
|
-
sendSimpleMessage({ identity, content }: {
|
|
51
|
+
sendSimpleMessage({ identity, content, metadata }: {
|
|
49
52
|
identity: string;
|
|
50
53
|
content: string;
|
|
54
|
+
metadata?: string;
|
|
51
55
|
}): Promise<void>;
|
|
52
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Sends a text message to an active user with a midia. For sending messages to clients who have never interacted or are inactive, use `sendTemplate` instead.
|
|
58
|
+
*/
|
|
59
|
+
sendMediaMessage({ identity, text, metadata, uri, type, title, aspectRatio, size, previewUri, }: {
|
|
60
|
+
type?: string;
|
|
61
|
+
uri: string;
|
|
62
|
+
identity: string;
|
|
63
|
+
text: string;
|
|
64
|
+
metadata?: string;
|
|
65
|
+
title: string;
|
|
66
|
+
aspectRatio?: string;
|
|
67
|
+
size?: number;
|
|
68
|
+
previewUri?: string;
|
|
69
|
+
previewType?: string;
|
|
70
|
+
}): Promise<void>;
|
|
71
|
+
sendMessageWithButtons({ identity, message, buttons, metadata, }: {
|
|
53
72
|
identity: string;
|
|
54
73
|
message: string;
|
|
55
74
|
buttons: string[];
|
|
75
|
+
metadata?: string;
|
|
56
76
|
}): Promise<void>;
|
|
57
77
|
updateContact({ identity, contactData }: {
|
|
58
78
|
identity: string;
|
|
@@ -67,6 +87,12 @@ export default class Blip {
|
|
|
67
87
|
value: unknown;
|
|
68
88
|
identity: string;
|
|
69
89
|
}): Promise<void>;
|
|
90
|
+
moveUser({ identity, botIdentity, botFlowId, botStateId, }: {
|
|
91
|
+
identity: string;
|
|
92
|
+
botIdentity: string;
|
|
93
|
+
botFlowId: string;
|
|
94
|
+
botStateId: string;
|
|
95
|
+
}): Promise<void>;
|
|
70
96
|
getWhatsAppIdentity(phone: string): Promise<string>;
|
|
71
97
|
createTrackEvent({ category, action, identity }: {
|
|
72
98
|
category: string;
|
package/dist/index.js
CHANGED
|
@@ -7,10 +7,12 @@ import validateInstance from './helpers/validate-instance.js';
|
|
|
7
7
|
import BlipError from './exceptions/blip-error.js';
|
|
8
8
|
import encodeBlipParams from './helpers/encode-blip-params.js';
|
|
9
9
|
import BlipBucket from './modules/blip-bucket.js';
|
|
10
|
+
import BlipMedia from './modules/blip-media.js';
|
|
10
11
|
export { BlipError };
|
|
11
12
|
export default class Blip {
|
|
12
13
|
api;
|
|
13
14
|
bucket;
|
|
15
|
+
media;
|
|
14
16
|
constructor(params) {
|
|
15
17
|
validateInstance(params);
|
|
16
18
|
this.api = axios.create({
|
|
@@ -21,6 +23,7 @@ export default class Blip {
|
|
|
21
23
|
timeout: 30000,
|
|
22
24
|
});
|
|
23
25
|
this.bucket = new BlipBucket(this.api);
|
|
26
|
+
this.media = new BlipMedia(this.api);
|
|
24
27
|
}
|
|
25
28
|
/**
|
|
26
29
|
* Sends a template-based message to any user. For sending messages to clients who are active, use `sendTemplateMessage` instead.
|
|
@@ -111,7 +114,7 @@ export default class Blip {
|
|
|
111
114
|
/**
|
|
112
115
|
* Sends a template-based message to an active user. For sending messages to clients who have never interacted or are inactive, use `sendTemplate` instead.
|
|
113
116
|
*/
|
|
114
|
-
async sendTemplateMessage({ identity, template }) {
|
|
117
|
+
async sendTemplateMessage({ identity, template, metadata, }) {
|
|
115
118
|
try {
|
|
116
119
|
const response = await this.api.post('/messages', {
|
|
117
120
|
id: randomUUID(),
|
|
@@ -121,6 +124,7 @@ export default class Blip {
|
|
|
121
124
|
type: 'template',
|
|
122
125
|
template,
|
|
123
126
|
},
|
|
127
|
+
metadata,
|
|
124
128
|
});
|
|
125
129
|
validateResponse(response);
|
|
126
130
|
}
|
|
@@ -131,13 +135,14 @@ export default class Blip {
|
|
|
131
135
|
/**
|
|
132
136
|
* Sends a simple text message to an active user. For sending messages to clients who have never interacted or are inactive, use `sendTemplate` instead.
|
|
133
137
|
*/
|
|
134
|
-
async sendSimpleMessage({ identity, content }) {
|
|
138
|
+
async sendSimpleMessage({ identity, content, metadata }) {
|
|
135
139
|
try {
|
|
136
140
|
const response = await this.api.post('/messages', {
|
|
137
141
|
id: randomUUID(),
|
|
138
142
|
to: identity,
|
|
139
143
|
type: 'text/plain',
|
|
140
144
|
content,
|
|
145
|
+
metadata,
|
|
141
146
|
});
|
|
142
147
|
validateResponse(response);
|
|
143
148
|
}
|
|
@@ -145,7 +150,33 @@ export default class Blip {
|
|
|
145
150
|
handleError(error);
|
|
146
151
|
}
|
|
147
152
|
}
|
|
148
|
-
|
|
153
|
+
/**
|
|
154
|
+
* Sends a text message to an active user with a midia. For sending messages to clients who have never interacted or are inactive, use `sendTemplate` instead.
|
|
155
|
+
*/
|
|
156
|
+
async sendMediaMessage({ identity, text, metadata, uri, type, title, aspectRatio = '1:1', size, previewUri, }) {
|
|
157
|
+
try {
|
|
158
|
+
const response = await this.api.post('/messages', {
|
|
159
|
+
id: randomUUID(),
|
|
160
|
+
to: identity,
|
|
161
|
+
type: 'application/vnd.lime.media-link+json',
|
|
162
|
+
content: {
|
|
163
|
+
text,
|
|
164
|
+
uri,
|
|
165
|
+
type,
|
|
166
|
+
title,
|
|
167
|
+
aspectRatio,
|
|
168
|
+
size,
|
|
169
|
+
previewUri,
|
|
170
|
+
},
|
|
171
|
+
metadata,
|
|
172
|
+
});
|
|
173
|
+
validateResponse(response);
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
handleError(error);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
async sendMessageWithButtons({ identity, message, buttons, metadata, }) {
|
|
149
180
|
try {
|
|
150
181
|
const response = await this.api.post('/messages', {
|
|
151
182
|
id: randomUUID(),
|
|
@@ -156,6 +187,7 @@ export default class Blip {
|
|
|
156
187
|
text: message,
|
|
157
188
|
options: buttons.map((button, index) => ({ order: index + 1, text: button })),
|
|
158
189
|
},
|
|
190
|
+
metadata,
|
|
159
191
|
});
|
|
160
192
|
validateResponse(response);
|
|
161
193
|
}
|
|
@@ -228,6 +260,12 @@ export default class Blip {
|
|
|
228
260
|
handleError(error);
|
|
229
261
|
}
|
|
230
262
|
}
|
|
263
|
+
async moveUser({ identity, botIdentity, botFlowId, botStateId, }) {
|
|
264
|
+
await Promise.all([
|
|
265
|
+
this.setContextVariable({ identity, variableName: 'master-state', value: botIdentity }),
|
|
266
|
+
this.setContextVariable({ identity, variableName: `stateid@${botFlowId}`, value: botStateId }),
|
|
267
|
+
]);
|
|
268
|
+
}
|
|
231
269
|
async getWhatsAppIdentity(phone) {
|
|
232
270
|
try {
|
|
233
271
|
const response = await this.api.post('/commands', {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
|
2
|
+
export default class BlipMedia {
|
|
3
|
+
private api;
|
|
4
|
+
constructor(api: AxiosInstance);
|
|
5
|
+
/**
|
|
6
|
+
* Uploads an media file to Blip storage to be used later when sending messages
|
|
7
|
+
* @param content A Blob containing a image, PDF or video
|
|
8
|
+
* @returns A public URL
|
|
9
|
+
*/
|
|
10
|
+
uploadMedia(content: Blob): Promise<string>;
|
|
11
|
+
/**
|
|
12
|
+
* Generates a new public URI to access blip chat media. Blip medias have expiration dates so you need to generate new ones to access the data.
|
|
13
|
+
* @param expiredMediaUri Blip media expired URI
|
|
14
|
+
* @returns A new public URI
|
|
15
|
+
*/
|
|
16
|
+
refreshMediaUri(expiredMediaUri: string): Promise<string>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { randomUUID } from 'crypto';
|
|
3
|
+
import validateResponse from '../helpers/validate-response.js';
|
|
4
|
+
import handleError from '../helpers/handle-error.js';
|
|
5
|
+
export default class BlipMedia {
|
|
6
|
+
api;
|
|
7
|
+
constructor(api) {
|
|
8
|
+
this.api = api;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Uploads an media file to Blip storage to be used later when sending messages
|
|
12
|
+
* @param content A Blob containing a image, PDF or video
|
|
13
|
+
* @returns A public URL
|
|
14
|
+
*/
|
|
15
|
+
async uploadMedia(content) {
|
|
16
|
+
try {
|
|
17
|
+
const response = await this.api.post('/commands', {
|
|
18
|
+
id: randomUUID(),
|
|
19
|
+
to: 'postmaster@media.msging.net',
|
|
20
|
+
method: 'get',
|
|
21
|
+
uri: '/upload-media-uri',
|
|
22
|
+
});
|
|
23
|
+
validateResponse(response);
|
|
24
|
+
const uploadResponse = await axios.post(response.data.resource, content, {
|
|
25
|
+
headers: { 'Content-Type': 'application/octet-stream' },
|
|
26
|
+
});
|
|
27
|
+
return uploadResponse.data.mediaUri;
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
throw handleError(error);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Generates a new public URI to access blip chat media. Blip medias have expiration dates so you need to generate new ones to access the data.
|
|
35
|
+
* @param expiredMediaUri Blip media expired URI
|
|
36
|
+
* @returns A new public URI
|
|
37
|
+
*/
|
|
38
|
+
async refreshMediaUri(expiredMediaUri) {
|
|
39
|
+
try {
|
|
40
|
+
const response = await this.api.post('/commands', {
|
|
41
|
+
id: randomUUID(),
|
|
42
|
+
to: 'postmaster@media.msging.net',
|
|
43
|
+
method: 'set',
|
|
44
|
+
type: 'text/plain',
|
|
45
|
+
uri: '/refresh-media-uri',
|
|
46
|
+
resource: expiredMediaUri,
|
|
47
|
+
});
|
|
48
|
+
validateResponse(response);
|
|
49
|
+
return response.data.resource;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
throw handleError(error);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|