|
1 | 1 | import { Logger } from '@rocket.chat/logger'; |
2 | 2 | import type { ExtendedFetchOptions } from '@rocket.chat/server-fetch'; |
3 | 3 | import { serverFetch as fetch } from '@rocket.chat/server-fetch'; |
| 4 | +import { ajv, validateUnauthorizedErrorResponse } from '@rocket.chat/rest-typings'; |
4 | 5 |
|
5 | 6 | import { API } from '../../../../api/server'; |
| 7 | +import type { ExtractRoutesFromAPI } from '../../../../api/server/ApiClass'; |
6 | 8 | import { settings } from '../../../../settings/server'; |
7 | 9 |
|
8 | 10 | const logger = new Logger('WebhookTest'); |
9 | 11 |
|
10 | | -API.v1.addRoute( |
| 12 | +const livechatWebhookEndpoints = API.v1.post( |
11 | 13 | 'livechat/webhook.test', |
12 | | - { authRequired: true, permissionsRequired: ['view-livechat-webhooks'] }, |
13 | 14 | { |
14 | | - async post() { |
15 | | - const sampleData = { |
16 | | - type: 'LivechatSession', |
17 | | - _id: 'fasd6f5a4sd6f8a4sdf', |
18 | | - label: 'title', |
19 | | - topic: 'asiodojf', |
20 | | - createdAt: new Date(), |
21 | | - lastMessageAt: new Date(), |
22 | | - tags: ['tag1', 'tag2', 'tag3'], |
| 15 | + authRequired: true, |
| 16 | + permissionsRequired: ['view-livechat-webhooks'], |
| 17 | + response: { |
| 18 | + 401: validateUnauthorizedErrorResponse, |
| 19 | + 200: ajv.compile<void>({ |
| 20 | + type: 'object', |
| 21 | + properties: { |
| 22 | + success: { type: 'boolean', enum: [true] }, |
| 23 | + }, |
| 24 | + required: ['success'], |
| 25 | + }), |
| 26 | + }, |
| 27 | + }, |
| 28 | + async function action() { |
| 29 | + const sampleData = { |
| 30 | + type: 'LivechatSession', |
| 31 | + _id: 'fasd6f5a4sd6f8a4sdf', |
| 32 | + label: 'title', |
| 33 | + topic: 'asiodojf', |
| 34 | + createdAt: new Date(), |
| 35 | + lastMessageAt: new Date(), |
| 36 | + tags: ['tag1', 'tag2', 'tag3'], |
| 37 | + customFields: { |
| 38 | + productId: '123456', |
| 39 | + }, |
| 40 | + visitor: { |
| 41 | + _id: '', |
| 42 | + name: 'visitor name', |
| 43 | + username: 'visitor-username', |
| 44 | + department: 'department', |
| 45 | + email: 'email@address.com', |
| 46 | + phone: '192873192873', |
| 47 | + ip: '123.456.7.89', |
| 48 | + browser: 'Chrome', |
| 49 | + os: 'Linux', |
23 | 50 | customFields: { |
24 | | - productId: '123456', |
| 51 | + customerId: '123456', |
25 | 52 | }, |
26 | | - visitor: { |
27 | | - _id: '', |
28 | | - name: 'visitor name', |
| 53 | + }, |
| 54 | + agent: { |
| 55 | + _id: 'asdf89as6df8', |
| 56 | + username: 'agent.username', |
| 57 | + name: 'Agent Name', |
| 58 | + email: 'agent@email.com', |
| 59 | + }, |
| 60 | + messages: [ |
| 61 | + { |
29 | 62 | username: 'visitor-username', |
30 | | - department: 'department', |
31 | | - email: 'email@address.com', |
32 | | - phone: '192873192873', |
33 | | - ip: '123.456.7.89', |
34 | | - browser: 'Chrome', |
35 | | - os: 'Linux', |
36 | | - customFields: { |
37 | | - customerId: '123456', |
38 | | - }, |
| 63 | + msg: 'message content', |
| 64 | + ts: new Date(), |
39 | 65 | }, |
40 | | - agent: { |
41 | | - _id: 'asdf89as6df8', |
| 66 | + { |
42 | 67 | username: 'agent.username', |
43 | | - name: 'Agent Name', |
44 | | - email: 'agent@email.com', |
| 68 | + agentId: 'asdf89as6df8', |
| 69 | + msg: 'message content from agent', |
| 70 | + ts: new Date(), |
45 | 71 | }, |
46 | | - messages: [ |
47 | | - { |
48 | | - username: 'visitor-username', |
49 | | - msg: 'message content', |
50 | | - ts: new Date(), |
51 | | - }, |
52 | | - { |
53 | | - username: 'agent.username', |
54 | | - agentId: 'asdf89as6df8', |
55 | | - msg: 'message content from agent', |
56 | | - ts: new Date(), |
57 | | - }, |
58 | | - ], |
59 | | - }; |
60 | | - const options = { |
61 | | - method: 'POST', |
62 | | - headers: { |
63 | | - 'X-RocketChat-Livechat-Token': settings.get<string>('Livechat_secret_token'), |
64 | | - 'Accept': 'application/json', |
65 | | - }, |
66 | | - body: sampleData, |
67 | | - // SECURITY: Webhooks can only be configured by users with enough privileges. It's ok to disable this check here. |
68 | | - ignoreSsrfValidation: true, |
69 | | - size: 10 * 1024 * 1024, |
70 | | - } as ExtendedFetchOptions; |
71 | | - |
72 | | - const webhookUrl = settings.get<string>('Livechat_webhookUrl'); |
| 72 | + ], |
| 73 | + }; |
| 74 | + const options = { |
| 75 | + method: 'POST', |
| 76 | + headers: { |
| 77 | + 'X-RocketChat-Livechat-Token': settings.get<string>('Livechat_secret_token'), |
| 78 | + 'Accept': 'application/json', |
| 79 | + }, |
| 80 | + body: sampleData, |
| 81 | + // SECURITY: Webhooks can only be configured by users with enough privileges. It's ok to disable this check here. |
| 82 | + ignoreSsrfValidation: true, |
| 83 | + size: 10 * 1024 * 1024, |
| 84 | + } as ExtendedFetchOptions; |
73 | 85 |
|
74 | | - if (!webhookUrl) { |
75 | | - return API.v1.failure('Webhook_URL_not_set'); |
76 | | - } |
| 86 | + const webhookUrl = settings.get<string>('Livechat_webhookUrl'); |
77 | 87 |
|
78 | | - try { |
79 | | - logger.debug({ msg: 'Testing webhook', webhookUrl }); |
80 | | - const request = await fetch(webhookUrl, options); |
81 | | - const response = await request.text(); |
| 88 | + if (!webhookUrl) { |
| 89 | + return API.v1.failure('Webhook_URL_not_set'); |
| 90 | + } |
82 | 91 |
|
83 | | - logger.debug({ msg: 'Webhook response', response }); |
84 | | - if (request.status === 200) { |
85 | | - return API.v1.success(); |
86 | | - } |
| 92 | + try { |
| 93 | + logger.debug({ msg: 'Testing webhook', webhookUrl }); |
| 94 | + const request = await fetch(webhookUrl, options); |
| 95 | + const response = await request.text(); |
87 | 96 |
|
88 | | - throw new Error('Invalid status code'); |
89 | | - } catch (error) { |
90 | | - logger.error({ msg: 'Error testing webhook', err: error }); |
91 | | - throw new Error('error-invalid-webhook-response'); |
| 97 | + logger.debug({ msg: 'Webhook response', response }); |
| 98 | + if (request.status === 200) { |
| 99 | + return API.v1.success(); |
92 | 100 | } |
93 | | - }, |
| 101 | + |
| 102 | + throw new Error('Invalid status code'); |
| 103 | + } catch (error) { |
| 104 | + logger.error({ msg: 'Error testing webhook', err: error }); |
| 105 | + throw new Error('error-invalid-webhook-response'); |
| 106 | + } |
94 | 107 | }, |
95 | 108 | ); |
| 109 | + |
| 110 | +type LivechatWebhookEndpoints = ExtractRoutesFromAPI<typeof livechatWebhookEndpoints>; |
| 111 | + |
| 112 | +declare module '@rocket.chat/rest-typings' { |
| 113 | + // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-object-type, @typescript-eslint/no-empty-interface |
| 114 | + interface Endpoints extends LivechatWebhookEndpoints {} |
| 115 | +} |
0 commit comments