Skip to content

Commit f049a15

Browse files
author
Gavin Barron
committed
Restoring calendar handler
1 parent 655c716 commit f049a15

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

CalendarWebHookHandler/function.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"bindings": [
3+
{
4+
"type": "httpTrigger",
5+
"direction": "in",
6+
"name": "req",
7+
"authLevel": "anonymous"
8+
},
9+
{
10+
"type": "http",
11+
"direction": "out",
12+
"name": "res"
13+
}
14+
],
15+
"disabled": false
16+
}

CalendarWebHookHandler/index.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const azureStorage = require('azure-storage');
2+
const cfg = require('../common/config')
3+
4+
module.exports = function (context, data) {
5+
context.log('Calendar Webhook Handler was triggered!');
6+
7+
// Handle subscription request
8+
if (data.query.validationToken) {
9+
context.log('registering new hook');
10+
context.res = {
11+
status: 200,
12+
body: data.query.validationToken
13+
};
14+
context.done();
15+
return;
16+
}
17+
18+
const storageConnectionString = process.env.AzureWebJobsStorage;
19+
const queueSvc = azureStorage.createQueueService(storageConnectionString);
20+
// Write incoming notifications onto a queue.
21+
queueSvc.createQueueIfNotExists('hook-recieved', err => {
22+
if (err) {
23+
// this should be a log for the dev, not a message to the user
24+
context.log('There was an error creating the hook-recieved queue: '+ err);
25+
context.done(err);
26+
return;
27+
}
28+
// enqueue a message to process the webhook request.
29+
const request = data.body;
30+
context.log(request);
31+
const notificationCount = request.value.length;
32+
let processed = 0;
33+
// web hook requests can contain multiple notifications
34+
for (let hook of request.value) {
35+
let msg = JSON.stringify(hook);
36+
context.log(msg);
37+
var queueMessageBuffer = new Buffer(msg).toString('base64');
38+
// only take action when the clientState is vaild.
39+
// return the 202 and do notthing if the clientState is vaild.
40+
if (hook.clientState !== cfg.CLIENT_STATE) {
41+
// add a log entry so that this can be investigated
42+
context.log('Unknown sender! Investigate the source of this message')
43+
context.log(data);
44+
context.res = { status: 202, body: 'All notifications processed' };
45+
context.done();
46+
return;
47+
}
48+
queueSvc.createMessage('hook-recieved', queueMessageBuffer, (err, result) => {
49+
processed++;
50+
// fail on any message not getting queued properly
51+
if (err) {
52+
context.log('error sending webhook messge on hook-recieved queue');
53+
context.done(err);
54+
return;
55+
}
56+
context.log('webhook messge on put onto hook-recieved queue');
57+
if (processed == notificationCount) {
58+
context.res = { status: 202, body: 'All notifications processed' };
59+
context.done(null, result);
60+
}
61+
});
62+
}
63+
});
64+
}

EventWebHookHandler/function.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "httpTrigger",
55
"direction": "in",
66
"name": "req",
7-
"authLevel": "anonymous"
7+
"authLevel": "function"
88
},
99
{
1010
"type": "http",

0 commit comments

Comments
 (0)