-
Notifications
You must be signed in to change notification settings - Fork 8
/
Library.gs
59 lines (49 loc) · 1.74 KB
/
Library.gs
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
var _ = Underscore.load();
/*
function initializer(sid, tkn, options) {
return new RestClient(sid, tkn, options);
}
//Main functional components of the Twilio module
initializer.RestClient = RestClient;
initializer.Capability = require('./Capability');
initializer.TwimlResponse = require('./TwimlResponse');
*/
/**
Utility function to validate an incoming request is indeed from Twilio
@param {string} authToken - The auth token, as seen in the Twilio portal
@param {string} twilioHeader - The value of the X-Twilio-Signature header from the request
@param {string} url - The full URL (with query string) you configured to handle this request
@param {object} params - the parameters sent with this request
*/
function validateRequest(authToken, twilioHeader, url, params) {
Object.keys(params).sort().forEach(function(key, i) {
url = url + key + params[key];
});
return twilioHeader === crypto.createHmac('sha1', authToken).update(url).digest('Base64');
};
var request = function(client, options, callback){
var err, response, body,
url = options.url ;
delete options.url;
options.headers = {
"Authorization" : "Basic " + Utilities.base64Encode(client.accountSid + ':' + client.authToken)
};
if( options.form){
options.payload = options.form;
delete options.form;
}
if( options.qs){ //pending, must append queryString parameters to the url
url = url + '?' + querystringgas.stringify(options.qs);
delete options.qs;
}
try{
var fetchResponse = UrlFetchApp.fetch(url, options);
}catch(e){
err = e
}
if(fetchResponse){
response = { statusCode : fetchResponse.getResponseCode() };
body = fetchResponse.getContentText();
}
callback.call(client, err, response, body);
}