-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
141 lines (127 loc) · 3.11 KB
/
handler.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
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
137
138
139
140
141
'use strict';
const querystring = require('querystring');
const base64 = require('base-64');
const fetch = require('node-fetch');
module.exports.sms_receiver = async (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
const params = querystring.parse(event.body);
const data = {
direction: params['direction'],
id: params['id'],
from: decodeURIComponent (params['from']),
to: decodeURIComponent (params['to']),
created: decodeURIComponent (params['created']),
message: decodeURIComponent (params['message'])
};
const fmConnection = {
database: process.env.DATABASE,
layout: process.env.LAYOUT,
account: process.env.ACCOUNT,
password: process.env.PASSWORD
};
let aws_result = '';
if (data.direction == 'incoming') {
const token = await login(fmConnection);
const result = await createRecord (fmConnection,data,token);
const out = await logout (fmConnection,token);
aws_result = 'OK';
}
else {
aws_result = 'Fel';
}
const response = {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST,HEAD,GET',
'Access-Control-Allow-Headers': 'Content-Type'
},
body: JSON.stringify({
result: aws_result,
})
};
callback(null, response);
};
async function login (fmConnection) {
let encryptedCredentials = base64.encode(fmConnection.account + ":" + fmConnection.password);
const url = fmConnection.database + "/sessions";
const headers =
{
'Authorization': 'Basic ' + encryptedCredentials,
'Content-Type': 'application/json',
};
const data = {};
const payload = JSON.stringify(data);
const options =
{
method: 'POST',
headers: headers,
body: payload
};
let fmDataToken = '';
try {
const response = await fetch(url,options);
const json = await response.json();
fmDataToken = json.response.token;
}
catch (error) {
console.log(error);
}
return fmDataToken;
}
async function createRecord (fmConnection,recordData,fmDataToken) {
const url = fmConnection.database + "/layouts/" + fmConnection.layout + "/records";
const headers =
{
"Content-Type": "application/json",
"Authorization": "Bearer " + fmDataToken
};
const data =
{
"fieldData": recordData
};
const payload = JSON.stringify (data);
const options =
{
method: 'POST',
headers: headers,
body: payload
}
let result = {};
try {
const response = await fetch(url,options);
result = await response.json();
}
catch (error) {
console.log(error);
}
console.log('createrecord param');
console.log(recordData);
console.log('createrecord');
console.log(result);
return result;
}
async function logout (fmConnection,fmDataToken) {
const url = fmConnection.database + "/sessions/" + fmDataToken;
const headers =
{
"Content-Type": "application/json"
};
const options =
{
method: 'DELETE',
headers: headers
};
let result = {};
try {
const response = await fetch(url,options);
result = await response.json();
}
catch (error) {
console.log(error);
}
console.log('logout');
console.log(result);
return result;
}