-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.gs
161 lines (125 loc) · 5.11 KB
/
Code.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const DEBUG_MODE = false;
function onGmailInsertIntoEmail(e) {
var invoice = JSON.parse(e.parameters.invoice);
var sats = JSON.parse(e.parameters.sats);
var maxAttempts = 10;
var attempt = 0;
var lastStatus = null;
while(attempt < maxAttempts)
{
var statusResponse = UrlFetchApp.fetch('https://api.lightning.gifts/status/' + invoice.chargeId);
if(DEBUG_MODE) Logger.log(statusResponse);
var status = JSON.parse(statusResponse.getContentText())
lastStatus = status;
if(status.status.toLowerCase() == "paid")
{
break;
}
// Wait 1 second before trying again...
Utilities.sleep(1000);
++attempt;
}
if(lastStatus == null || lastStatus.status.toLowerCase() != "paid")
{
return null;
}
// Now determine USD value...
var toCurrency = "usd";
var usdResponse = UrlFetchApp.fetch('https://api.bitfinex.com/v1/pubticker/btc' + toCurrency);
if(DEBUG_MODE) Logger.log(usdResponse);
var usd = JSON.parse(usdResponse.getContentText())
var usdPerSat = parseFloat(usd.last_price) / 100000000;
var usdValue = usdPerSat * sats;
if(DEBUG_MODE) Logger.log(invoice);
var orderId = invoice.orderId;
var lnurl = invoice.lnurl;
var giftUrl = "https://lightning.gifts/redeem/" + orderId + "?lightning=" + lnurl;
var imageUrl = Utilities.formatString('https://api.qrserver.com/v1/create-qr-code/?margin=10&size=150x150&data=%s', encodeURIComponent(giftUrl));
var htmlContent = "<table style='margin-top: 20px; background-color: #F7931A; border-radius: 6px;'>";
htmlContent+= "<tbody>";
htmlContent+= "<tr>";
htmlContent+= "<td>"
htmlContent+= '<a href="' + giftUrl + '"><img style="display: block; max-width: 150px; max-height: 150px; border-radius: 6px;" src="'
+ imageUrl + '"/></a>'
htmlContent+= "</td>"
htmlContent+= "<td style='padding: 10px;'>"
htmlContent+= "<p style='margin: 0px; margin-bottom: 10px; color: #000000;'>Here are <strong>" + sats + " ₿ satoshis</strong>.</p>"
htmlContent+= "<p style='margin: 0px; margin-bottom: 10px; color: #000000;'>Scan the QR code with a Bitcoin wallet to receive them.</p>"
htmlContent+= "<p style='margin: 0px; color: #000000;'>Estimated value at the time sent: <strong>$" + usdValue.toFixed(2) + "</strong></p>"
htmlContent+= "</td>"
htmlContent+= "</tr>";
htmlContent+= "</tbody>";
htmlContent+= "</table>";
var response = CardService.newUpdateDraftActionResponseBuilder()
.setUpdateDraftBodyAction(CardService.newUpdateDraftBodyAction()
.addUpdateContent(htmlContent, CardService.ContentType.MUTABLE_HTML)
.setUpdateType(CardService.UpdateDraftBodyType.INSERT_AT_END));
return response.build();
}
function onGmailCreateGift(e) {
var sats = parseInt(e.formInputs.satsInput[0])
var dataToPost = {
amount: sats
};
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(dataToPost)
};
var createResponse = UrlFetchApp.fetch('https://api.lightning.gifts/create', options);
if(DEBUG_MODE) Logger.log(createResponse);
var invoice = JSON.parse(createResponse.getContentText())
var header = CardService.newCardHeader()
.setTitle('Send ' + sats + ' satoshis to here using wallet');
var lnbc = invoice.lightningInvoice.payreq;
var qrCodeUrl = "https://api.qrserver.com/v1/create-qr-code/?margin=50&size=300x300&data=" + lnbc;
var image = CardService.newImage().setAltText("QR Code").setImageUrl(qrCodeUrl);
// Create a button that inserts a QR code into the email
var action = CardService.newAction()
.setFunctionName('onGmailInsertIntoEmail')
.setParameters({
'invoice': JSON.stringify(invoice),
'sats': JSON.stringify(sats)
});
var button = CardService.newTextButton()
.setText('I\'ve done that')
.setOnClickAction(action)
.setTextButtonStyle(CardService.TextButtonStyle.FILLED);
var buttonSet = CardService.newButtonSet()
.addButton(button);
// Assemble the widgets and return the card.
var section = CardService.newCardSection()
.addWidget(image)
.addWidget(buttonSet);
var card = CardService.newCardBuilder()
.setHeader(header)
.addSection(section);
return card.build();
}
function onGmailCompose(e) {
var header = CardService.newCardHeader()
.setTitle('Enter how many satoshis to send');
// Create text input for entering amount (in sats)
var input = CardService.newTextInput()
.setFieldName('satsInput')
.setTitle('Amount (satoshis)');
// Create a button that creates a gift
var action = CardService.newAction()
.setFunctionName('onGmailCreateGift');
var button = CardService.newTextButton()
.setText('Transfer')
.setOnClickAction(action)
.setTextButtonStyle(CardService.TextButtonStyle.FILLED);
var buttonSet = CardService.newButtonSet()
.addButton(button);
// Assemble the widgets and return the card.
var section = CardService.newCardSection()
.addWidget(input)
.addWidget(buttonSet);
var card = CardService.newCardBuilder()
.setHeader(header)
.addSection(section);
return card.build();
}
function onDefaultHomePageOpen() {
}