-
Notifications
You must be signed in to change notification settings - Fork 0
/
pusher_test.js
62 lines (50 loc) · 1.74 KB
/
pusher_test.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
function sendToPusher(event,data) {
var pvals={
appId: '',
key: '',
secret: '',
cluster: 'us2',
encrypted: true
};
var url = `https://api-${pvals["cluster"]}.pusher.com/apps/${pvals["appId"]}/events`;
var body = {"name":event,"channels":["my-channel"],"data":JSON.stringify(data)};
var bodystring = JSON.stringify(body);
var now=new Date();
var d = Math.round(now.getTime() / 1000);
var auth_timestamp = d;
var auth_version = '1.0';
var bodymd5 = byteToString(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, bodystring));
var wholething = `POST
/apps/${pvals["appId"]}/events
auth_key=${pvals["key"]}&auth_timestamp=${auth_timestamp}&auth_version=${auth_version}&body_md5=${bodymd5}`;
var wholethingencrypt = byteToString(Utilities.computeHmacSha256Signature(wholething,pvals["secret"]));
Logger.log(wholethingencrypt);
var options = {
'method' : 'post',
'contentType': 'application/json',
// Convert the JavaScript object to a JSON string.
'payload' : bodystring,
'muteHttpExceptions' : true
};
var urltry = UrlFetchApp.fetch(url+`?auth_key=${pvals["key"]}&auth_timestamp=${auth_timestamp}&auth_version=${auth_version}&body_md5=${bodymd5}&auth_signature=${wholethingencrypt}`, options);
}
function byteToString(byte) {
var signature = byte.reduce(function(str,chr){
chr = (chr < 0 ? chr + 256 : chr).toString(16);
return str + (chr.length==1?'0':'') + chr;
},'');
return signature;
}
function sendMessage(message)
{
sendToPusher('test', {message: message});
}
function test()
{
sendMessage("hi")
}
function doGet(e)
{
var t = HtmlService.createTemplateFromFile("main");
return t.evaluate();
}