This repository has been archived by the owner on Oct 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
/
TwilioProcessing.js
129 lines (110 loc) · 4.7 KB
/
TwilioProcessing.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
// This function processes incoming data from Twilio, formats it and makes an HTTPS POST request to the the Chat Service API endpoint
/* == Imports == */
var querystring = require('querystring');
var AWS = require('aws-sdk');
var path = require('path');
/* == Globals == */
var API = {
region: 'INSERT YOUR REGION CODE HERE', //i.e 'us-east-1'
endpoint: 'INSERT YOUR API GATEWAY URL HERE INCLUDING THE HTTPS://' // ie: 'https://xxxxxxx.execute-api.us-east-1.amazonaws.com'
};
var table = "YOUR DYNAMODB USERS TABLE"; //INSERT THE NAME OF YOUR DYNAMODB USERS TABLE
var index = "YOUR phoneindex DYNAMODB INDEX NAME"; //INSERT THE NAME OF YOUR phoneindex from DynamoDB.
var endpoint = new AWS.Endpoint(API.endpoint);
var creds = new AWS.EnvironmentCredentials('AWS');
var docClient = new AWS.DynamoDB.DocumentClient({
region: API.region
});
exports.handler = function(event, context) {
var params = querystring.parse(event.postBody);
var from = params.From;
var timestamp = "" + new Date().getTime();
var numMedia = params.NumMedia;
var message;
var mediaURL;
/* == DynamoDB Params == */
var ddbParams = {
TableName: table,
IndexName: index,
KeyConditionExpression: "phone = :from",
ProjectionExpression: "userid, phone",
ExpressionAttributeValues: {
":from": from
}
};
// If Message sent and Image sent, concat image url to message.
if (params.Body !== null || params.Body !== 'null') {
message = params.Body;
// If picture was sent along, append to message string.
if (numMedia > 0) {
mediaURL = params.MediaUrl0;
message = message + " [IMAGE SENT]: " + mediaURL;
}
}
// If message was not sent but image URL was sent, then set message to image URL
else if ((params.Body == null || params.Body == 'null') && numMedia > 0) {
mediaURL = params.MediaUrl0;
message = "Image sent: " + mediaURL;
}
// If no message or media sent, throw error.
else {
return context.fail("There was an error. Try again.");
}
// Now that we have the Twilio data formatted correctly, we make a request to our Chat Service
var post_data = JSON.stringify({
"message": message,
"name": from,
"channel": "default",
"timestamp": timestamp
});
/* == Query Users table to confirm that incoming phone number exists and is tied to an authorized survivor == */
docClient.query(ddbParams, function(err, data) {
if (err) {
console.log('Error. Unable to insert text message. DynamoDB querying failed. RESULT: ' + JSON.stringify(err));
context.fail(new Error('DynamoDB Error: ' + err));
}
else {
// FOR TESTING
//console.log('Query response is: ' + JSON.stringify(data));
//console.log('count is: ' + data.Count);
// If no records returned then fail the message with not authorized message.
if (data.Count < 1) {
context.done('Your phone number is not authorized to send texts to survivors. There were no phone numbers matching yours. Please sign up first.');
console.log('Text from unauthorized number. No records matching that number');
} else {
// Parse result and get phone.
data.Items.forEach(function(item) {
console.log('Incoming message from: ' + item.phone);
});
postToChatService(post_data, context);
}
}
});
}
function postToChatService(post_data, context) {
var req = new AWS.HttpRequest(endpoint);
req.method = 'POST';
req.path = '/ZombieWorkshopStage/zombie/message';
req.port = '443';
req.region = API.region;
req.headers['presigned-expires'] = false;
req.headers['Host'] = endpoint.host;
req.body = post_data;
console.log('host is ' + endpoint.host);
var signer = new AWS.Signers.V4(req,'execute-api'); // es: service code
signer.addAuthorization(creds, new Date());
var send = new AWS.NodeHttpClient();
send.handleRequest(req, null, function(httpResp) {
var respBody = '';
httpResp.on('data', function (chunk) {
respBody += chunk;
});
httpResp.on('end', function (chunk) {
console.log('Response: ' + respBody);
context.succeed('Text received in chat room. Survivors have been notified. Message sent was: ' + JSON.parse(post_data).message);
});
}, function(err) {
console.log('Error: ' + err);
context.fail('Lambda failed with error ' + err);
});
}