@whitewall/blip-sdk 0.0.136 → 0.0.138

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.
Files changed (54) hide show
  1. package/dist/cjs/client.js +1 -1
  2. package/dist/cjs/client.js.map +1 -1
  3. package/dist/esm/client.js +1 -1
  4. package/dist/esm/client.js.map +1 -1
  5. package/package.json +2 -2
  6. package/src/client.ts +117 -0
  7. package/src/index.ts +6 -0
  8. package/src/namespaces/account.ts +729 -0
  9. package/src/namespaces/activecampaign.ts +285 -0
  10. package/src/namespaces/analytics.ts +230 -0
  11. package/src/namespaces/billing.ts +17 -0
  12. package/src/namespaces/builder.ts +52 -0
  13. package/src/namespaces/configurations.ts +19 -0
  14. package/src/namespaces/context.ts +67 -0
  15. package/src/namespaces/desk.ts +679 -0
  16. package/src/namespaces/media.ts +39 -0
  17. package/src/namespaces/namespace.ts +125 -0
  18. package/src/namespaces/plugins.ts +223 -0
  19. package/src/namespaces/portal.ts +402 -0
  20. package/src/namespaces/scheduler.ts +88 -0
  21. package/src/namespaces/whatsapp.ts +383 -0
  22. package/src/sender/bliperror.ts +42 -0
  23. package/src/sender/enveloperesolver.ts +148 -0
  24. package/src/sender/gateway/customgatewaysender.ts +43 -0
  25. package/src/sender/http/httpsender.ts +94 -0
  26. package/src/sender/index.ts +7 -0
  27. package/src/sender/plugin/communication.ts +72 -0
  28. package/src/sender/plugin/pluginsender.ts +75 -0
  29. package/src/sender/security.ts +33 -0
  30. package/src/sender/sender.ts +145 -0
  31. package/src/sender/sessionnegotiator.ts +175 -0
  32. package/src/sender/tcp/tcpsender.ts +252 -0
  33. package/src/sender/throttler.ts +36 -0
  34. package/src/sender/websocket/websocketsender.ts +175 -0
  35. package/src/types/account.ts +84 -0
  36. package/src/types/analytics.ts +18 -0
  37. package/src/types/billing.ts +15 -0
  38. package/src/types/command.ts +47 -0
  39. package/src/types/commons.ts +16 -0
  40. package/src/types/desk.ts +51 -0
  41. package/src/types/envelope.ts +9 -0
  42. package/src/types/flow.ts +327 -0
  43. package/src/types/index.ts +13 -0
  44. package/src/types/message.ts +116 -0
  45. package/src/types/node.ts +86 -0
  46. package/src/types/notification.ts +18 -0
  47. package/src/types/plugins.ts +51 -0
  48. package/src/types/portal.ts +39 -0
  49. package/src/types/reason.ts +22 -0
  50. package/src/types/session.ts +22 -0
  51. package/src/types/whatsapp.ts +84 -0
  52. package/src/utils/odata.ts +114 -0
  53. package/src/utils/random.ts +3 -0
  54. package/src/utils/uri.ts +46 -0
@@ -0,0 +1,402 @@
1
+ import type { BlipClient } from '../client.ts'
2
+ import { BlipError } from '../sender/bliperror.ts'
3
+ import { PluginSender } from '../sender/plugin/pluginsender.ts'
4
+ import type { Tenant } from '../types/account.ts'
5
+ import { type Identity, Node, type PossiblyNode } from '../types/node.ts'
6
+ import type { Application, ApplicationUserInfo, TenantSubscription, TenantUserInfo } from '../types/portal.ts'
7
+ import { uri } from '../utils/uri.ts'
8
+ import { type ConsumeOptions, Namespace, type SendCommandOptions } from './namespace.ts'
9
+
10
+ export class PortalNamespace extends Namespace {
11
+ constructor(blipClient: BlipClient, defaultOptions?: SendCommandOptions) {
12
+ super(blipClient, 'portal', { ...defaultOptions, domain: 'blip.ai' })
13
+ }
14
+
15
+ public async getInstalledPlugins(opts?: ConsumeOptions) {
16
+ const configurations = await this.blipClient.account.getConfigurations({
17
+ ...opts,
18
+ ownerIdentity: this.identity,
19
+ })
20
+ if ('Plugins' in configurations) {
21
+ const plugins = JSON.parse(configurations.Plugins) as Record<string, { name: string; url: string }>
22
+ return Object.entries(plugins).map(([key, value]) => ({
23
+ id: key,
24
+ name: value.name,
25
+ url: value.url,
26
+ }))
27
+ }
28
+
29
+ return []
30
+ }
31
+
32
+ public async deletePlugin(id: string, opts?: ConsumeOptions): Promise<void>
33
+ public async deletePlugin(url: string | RegExp, opts?: ConsumeOptions): Promise<void>
34
+ public async deletePlugin(idOrUrl: string | RegExp, opts?: ConsumeOptions): Promise<void> {
35
+ const configurations = await this.blipClient.account.getConfigurations({
36
+ ...opts,
37
+ ownerIdentity: this.identity,
38
+ })
39
+
40
+ if ('Plugins' in configurations) {
41
+ const plugins = JSON.parse(configurations.Plugins) as Record<string, { name: string; url: string }>
42
+ for (const id in plugins) {
43
+ if (
44
+ (idOrUrl instanceof RegExp && idOrUrl.test(plugins[id].url)) ||
45
+ (typeof idOrUrl === 'string' && (idOrUrl === id || plugins[id].url === idOrUrl))
46
+ ) {
47
+ delete plugins[id]
48
+ }
49
+ }
50
+
51
+ return await this.blipClient.account.setConfigurations(
52
+ { Plugins: JSON.stringify(plugins) },
53
+ { ...opts, ownerIdentity: this.identity },
54
+ )
55
+ }
56
+ }
57
+
58
+ public async addPlugin(
59
+ id: string,
60
+ settings: {
61
+ name: string
62
+ url: string
63
+ },
64
+ opts?: ConsumeOptions,
65
+ ): Promise<void> {
66
+ const configurations = await this.blipClient.account.getConfigurations({
67
+ ...opts,
68
+ ownerIdentity: this.identity,
69
+ })
70
+
71
+ const plugins = 'Plugins' in configurations ? JSON.parse(configurations.Plugins) : {}
72
+ plugins[id] = settings
73
+
74
+ return await this.blipClient.account.setConfigurations(
75
+ { Plugins: JSON.stringify(plugins) },
76
+ { ...opts, ownerIdentity: this.identity },
77
+ )
78
+ }
79
+
80
+ public async updatePlugin(
81
+ id: string,
82
+ settings: Partial<{
83
+ name: string
84
+ url: string
85
+ }>,
86
+ opts?: ConsumeOptions,
87
+ ): Promise<void> {
88
+ const configurations = await this.blipClient.account.getConfigurations({
89
+ ...opts,
90
+ ownerIdentity: this.identity,
91
+ })
92
+
93
+ if ('Plugins' in configurations) {
94
+ const plugins = JSON.parse(configurations.Plugins) as Record<string, { name: string; url: string }>
95
+ if (id in plugins) {
96
+ plugins[id] = { ...plugins[id], ...settings }
97
+ return await this.blipClient.account.setConfigurations(
98
+ { Plugins: JSON.stringify(plugins) },
99
+ { ...opts, ownerIdentity: this.identity },
100
+ )
101
+ } else {
102
+ throw new Error(`Plugin with id "${id}" not found`)
103
+ }
104
+ } else {
105
+ throw new Error('No plugins found')
106
+ }
107
+ }
108
+
109
+ public getApplication(bot?: PossiblyNode, opts?: ConsumeOptions): Promise<Application & { accessKey: string }> {
110
+ if (bot) {
111
+ return this.sendCommand(
112
+ {
113
+ method: 'get',
114
+ uri: uri`/applications/${Node.from(bot, 'msging.net')}`,
115
+ },
116
+ opts,
117
+ )
118
+ } else {
119
+ if (!this.blipClient.context.isOnPortal()) {
120
+ throw new Error('This command is only allowed for portal senders')
121
+ }
122
+
123
+ const sender = PluginSender.check(this.blipClient)
124
+ return sender.channel.post('getApplication', null)
125
+ }
126
+ }
127
+
128
+ public getApplications(tenantId: string, opts?: ConsumeOptions): Promise<Array<Application>> {
129
+ return this.sendCommand(
130
+ {
131
+ method: 'get',
132
+ uri: uri`/applications?${{ tenantId }}`,
133
+ },
134
+ {
135
+ collection: true,
136
+ // This route doesn't handle pagination correctly, it will always fetch all
137
+ fetchall: false,
138
+ ...opts,
139
+ },
140
+ )
141
+ }
142
+
143
+ /** @returns all applications where the current user is an admin */
144
+ public getAdminApplications(tenantId?: string, opts?: ConsumeOptions): Promise<Array<Identity>> {
145
+ return this.sendCommand(
146
+ {
147
+ method: 'get',
148
+ uri: uri`/applications-admin?${{ tenantId }}`,
149
+ },
150
+ {
151
+ collection: true,
152
+ // This route doesn't handle pagination correctly, it will always fetch all
153
+ fetchall: false,
154
+ ...opts,
155
+ },
156
+ )
157
+ }
158
+
159
+ public async getParentApplications(): Promise<Array<Application>> {
160
+ const current = await this.getApplication()
161
+ const applications = await this.getApplications(current.tenantId)
162
+
163
+ const parentApps = await Promise.all(
164
+ applications.map(async (app) => {
165
+ const client = this.blipClient.as(app.shortName)
166
+ const template = await client.account.getTemplateType()
167
+ if (template === 'master') {
168
+ const children = await client.account.getChildren()
169
+ if (children.some((child) => child.identifier === current.shortName)) {
170
+ return app
171
+ }
172
+ }
173
+
174
+ // Return undefined if the app is not a parent
175
+ return undefined
176
+ }),
177
+ )
178
+
179
+ return parentApps.filter((app) => app !== undefined)
180
+ }
181
+
182
+ public async getTenant(tenantId: string): Promise<Tenant> {
183
+ return await this.sendCommand({
184
+ method: 'get',
185
+ uri: uri`/tenants/${tenantId}`,
186
+ })
187
+ }
188
+
189
+ public async tenantExists(tenantId: string): Promise<boolean> {
190
+ try {
191
+ await this.sendCommand({
192
+ method: 'get',
193
+ uri: uri`/tenants/${tenantId}`,
194
+ })
195
+ return true
196
+ } catch (err) {
197
+ if (err instanceof BlipError) {
198
+ if (err.code === 67) {
199
+ return false
200
+ } else if (err.code === 35) {
201
+ return true
202
+ }
203
+ }
204
+
205
+ throw err
206
+ }
207
+ }
208
+
209
+ public async getTenants(opts?: ConsumeOptions): Promise<Array<Tenant>> {
210
+ return await this.sendCommand(
211
+ {
212
+ method: 'get',
213
+ uri: uri`/tenants-mine`,
214
+ },
215
+ {
216
+ collection: true,
217
+ ...opts,
218
+ },
219
+ )
220
+ }
221
+
222
+ public async getTenantUsers(
223
+ tenantId: string,
224
+ opts?: ConsumeOptions,
225
+ ): Promise<Array<Omit<TenantUserInfo, 'creationDate' | 'updateDate'>>> {
226
+ return await this.sendCommand(
227
+ {
228
+ method: 'get',
229
+ uri: uri`/tenants/${tenantId}/users`,
230
+ },
231
+ {
232
+ collection: true,
233
+ ...opts,
234
+ },
235
+ )
236
+ }
237
+
238
+ public async getTenantUser(tenantId: string, user: Identity, opts?: ConsumeOptions): Promise<TenantUserInfo> {
239
+ return await this.sendCommand(
240
+ {
241
+ method: 'get',
242
+ uri: uri`/tenants/${tenantId}/users/${user}`,
243
+ },
244
+ opts,
245
+ )
246
+ }
247
+
248
+ public async getTenantSubscription(tenantId: string, opts?: ConsumeOptions): Promise<TenantSubscription> {
249
+ return await this.sendCommand(
250
+ {
251
+ method: 'get',
252
+ uri: uri`/tenants/${tenantId}/subscription`,
253
+ },
254
+ opts,
255
+ )
256
+ }
257
+
258
+ public async deleteTenantUser(tenantId: string, user: Identity, opts?: ConsumeOptions): Promise<void> {
259
+ return await this.sendCommand(
260
+ {
261
+ method: 'delete',
262
+ uri: uri`/tenants/${tenantId}/users/${user}`,
263
+ },
264
+ opts,
265
+ )
266
+ }
267
+
268
+ public async setTenantUser(
269
+ tenantId: string,
270
+ user: Identity,
271
+ roleId: TenantUserInfo['roleId'],
272
+ opts?: ConsumeOptions,
273
+ ): Promise<void> {
274
+ return await this.sendCommand(
275
+ {
276
+ method: 'set',
277
+ uri: uri`/tenants/${tenantId}/users`,
278
+ type: 'application/vnd.lime.collection+json',
279
+ resource: {
280
+ items: [
281
+ {
282
+ tenantId,
283
+ userIdentity: user,
284
+ roleId,
285
+ },
286
+ ],
287
+ itemType: 'application/vnd.iris.portal.tenant-user+json',
288
+ },
289
+ },
290
+ opts,
291
+ )
292
+ }
293
+
294
+ public async getApplicationUsers(bot: PossiblyNode, opts?: ConsumeOptions): Promise<Array<ApplicationUserInfo>> {
295
+ return await this.sendCommand(
296
+ {
297
+ method: 'get',
298
+ uri: uri`/applications/${Node.from(bot, 'msging.net')}/users`,
299
+ },
300
+ opts,
301
+ )
302
+ }
303
+
304
+ public async getApplicationUser(
305
+ bot: PossiblyNode,
306
+ user: Identity,
307
+ opts?: ConsumeOptions,
308
+ ): Promise<ApplicationUserInfo> {
309
+ return await this.sendCommand(
310
+ {
311
+ method: 'get',
312
+ uri: uri`/applications/${Node.from(bot, 'msging.net')}/users/${user}`,
313
+ },
314
+ opts,
315
+ )
316
+ }
317
+
318
+ public async deleteApplicationUser(bot: PossiblyNode, user: Identity, opts?: ConsumeOptions): Promise<void> {
319
+ return await this.sendCommand(
320
+ {
321
+ method: 'delete',
322
+ uri: uri`/applications/${Node.from(bot, 'msging.net')}/users/${user}`,
323
+ },
324
+ opts,
325
+ )
326
+ }
327
+
328
+ public async setApplicationUser(
329
+ bot: PossiblyNode,
330
+ user: Identity,
331
+ roleId: TenantUserInfo['roleId'],
332
+ tenantId: string,
333
+ opts?: ConsumeOptions,
334
+ ): Promise<void> {
335
+ await this.sendCommand(
336
+ {
337
+ method: 'set',
338
+ uri: uri`/applications/${Node.from(bot, 'msging.net')}/users?${{ tenantId }}`,
339
+ type: 'application/vnd.lime.identity',
340
+ resource: user,
341
+ },
342
+ opts,
343
+ )
344
+
345
+ await this.updateApplicationUserPermissions(bot, user, roleId, opts)
346
+ }
347
+
348
+ public async updateApplicationUserPermissions(
349
+ bot: PossiblyNode,
350
+ user: Identity,
351
+ roleId: TenantUserInfo['roleId'],
352
+ opts?: ConsumeOptions,
353
+ ): Promise<void> {
354
+ const actions: Array<'read' | 'write'> = ['read']
355
+ const otherPermissions: Array<{
356
+ permissionId: string
357
+ actions: typeof actions
358
+ userIdentity: typeof user
359
+ }> = []
360
+
361
+ if (roleId !== 'guest') {
362
+ actions.push('write')
363
+ }
364
+ if (roleId === 'admin') {
365
+ otherPermissions.push({
366
+ permissionId: 'team',
367
+ actions,
368
+ userIdentity: user,
369
+ })
370
+ }
371
+
372
+ return await this.sendCommand(
373
+ {
374
+ method: 'set',
375
+ uri: uri`/applications/${Node.from(bot, 'msging.net')}/permissions`,
376
+ type: 'application/vnd.lime.collection+json',
377
+ resource: {
378
+ itemType: 'application/vnd.iris.portal.user-permission+json',
379
+ items: [
380
+ ...otherPermissions,
381
+ { permissionId: 'payments', actions, userIdentity: user },
382
+ { permissionId: 'ai-providers', actions, userIdentity: user },
383
+ { permissionId: 'ai-model', actions, userIdentity: user },
384
+ { permissionId: 'ai-enhancement', actions, userIdentity: user },
385
+ { permissionId: 'ai-answers', actions, userIdentity: user },
386
+ { permissionId: 'channels', actions, userIdentity: user },
387
+ { permissionId: 'desk', actions, userIdentity: user },
388
+ { permissionId: 'users', actions, userIdentity: user },
389
+ { permissionId: 'scheduler', actions, userIdentity: user },
390
+ { permissionId: 'config-basicConfigurations', actions, userIdentity: user },
391
+ { permissionId: 'config-connectionInformation', actions, userIdentity: user },
392
+ { permissionId: 'resources', actions, userIdentity: user },
393
+ { permissionId: 'logMessages', actions, userIdentity: user },
394
+ { permissionId: 'builder', actions, userIdentity: user },
395
+ { permissionId: 'analysis', actions, userIdentity: user },
396
+ ],
397
+ },
398
+ },
399
+ opts,
400
+ )
401
+ }
402
+ }
@@ -0,0 +1,88 @@
1
+ import type { BlipClient } from '../client.ts'
2
+ import { BlipError } from '../sender/bliperror.ts'
3
+ import type { UnknownMessage } from '../types/index.ts'
4
+ import { uri } from '../utils/uri.ts'
5
+ import { type ConsumeOptions, Namespace, type SendCommandOptions } from './namespace.ts'
6
+
7
+ export class SchedulerNamespace extends Namespace {
8
+ constructor(blipClient: BlipClient, defaultOptions?: SendCommandOptions) {
9
+ super(blipClient, 'scheduler', defaultOptions)
10
+ }
11
+
12
+ public async scheduleMessage(
13
+ schedule: {
14
+ name?: string
15
+ message: UnknownMessage
16
+ when: string
17
+ },
18
+ opts?: ConsumeOptions,
19
+ ) {
20
+ return await this.sendCommand(
21
+ {
22
+ method: 'set',
23
+ uri: uri`/schedules`,
24
+ type: 'application/vnd.iris.schedule+json',
25
+ resource: schedule,
26
+ },
27
+ opts,
28
+ )
29
+ }
30
+
31
+ public async getSchedule(
32
+ id: string,
33
+ opts?: ConsumeOptions,
34
+ ): Promise<
35
+ | {
36
+ when: string
37
+ message: UnknownMessage
38
+ status: 'scheduled' | 'executed' | 'canceled'
39
+ }
40
+ | undefined
41
+ > {
42
+ try {
43
+ return await this.sendCommand(
44
+ {
45
+ method: 'get',
46
+ uri: uri`/schedules/${id}`,
47
+ },
48
+ opts,
49
+ )
50
+ } catch (err) {
51
+ if (err instanceof BlipError && err.code === 67) {
52
+ return undefined
53
+ }
54
+
55
+ throw err
56
+ }
57
+ }
58
+
59
+ public async getSchedules(opts?: ConsumeOptions): Promise<
60
+ Array<{
61
+ id: string
62
+ when: string
63
+ message: UnknownMessage
64
+ status: 'scheduled' | 'executed' | 'canceled'
65
+ }>
66
+ > {
67
+ return await this.sendCommand(
68
+ {
69
+ method: 'get',
70
+ uri: uri`/schedules`,
71
+ },
72
+ {
73
+ collection: true,
74
+ ...opts,
75
+ },
76
+ )
77
+ }
78
+
79
+ public async cancelSchedule(id: string, opts?: ConsumeOptions) {
80
+ return await this.sendCommand(
81
+ {
82
+ method: 'delete',
83
+ uri: uri`/schedules/${id}`,
84
+ },
85
+ opts,
86
+ )
87
+ }
88
+ }