forked from Watson-Personal-Assistant/kr-node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.js
45 lines (41 loc) · 1.26 KB
/
action.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
require('dotenv').config({ path: __dirname + '/.env' });
var request = require('request');
var KnowledgeObject = require('./sdk/object');
// Main function
function main(event) {
console.log('in action main');
var doorId = event[0].id;
console.log('got door id as ' + doorId)
return KnowledgeObject.retrieve(doorId).then((doorObj) => {
console.log('Door name', JSON.stringify(doorObj));
name = doorObj.attributes.name;
var post_data = {
'key': process.env.API_KEY,
'alert': `Alert! Your ${name} is open!`
};
var headers = {
'Content-type': 'application/json',
};
var options = {
url: 'http://wpa-chat-bot.mybluemix.net/notification',
method: 'POST',
headers: headers,
body: JSON.stringify(post_data)
};
return new Promise(function (res, rej) {
request(options, function (err, response, body) {
if (err || (response && response.statusCode !== 200)) {
console.log(`Error sending notification ${err} ${body}`);
if (response.statusCode) {
console.log(`Status code ${response.statusCode}`);
}
rej(body);
} else {
resData = { body: body };
res(resData);
}
});
});
});
}
exports.main = main;