-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintegration.js
More file actions
136 lines (119 loc) · 3.21 KB
/
Copy pathintegration.js
File metadata and controls
136 lines (119 loc) · 3.21 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict';
const request = require('request');
const async = require('async');
const BASE_URI = 'http://httpbin.org/get';
let Logger;
/**
* The startup method is called once when the integration is first loaded by the server. It can be used
* to do any initializations required (e.g., setting up persistent database connections)
*
* @param logger logger object
*/
function startup(logger) {
// cache a reference to the logger
Logger = logger;
}
/**
* @param entities array of entity objects
* @param options user options object
* @param cb callback function
*/
function doLookup(entities, options, cb) {
let lookupResults = [];
async.each(
entities,
function (entityObj, next) {
_lookupEntity(entityObj, options, function (err, result) {
if (err) {
next(err);
} else {
lookupResults.push(result);
next(null);
}
});
},
function (err) {
cb(err, lookupResults);
}
);
}
function _lookupEntity(entityObj, options, cb) {
const requestOptions = {
uri: BASE_URI,
method: 'GET',
qs: {
apiKey: options.apiKey
},
json: true
};
request(requestOptions, function (err, response, body) {
// check for a network error
if (err) {
cb({
detail: 'Error Making Network Request',
err: err
});
return;
}
// If we get a 404 then cache a miss
if (response.statusCode === 404) {
cb(null, {
entity: entityObj,
data: null // setting data to null indicates to the server that this entity lookup was a "miss"
});
return;
}
if (response.statusCode !== 200) {
cb({
detail: `Unexpected HTTP Status Code Received ${response.statusCode}`,
debug: body
});
return;
}
// The lookup results returned is an array of lookup objects with the following format
cb(null, {
// Required: This is the entity object passed into the integration doLookup method
entity: entityObj,
// Required: An object containing everything you want passed to the template
data: {
// Required: These are the tags that are displayed in your template
summary: [body.origin],
// Data that you want to pass back to the notification window details block
details: body
}
});
});
}
function onDetails(lookupObject, options, cb) {
// We're adding an artificial 3 second delay before we respond back to the onDetails request
setTimeout(function () {
// Add some new information to our details
lookupObject.data.details.newOnDetailsData = {
key: 'This is some onDetails data'
};
// Add a new tag
lookupObject.data.summary.push('onDetails Tag');
// Send back just the `data` property on the lookupObject
cb(null, lookupObject.data);
}, 3000);
}
let messageCounter = 0;
function onMessage(payload, options, cb) {
switch (payload.type) {
case 'COUNT_CLICKS':
cb(null, {
message: `I've been clicked ${++messageCounter} times`
});
break;
default:
cb({
detail: `Unexpected onMessage type received: ${payload.type}`
});
}
}
module.exports = {
doLookup,
startup,
onDetails,
onMessage
};