-
Notifications
You must be signed in to change notification settings - Fork 0
/
callerInfo.js
52 lines (47 loc) · 1.37 KB
/
callerInfo.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
exports.handler = function(context, event, callback) {
let caller = event.Caller;
caller = caller.substring(1);
postUrl = context.BASE_URI + '/callers';
findUrl = context.BASE_URI + '/callers?filters[PhoneNumber][$contains]=' + caller;
auth = 'Bearer ' + context.EXT_API_TOK1 + context.EXT_API_TOK2;
var got = require('got');
got(findUrl, {
responseType: 'json',
headers: {
"Authorization": auth
}
})
.then(function(response) {
const resp = response.body;
if (typeof resp.data[0] != "undefined") {
var found = resp.data[0];
console.log('number found');
callback(null, found);
} else {
got(postUrl, {
method: "POST",
responseType: 'json',
headers: {
"Content-Type": "application/json",
"Authorization": auth
},
json: {
"data": {
"PhoneNumber": event.Caller,
"smsOptIn": false
}
},
})
.then(function(response) {
console.log(response.body.data)
callback(null, response.body.data);
})
.catch(function(error) {
callback(error)
});
}
})
.catch(function(error) {
callback(error)
});
};