-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathagent-integrations.controller.ts
More file actions
496 lines (482 loc) · 21.5 KB
/
Copy pathagent-integrations.controller.ts
File metadata and controls
496 lines (482 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import {
Body,
ClassSerializerInterceptor,
Controller,
Delete,
Get,
HttpCode,
HttpStatus,
Param,
Patch,
Post,
Query,
UseInterceptors,
} from '@nestjs/common';
import { ApiExcludeController, ApiExcludeEndpoint, ApiOperation } from '@nestjs/swagger';
import { ProductFeature, RequirePermissions } from '@novu/application-generic';
import {
ApiRateLimitCategoryEnum,
DirectionEnum,
PermissionsEnum,
ProductFeatureKeyEnum,
UserSessionData,
} from '@novu/shared';
import { RequireAuthentication } from '../../../auth/framework/auth.decorator';
import { ExternalApiAccessible } from '../../../auth/framework/external-api.decorator';
import { ThrottlerCategory } from '../../../rate-limiting/guards';
import {
ApiCommonResponses,
ApiNoContentResponse,
ApiNotFoundResponse,
ApiResponse,
} from '../../../shared/framework/response.decorator';
import { KeylessAccessible } from '../../../shared/framework/swagger/keyless.security';
import { UserSession } from '../../../shared/framework/user.decorator';
import { SendAgentWelcomeMessageCommand } from '../../conversation-runtime/reply/send-agent-welcome-message/send-agent-welcome-message.command';
import { SendAgentWelcomeMessage } from '../../conversation-runtime/reply/send-agent-welcome-message/send-agent-welcome-message.usecase';
import { SendAgentTestEmailCommand } from '../../email/send-agent-test-email/send-agent-test-email.command';
import { SendAgentTestEmail } from '../../email/send-agent-test-email/send-agent-test-email.usecase';
import { UpdateAgentInboxSharedCommand } from '../../management/usecases/update-agent-inbox-shared/update-agent-inbox-shared.command';
import { UpdateAgentInboxShared } from '../../management/usecases/update-agent-inbox-shared/update-agent-inbox-shared.usecase';
import {
AddAgentIntegrationRequestDto,
AgentIntegrationResponseDto,
ListAgentIntegrationsQueryDto,
ListAgentIntegrationsResponseDto,
UpdateAgentInboxSharedRequestDto,
UpdateAgentIntegrationRequestDto,
} from '../../shared/dtos';
import { ConfigureTelegramWebhookResponseDto } from '../../shared/dtos/configure-telegram-webhook-response.dto';
import { ConfigureWhatsAppWebhookResponseDto } from '../../shared/dtos/configure-whatsapp-webhook-response.dto';
import { IssueSlackSetupLinkResponseDto } from '../../shared/dtos/issue-slack-setup-link-response.dto';
import { IssueTelegramMobileLinkRequestDto } from '../../shared/dtos/issue-telegram-mobile-link-request.dto';
import { IssueTelegramMobileLinkResponseDto } from '../../shared/dtos/issue-telegram-mobile-link-response.dto';
import { IssueTelegramSubscriberLinkRequestDto } from '../../shared/dtos/issue-telegram-subscriber-link-request.dto';
import { IssueTelegramSubscriberLinkResponseDto } from '../../shared/dtos/issue-telegram-subscriber-link-response.dto';
import { SendAgentTestEmailRequestDto } from '../../shared/dtos/send-agent-test-email-request.dto';
import { SendAgentWelcomeMessageRequestDto } from '../../shared/dtos/send-agent-welcome-message-request.dto';
import {
SendWhatsAppTestTemplateRequestDto,
SendWhatsAppTestTemplateResponseDto,
} from '../../shared/dtos/send-whatsapp-test-template.dto';
import { IssueSlackSetupLinkCommand } from '../slack-linking/issue-slack-setup-link/issue-slack-setup-link.command';
import { IssueSlackSetupLink } from '../slack-linking/issue-slack-setup-link/issue-slack-setup-link.usecase';
import { ConfigureTelegramAgentWebhookCommand } from '../telegram/configure-telegram-agent-webhook/configure-telegram-agent-webhook.command';
import { ConfigureTelegramAgentWebhook } from '../telegram/configure-telegram-agent-webhook/configure-telegram-agent-webhook.usecase';
import { IssueTelegramMobileLinkCommand } from '../telegram-linking/issue-telegram-mobile-link/issue-telegram-mobile-link.command';
import { IssueTelegramMobileLink } from '../telegram-linking/issue-telegram-mobile-link/issue-telegram-mobile-link.usecase';
import { IssueTelegramSubscriberLinkCommand } from '../telegram-linking/issue-telegram-subscriber-link/issue-telegram-subscriber-link.command';
import { IssueTelegramSubscriberLink } from '../telegram-linking/issue-telegram-subscriber-link/issue-telegram-subscriber-link.usecase';
import { ConfigureWhatsAppWebhookCommand } from '../whatsapp/configure-whatsapp-webhook/configure-whatsapp-webhook.command';
import { ConfigureWhatsAppWebhook } from '../whatsapp/configure-whatsapp-webhook/configure-whatsapp-webhook.usecase';
import { SendWhatsAppTestTemplateCommand } from '../whatsapp/send-whatsapp-test-template/send-whatsapp-test-template.command';
import { SendWhatsAppTestTemplate } from '../whatsapp/send-whatsapp-test-template/send-whatsapp-test-template.usecase';
import { AddAgentIntegrationCommand } from './add-agent-integration/add-agent-integration.command';
import { AddAgentIntegration } from './add-agent-integration/add-agent-integration.usecase';
import { ListAgentIntegrationsCommand } from './list-agent-integrations/list-agent-integrations.command';
import { ListAgentIntegrations } from './list-agent-integrations/list-agent-integrations.usecase';
import { RemoveAgentIntegrationCommand } from './remove-agent-integration/remove-agent-integration.command';
import { RemoveAgentIntegration } from './remove-agent-integration/remove-agent-integration.usecase';
import { UpdateAgentIntegrationCommand } from './update-agent-integration/update-agent-integration.command';
import { UpdateAgentIntegration } from './update-agent-integration/update-agent-integration.usecase';
@ThrottlerCategory(ApiRateLimitCategoryEnum.CONFIGURATION)
@ApiCommonResponses()
@Controller('/agents')
@UseInterceptors(ClassSerializerInterceptor)
@ApiExcludeController()
@RequireAuthentication()
export class AgentIntegrationsController {
constructor(
private readonly addAgentIntegrationUsecase: AddAgentIntegration,
private readonly listAgentIntegrationsUsecase: ListAgentIntegrations,
private readonly updateAgentIntegrationUsecase: UpdateAgentIntegration,
private readonly removeAgentIntegrationUsecase: RemoveAgentIntegration,
private readonly sendAgentTestEmailUsecase: SendAgentTestEmail,
private readonly sendAgentWelcomeMessageUsecase: SendAgentWelcomeMessage,
private readonly configureWhatsAppWebhookUsecase: ConfigureWhatsAppWebhook,
private readonly sendWhatsAppTestTemplateUsecase: SendWhatsAppTestTemplate,
private readonly configureTelegramAgentWebhookUsecase: ConfigureTelegramAgentWebhook,
private readonly issueTelegramMobileLinkUsecase: IssueTelegramMobileLink,
private readonly issueSlackSetupLinkUsecase: IssueSlackSetupLink,
private readonly issueTelegramSubscriberLinkUsecase: IssueTelegramSubscriberLink,
private readonly updateAgentInboxSharedUsecase: UpdateAgentInboxShared
) {}
@Post('/:identifier/integrations')
@ExternalApiAccessible()
@KeylessAccessible()
@ApiResponse(AgentIntegrationResponseDto, 201)
@ApiOperation({
summary: 'Link integration to agent',
description:
'Creates a link between an agent (by identifier) and an integration (by integration **identifier**, not the internal _id).',
})
@ApiNotFoundResponse({
description: 'The agent or integration was not found.',
})
@RequirePermissions(PermissionsEnum.AGENT_WRITE)
addAgentIntegration(
@UserSession() user: UserSessionData,
@Param('identifier') identifier: string,
@Body() body: AddAgentIntegrationRequestDto
): Promise<AgentIntegrationResponseDto> {
return this.addAgentIntegrationUsecase.execute(
AddAgentIntegrationCommand.create({
userId: user._id,
environmentId: user.environmentId,
organizationId: user.organizationId,
agentIdentifier: identifier,
integrationIdentifier: body.integrationIdentifier,
providerId: body.providerId,
})
);
}
@Get('/:identifier/integrations')
@ExternalApiAccessible()
@KeylessAccessible()
@ApiResponse(ListAgentIntegrationsResponseDto)
@ApiOperation({
summary: 'List agent integrations',
description:
'Lists integration links for an agent identified by its external identifier. Supports cursor pagination via **after**, **before**, **limit**, **orderBy**, and **orderDirection**.',
})
@ApiNotFoundResponse({
description: 'The agent was not found.',
})
@RequirePermissions(PermissionsEnum.AGENT_READ)
listAgentIntegrations(
@UserSession() user: UserSessionData,
@Param('identifier') identifier: string,
@Query() query: ListAgentIntegrationsQueryDto
): Promise<ListAgentIntegrationsResponseDto> {
return this.listAgentIntegrationsUsecase.execute(
ListAgentIntegrationsCommand.create({
user,
environmentId: user.environmentId,
organizationId: user.organizationId,
agentIdentifier: identifier,
limit: Number(query.limit || '10'),
after: query.after,
before: query.before,
orderDirection: query.orderDirection || DirectionEnum.DESC,
orderBy: query.orderBy || '_id',
includeCursor: query.includeCursor,
integrationIdentifier: query.integrationIdentifier,
})
);
}
@Patch('/:identifier/integrations/:agentIntegrationId')
@ApiResponse(AgentIntegrationResponseDto)
@ApiOperation({
summary: 'Update agent-integration link',
description: 'Updates which integration a link points to (by integration **identifier**, not the internal _id).',
})
@ApiNotFoundResponse({
description: 'The agent, integration, or link was not found.',
})
@RequirePermissions(PermissionsEnum.AGENT_WRITE)
updateAgentIntegration(
@UserSession() user: UserSessionData,
@Param('identifier') identifier: string,
@Param('agentIntegrationId') agentIntegrationId: string,
@Body() body: UpdateAgentIntegrationRequestDto
): Promise<AgentIntegrationResponseDto> {
return this.updateAgentIntegrationUsecase.execute(
UpdateAgentIntegrationCommand.create({
userId: user._id,
environmentId: user.environmentId,
organizationId: user.organizationId,
agentIdentifier: identifier,
agentIntegrationId,
integrationIdentifier: body.integrationIdentifier,
})
);
}
@Delete('/:identifier/integrations/:agentIntegrationId')
@HttpCode(HttpStatus.NO_CONTENT)
@ApiOperation({
summary: 'Remove agent-integration link',
description: 'Deletes a specific agent-integration link by its document id.',
})
@ApiNoContentResponse({
description: 'The link was removed.',
})
@ApiNotFoundResponse({
description: 'The agent or agent-integration link was not found.',
})
@RequirePermissions(PermissionsEnum.AGENT_WRITE)
removeAgentIntegration(
@UserSession() user: UserSessionData,
@Param('identifier') identifier: string,
@Param('agentIntegrationId') agentIntegrationId: string
): Promise<void> {
return this.removeAgentIntegrationUsecase.execute(
RemoveAgentIntegrationCommand.create({
userId: user._id,
environmentId: user.environmentId,
organizationId: user.organizationId,
agentIdentifier: identifier,
agentIntegrationId,
})
);
}
@Post('/:identifier/integrations/:integrationIdentifier/whatsapp/auto-configure')
@ApiExcludeEndpoint()
@HttpCode(HttpStatus.OK)
@ApiOperation({
summary: 'Auto-configure the WhatsApp webhook for an agent integration',
description:
'Calls Meta to register Novu as the webhook callback for the connected WhatsApp Business Account, subscribing to message events with the auto-generated verify token. Falls back to manual configuration when the access token lacks the management scope.',
})
@ApiNotFoundResponse({ description: 'The agent or integration was not found.' })
@RequirePermissions(PermissionsEnum.AGENT_WRITE)
configureAgentWhatsAppWebhook(
@UserSession() user: UserSessionData,
@Param('identifier') identifier: string,
@Param('integrationIdentifier') integrationIdentifier: string
): Promise<ConfigureWhatsAppWebhookResponseDto> {
return this.configureWhatsAppWebhookUsecase.execute(
ConfigureWhatsAppWebhookCommand.create({
userId: user._id,
environmentId: user.environmentId,
organizationId: user.organizationId,
agentIdentifier: identifier,
integrationIdentifier,
})
);
}
@Post('/:identifier/integrations/:integrationIdentifier/whatsapp/test-template')
@ApiExcludeEndpoint()
@HttpCode(HttpStatus.OK)
@ApiOperation({
summary: 'Send a WhatsApp test template from the agent integration',
description:
'Sends the `hello_world` template via the configured WhatsApp Business phone number to verify outbound delivery.',
})
@ApiNotFoundResponse({ description: 'The agent or integration was not found.' })
@RequirePermissions(PermissionsEnum.AGENT_WRITE)
sendAgentWhatsAppTestTemplate(
@UserSession() user: UserSessionData,
@Param('identifier') identifier: string,
@Param('integrationIdentifier') integrationIdentifier: string,
@Body() body: SendWhatsAppTestTemplateRequestDto
): Promise<SendWhatsAppTestTemplateResponseDto> {
return this.sendWhatsAppTestTemplateUsecase.execute(
SendWhatsAppTestTemplateCommand.create({
userId: user._id,
environmentId: user.environmentId,
organizationId: user.organizationId,
agentIdentifier: identifier,
integrationIdentifier,
subscriberId: body.subscriberId,
})
);
}
@Post('/:identifier/test-email')
@HttpCode(HttpStatus.OK)
@ProductFeature(ProductFeatureKeyEnum.AGENT_EMAIL_INTEGRATION)
@ApiOperation({
summary: 'Send a test email to the agent inbound address',
description:
'Sends a test email to the configured inbound address using the agent outbound provider (or the Novu demo integration as fallback). Used to verify the inbound email pipeline.',
})
@ApiNotFoundResponse({
description: 'The agent was not found.',
})
@RequirePermissions(PermissionsEnum.AGENT_WRITE)
sendAgentTestEmail(
@UserSession() user: UserSessionData,
@Param('identifier') identifier: string,
@Body() body: SendAgentTestEmailRequestDto
): Promise<{ success: boolean }> {
return this.sendAgentTestEmailUsecase.execute(
SendAgentTestEmailCommand.create({
userId: user._id,
environmentId: user.environmentId,
organizationId: user.organizationId,
agentIdentifier: identifier,
targetAddress: body.targetAddress,
})
);
}
@Patch('/:identifier/inbox/shared')
@ApiResponse(AgentIntegrationResponseDto)
@ApiOperation({
summary: 'Enable or disable the Novu shared inbox for an agent',
description:
'Disabling drops inbound mail addressed to this agent on the shared `agentconnect.sh` domain — custom-domain ' +
'routes continue to deliver. Refused when no custom-domain inbox is configured (would leave the agent with ' +
'zero inbound paths).',
})
@ApiNotFoundResponse({ description: 'The agent or its Novu Email integration was not found.' })
@ProductFeature(ProductFeatureKeyEnum.AGENT_EMAIL_INTEGRATION)
@RequirePermissions(PermissionsEnum.AGENT_WRITE)
updateAgentInboxShared(
@UserSession() user: UserSessionData,
@Param('identifier') identifier: string,
@Body() body: UpdateAgentInboxSharedRequestDto
): Promise<AgentIntegrationResponseDto> {
return this.updateAgentInboxSharedUsecase.execute(
UpdateAgentInboxSharedCommand.create({
userId: user._id,
environmentId: user.environmentId,
organizationId: user.organizationId,
agentIdentifier: identifier,
disabled: body.disabled,
})
);
}
@Post('/:identifier/welcome-message')
@ExternalApiAccessible()
@KeylessAccessible()
@HttpCode(HttpStatus.OK)
@ApiOperation({
summary: 'Send onboarding welcome message',
description:
'Sends a proactive DM to the agent installer after Slack OAuth, a welcome email after email ' +
'connection, or posts a bridge-connected follow-up message into an existing conversation thread ' +
'when conversationId is supplied.',
})
@ApiNotFoundResponse({ description: 'The agent or integration was not found.' })
@RequirePermissions(PermissionsEnum.AGENT_WRITE)
sendAgentWelcomeMessage(
@UserSession() user: UserSessionData,
@Param('identifier') identifier: string,
@Body() body: SendAgentWelcomeMessageRequestDto
): Promise<{ sent: boolean; conversationId?: string; claimToken?: string }> {
return this.sendAgentWelcomeMessageUsecase.execute(
SendAgentWelcomeMessageCommand.create({
userId: user._id,
environmentId: user.environmentId,
organizationId: user.organizationId,
agentIdentifier: identifier,
integrationIdentifier: body.integrationIdentifier,
conversationId: body.conversationId,
})
);
}
@Post('/:identifier/integrations/:integrationId/telegram/configure')
@ExternalApiAccessible()
@KeylessAccessible()
@HttpCode(HttpStatus.OK)
@ApiResponse(ConfigureTelegramWebhookResponseDto, 200)
@ApiOperation({
summary: 'Configure Telegram bot webhook',
description: `Registers the Novu agent webhook URL with Telegram for the specified integration,
generates a cryptographic secret token for webhook verification,
and persists it on the integration. Re-running rotates the secret.`,
})
@ApiNotFoundResponse({
description: 'The agent, integration, or agent-integration link was not found.',
})
@RequirePermissions(PermissionsEnum.AGENT_WRITE)
updateTelegramWebhook(
@UserSession() user: UserSessionData,
@Param('identifier') identifier: string,
@Param('integrationId') integrationId: string
): Promise<ConfigureTelegramWebhookResponseDto> {
return this.configureTelegramAgentWebhookUsecase.execute(
ConfigureTelegramAgentWebhookCommand.create({
userId: user._id,
environmentId: user.environmentId,
organizationId: user.organizationId,
agentIdentifier: identifier,
integrationId,
})
);
}
@Post('/:identifier/integrations/:integrationId/telegram/mobile-link')
@ExternalApiAccessible()
@KeylessAccessible()
@HttpCode(HttpStatus.OK)
@ApiResponse(IssueTelegramMobileLinkResponseDto, 200)
@ApiOperation({
summary: 'Issue a short-lived Telegram mobile setup link',
description:
'Issues a signed, single-use link (TTL = 5 minutes) that can be opened on a mobile device to finish ' +
'configuring a Telegram bot without re-authenticating. Telegram-only.',
})
@ApiNotFoundResponse({
description: 'The agent, integration, or agent-integration link was not found.',
})
@RequirePermissions(PermissionsEnum.AGENT_WRITE)
createTelegramMobileLink(
@UserSession() user: UserSessionData,
@Param('identifier') identifier: string,
@Param('integrationId') integrationId: string,
@Body() body?: IssueTelegramMobileLinkRequestDto
): Promise<IssueTelegramMobileLinkResponseDto> {
return this.issueTelegramMobileLinkUsecase.execute(
IssueTelegramMobileLinkCommand.create({
userId: user._id,
environmentId: user.environmentId,
organizationId: user.organizationId,
agentIdentifier: identifier,
integrationId,
subscriberId: body?.subscriberId,
})
);
}
@Post('/:identifier/integrations/:integrationId/slack/setup-link')
@ExternalApiAccessible()
@KeylessAccessible()
@HttpCode(HttpStatus.OK)
@ApiResponse(IssueSlackSetupLinkResponseDto, 200)
@ApiOperation({
summary: 'Issue a short-lived Slack setup link',
description:
'Issues a signed, single-use link (TTL = 5 minutes) that can be opened to paste a Slack App ' +
'Configuration Token without re-authenticating. Slack-only.',
})
@ApiNotFoundResponse({
description: 'The agent, integration, or agent-integration link was not found.',
})
@RequirePermissions(PermissionsEnum.AGENT_WRITE)
createSlackSetupLink(
@UserSession() user: UserSessionData,
@Param('identifier') identifier: string,
@Param('integrationId') integrationId: string
): Promise<IssueSlackSetupLinkResponseDto> {
return this.issueSlackSetupLinkUsecase.execute(
IssueSlackSetupLinkCommand.create({
userId: user._id,
environmentId: user.environmentId,
organizationId: user.organizationId,
agentIdentifier: identifier,
integrationId,
})
);
}
@Post('/:identifier/integrations/:integrationId/telegram/subscriber-link')
@ExternalApiAccessible()
@KeylessAccessible()
@HttpCode(HttpStatus.OK)
@ApiResponse(IssueTelegramSubscriberLinkResponseDto, 200)
@ApiOperation({
summary: 'Issue a Telegram subscriber-link deep link',
description:
'Issues a short-lived opaque start code and returns a Telegram `t.me/<bot>?start=<code>` deep link. When ' +
'opened, Telegram sends `/start <code>` to the bot; the agent webhook consumes the code server-side and ' +
'creates a `telegram_chat` channel endpoint so notifications can reach that subscriber via Telegram.',
})
@ApiNotFoundResponse({
description: 'The agent, integration, agent-integration link, or subscriber was not found.',
})
@RequirePermissions(PermissionsEnum.AGENT_WRITE)
createTelegramSubscriberLink(
@UserSession() user: UserSessionData,
@Param('identifier') identifier: string,
@Param('integrationId') integrationId: string,
@Body() body: IssueTelegramSubscriberLinkRequestDto
): Promise<IssueTelegramSubscriberLinkResponseDto> {
return this.issueTelegramSubscriberLinkUsecase.execute(
IssueTelegramSubscriberLinkCommand.create({
userId: user._id,
environmentId: user.environmentId,
organizationId: user.organizationId,
agentIdentifier: identifier,
integrationId,
subscriberId: body.subscriberId,
})
);
}
}