-
Notifications
You must be signed in to change notification settings - Fork 38
/
events_webhooks.js
155 lines (118 loc) · 3.44 KB
/
events_webhooks.js
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
/*
* node-crisp-api
*
* Copyright 2022, Crisp IM SAS
* Author: Valerian Saliou <valerian@valeriansaliou.name>
*/
"use strict";
var http = require("http");
var url = require("url");
const TOKEN = {
identifier : "<YOUR_API_TOKEN_IDENTIFIER>",
key : "<YOUR_API_TOKEN_KEY>",
signSecret : "<YOUR_HOOKS_SIGNATURE_SECRET>"
};
const HOOKS_SERVER = {
port : 8080,
path : "/"
};
var Crisp = require("../");
var CrispClient = new Crisp();
console.info("Authenticating...");
CrispClient.authenticateTier("plugin", TOKEN.identifier, TOKEN.key);
CrispClient.setRtmMode(Crisp.RTM_MODES.WebHooks);
console.info("Listening for events...");
CrispClient.on("message:send", function(message) {
console.info("[Web Hooks] Got 'message:send' event:", message);
})
.then(function() {
console.error("[Web Hooks] Requested to listen to sent messages");
})
.catch(function(error) {
console.error("[Web Hooks] Failed listening to sent messages:", error);
});
CrispClient.on("message:received", function(message) {
console.info("[Web Hooks] Got 'message:received' event:", message);
})
.then(function() {
console.error("[Web Hooks] Requested to listen to received messages");
})
.catch(function(error) {
console.error("[Web Hooks] Failed listening to received messages:", error);
});
console.info(
"Starting Web Hooks HTTP endpoint at " +
("http://localhost:" + HOOKS_SERVER.port + "/")
);
var _processWebhooksEvent = function(request, body) {
var secret = TOKEN.signSecret,
timestamp = request.headers["x-crisp-request-timestamp"],
signature = request.headers["x-crisp-signature"];
// Verify Web Hook payload
if (CrispClient.verifyHook(secret, body, timestamp, signature) !== true) {
console.warn(
"[Web Hooks] Web Hooks request could not be verified with signature: " +
(signature || "(?)")
);
return 403;
}
// Receive Web Hook payload
var error = CrispClient.receiveHook(body);
if (error !== null) {
console.error(
"[Web Hooks] Web Hooks payload processing error: " + error.toString()
);
return 400;
}
console.info(
"[Web Hooks] Web Hooks payload processed:\n \\->", body
);
return 200;
};
var _handleIncomingRequest = function(request, response, body) {
// Handle request?
var responseStatus = 404,
requestURL = url.parse(request.url);
if (requestURL && requestURL.pathname === HOOKS_SERVER.path) {
switch (request.method) {
case "POST": {
responseStatus = _processWebhooksEvent(request, body);
break;
}
default: {
responseStatus = 405;
}
}
}
// Send response
response.writeHead(responseStatus, {
"Content-Type" : "text/plain"
});
response.end(http.STATUS_CODES[responseStatus]);
};
http
.createServer(function(request, response) {
console.debug(
"[Web Hooks] Received HTTP request: " + request.method + " " + request.url
);
var bodyBuffer = "";
request
.on("data", function(chunk) {
bodyBuffer += chunk;
})
.on("end", function() {
// Attempt to parse body to JSON
var body = null;
try {
if (bodyBuffer) {
body = JSON.parse(bodyBuffer);
}
} catch (_) {
// Ignore errors
}
// Pass request to handler
_handleIncomingRequest(request, response, body);
})
.setEncoding("utf8");
})
.listen(HOOKS_SERVER.port);