-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathwebhooks.ts
More file actions
115 lines (105 loc) · 3.04 KB
/
webhooks.ts
File metadata and controls
115 lines (105 loc) · 3.04 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
import { Logger } from '@rocket.chat/logger';
import { ajv, validateUnauthorizedErrorResponse } from '@rocket.chat/rest-typings';
import type { ExtendedFetchOptions } from '@rocket.chat/server-fetch';
import { serverFetch as fetch } from '@rocket.chat/server-fetch';
import { API } from '../../../../api/server';
import type { ExtractRoutesFromAPI } from '../../../../api/server/ApiClass';
import { settings } from '../../../../settings/server';
const logger = new Logger('WebhookTest');
const livechatWebhookEndpoints = API.v1.post(
'livechat/webhook.test',
{
authRequired: true,
permissionsRequired: ['view-livechat-webhooks'],
response: {
401: validateUnauthorizedErrorResponse,
200: ajv.compile<{ success: boolean }>({
type: 'object',
properties: {
success: { type: 'boolean', enum: [true] },
},
required: ['success'],
additionalProperties: false,
}),
},
},
async function action() {
const sampleData = {
type: 'LivechatSession',
_id: 'fasd6f5a4sd6f8a4sdf',
label: 'title',
topic: 'asiodojf',
createdAt: new Date(),
lastMessageAt: new Date(),
tags: ['tag1', 'tag2', 'tag3'],
customFields: {
productId: '123456',
},
visitor: {
_id: '',
name: 'visitor name',
username: 'visitor-username',
department: 'department',
phone: '192873192873',
ip: '123.456.7.89',
browser: 'Chrome',
os: 'Linux',
customFields: {
customerId: '123456',
},
},
agent: {
_id: 'asdf89as6df8',
username: 'agent.username',
name: 'Agent Name',
},
messages: [
{
username: 'visitor-username',
msg: 'message content',
ts: new Date(),
},
{
username: 'agent.username',
agentId: 'asdf89as6df8',
msg: 'message content from agent',
ts: new Date(),
},
],
};
const options = {
method: 'POST',
headers: {
'X-RocketChat-Livechat-Token': settings.get<string>('Livechat_secret_token'),
'Accept': 'application/json',
},
body: sampleData,
ignoreSsrfValidation: true,
size: 10 * 1024 * 1024,
} as ExtendedFetchOptions;
const webhookUrl = settings.get<string>('Livechat_webhookUrl');
if (!webhookUrl) {
return API.v1.failure('Webhook_URL_not_set');
}
try {
logger.debug({ msg: 'Testing webhook', host: new URL(webhookUrl).host });
const request = await fetch(webhookUrl, options);
await request.text();
logger.debug({ msg: 'Webhook response', status: request.status });
if (request.status === 200) {
return API.v1.success();
}
throw new Error('Invalid status code');
} catch (error) {
logger.error({ msg: 'Error testing webhook', err: error });
throw new Error('error-invalid-webhook-response');
}
},
);
type LivechatWebhookEndpoints = ExtractRoutesFromAPI<typeof livechatWebhookEndpoints>;
declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends LivechatWebhookEndpoints {}
}