-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeexpressbotlab.js
70 lines (57 loc) · 2.76 KB
/
nodeexpressbotlab.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
const express = require("express");
const apirequest = require('./Controllers/apirequest');
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
const debug = require("debug")("samples");
const started = Date.now();
app.route("/")
// healthcheck
.get(function (req, res) {
res.json({
message: "Congrats, your Cisco Spark bot is up and running",
since: new Date(started).toISOString(),
code: "express-all-in-one.js",
tip: "Register your bot as a WebHook to start receiving events: https://developer.ciscospark.com/endpoint-webhooks-post.html"
});
})
// webhook endpoint
.post(function (req, res) {
// analyse incoming payload, should conform to Spark webhook trigger specifications
debug("DEBUG: webhook invoked");
if (!req.body ) { //|| !Utils.checkWebhookEvent(req.body)
console.log("WARNING: Unexpected payload POSTed, aborting...");
res.status(400).json({message: "Bad payload for Webhook",
details: "either the bot is misconfigured or Cisco Spark is running a new API version"});
return;
}
// event is ready to be processed, let's send a response to Spark without waiting any longer
res.status(200).json({message: "message is being processed by webhook"});
// process incoming resource/event, see https://developer.ciscospark.com/webhooks-explained.html
extractWebhookReqBody(req.body);
});
// Starts the Bot service
//
// [WORKAROUND] in some container situation (ie, Cisco Shipped), we need to use an OVERRIDE_PORT to force our bot to start and listen to the port defined in the Dockerfile (ie, EXPOSE),
// and not the PORT dynamically assigned by the host or scheduler.
const port = process.env.OVERRIDE_PORT || process.env.PORT || 8080;
app.listen(port, function () {
console.log("Cisco Spark Bot started at http://localhost:" + port + "/");
console.log(" GET / for health checks");
console.log(" POST / to procress new Webhook events");
});
// Invoked when the Spark webhook is triggered
function extractWebhookReqBody(trigger) {
if (trigger.data.roomType === "group") {
{apirequest.GroupRoomResponse(trigger)};
} else if (trigger.data.roomType === "direct") {
{apirequest.DirectRoomResponse(trigger)};
} else if (trigger.resource === 'memberships' && trigger.event === 'created') {
if (trigger.data.personEmail === '[email protected]') {
{apirequest.botMembershipAdd(trigger)};
}
} else {
console.log(trigger);
}
}