@@ -19,18 +19,16 @@ exports.create = function(name, chatBot, options) {
19
19
trigger . options . clientSecret = trigger . options . clientSecret || undefined ;
20
20
trigger . options . redirectURI = trigger . options . redirectURI || undefined ;
21
21
22
+ trigger . scope = 'wallet:accounts:read,wallet:sells:create' ;
22
23
trigger . db = ( function ( ) { try { return JSON . parse ( fs . readFileSync ( trigger . options . dbFile ) ) ; } catch ( e ) { return { } } } ) ( ) ;
23
- trigger . client ;
24
+
24
25
trigger . options . clearcommand = trigger . options . clearcommand || '!clear' ;
25
26
trigger . options . authcommand = trigger . options . authcommand || '!auth' ;
26
- trigger . options . saveTimer = trigger . options . saveTimer || 1000 * 60 * 30 ;
27
+ trigger . options . saveTimer = trigger . options . saveTimer || 1000 * 60 * 5 ;
27
28
trigger . options . sellcommand = trigger . options . sellcommand || '!sell' ;
28
29
trigger . options . buycommand = trigger . options . buycommand || '!buy' ;
29
30
trigger . options . balancecommand = trigger . options . balancecommand || '!balance' ;
30
- trigger . options . sendcommand = trigger . options . sendcommand || '!send' ;
31
- trigger . options . requestcommand = trigger . options . requestcommand || '!request' ;
32
31
trigger . options . pricecommand = trigger . options . pricecommand || '!prices' ;
33
- trigger . options . transcommand = trigger . options . transcommand || '!transfers' ;
34
32
return trigger ;
35
33
}
36
34
@@ -48,7 +46,7 @@ BitcoinTrigger.prototype._onLoad = function() {
48
46
else {
49
47
that . winston . debug ( withName + 'Wrote to db file' ) ;
50
48
}
51
- } )
49
+ } ) ;
52
50
}
53
51
catch ( e ) {
54
52
that . winston . error ( withName + 'Error: ' + e ) ;
@@ -58,10 +56,6 @@ BitcoinTrigger.prototype._onLoad = function() {
58
56
return true ;
59
57
}
60
58
if ( ! this . options . clientID || ! this . options . clientSecret || ! this . options . redirectURI || this . chatBot . options . disableWebserver === true ) {
61
- console . log ( this . options . clientID ) ;
62
- console . log ( this . options . clientSecret ) ;
63
- console . log ( this . options . redirectURI ) ;
64
- console . log ( this . chatBot . options . disableWebserver ) ;
65
59
this . winston . error ( withName + 'Must specify client ID and client Secret and redirect URI and webserver enabled!' ) ;
66
60
return false ;
67
61
}
@@ -90,9 +84,6 @@ BitcoinTrigger.prototype._respond = function(toId, userId, message) {
90
84
}
91
85
}
92
86
93
- setInterval ( function ( ) {
94
- that . _refreshToken ( userId ) ;
95
- } , 1000 * 60 * 60 * 2 ) ;
96
87
var query = this . _stripCommand ( message , this . options . balancecommand ) ;
97
88
if ( query ) {
98
89
this . _getBalance ( toId , userId ) ;
@@ -120,35 +111,99 @@ BitcoinTrigger.prototype._respond = function(toId, userId, message) {
120
111
}
121
112
122
113
query = this . _stripCommand ( message , this . options . sellcommand ) ;
114
+ if ( query && query . params . length === 3 ) {
115
+ this . _sell ( toId , userId , query . params [ 1 ] , query . params [ 2 ] ) ;
116
+ }
117
+
118
+ query = this . _stripCommand ( message , this . options . buycommand ) ;
119
+ if ( query && query . params . length === 3 ) {
120
+ this . _buy ( toId , userId , query . params [ 1 ] , query . params [ 2 ] ) ;
121
+ }
122
+
123
+ query = this . _stripCommand ( message , this . options . pricecommand ) ;
123
124
if ( query && query . params . length === 2 ) {
124
- this . _sell ( toId , userId , query . params [ 1 ] ) ;
125
+ this . _getPrices ( toId , userId , query . params [ 1 ] ) ;
125
126
}
126
127
}
127
128
128
- BitcoinTrigger . prototype . _sell = function ( toId , userId , amount ) {
129
+ BitcoinTrigger . prototype . _getPrices = function ( toId , userId , currency ) {
130
+ const _db = this . db [ userId ] ;
131
+ const client = new Coinbase . Client ( {
132
+ "accessToken" : _db . accessToken ,
133
+ "refreshToken" : _db . refreshToken
134
+ } ) ;
135
+ const withName = this . chatBot . name + '/' + this . name + ': ' ;
136
+
137
+ var sellPrice = '' ;
138
+ var buyPrice = '' ;
139
+
140
+ client . getBuyPrice ( {
141
+ "currency" : currency
142
+ } , function ( e , obj ) {
143
+ buyPrice = obj . data . amount ;
144
+ } ) ;
145
+ client . getSellPrice ( {
146
+ "currency" : currency
147
+ } , function ( e , obj ) {
148
+ sellPrice = obj . data . amount ;
149
+ } ) ;
150
+
151
+ setTimeout ( ( ) => {
152
+ this . _sendMessageAfterDelay ( toId , 'Sell price: ' + sellPrice + '. Buy price: ' + buyPrice ) ;
153
+ } , 1000 ) ;
154
+ }
155
+
156
+ BitcoinTrigger . prototype . _buy = function ( toId , userId , amount , currency ) {
157
+ const withName = this . chatBot . name + '/' + this . name + ': ' ;
158
+ const _db = this . db [ userId ] ;
159
+ const client = new Coinbase . Client ( {
160
+ "accessToken" : _db . accessToken ,
161
+ "refreshToken" : _db . refreshToken
162
+ } ) ;
163
+ const account = new Coinbase . model . Account ( client , {
164
+ "id" : _db . id
165
+ } ) ;
166
+
167
+ const args = {
168
+ "amount" : amount ,
169
+ "currency" : currency
170
+ } ;
171
+
172
+ account . buy ( args , ( e , xfer ) => {
173
+ if ( e ) {
174
+ this . winston . error ( withName + e . stack ) ;
175
+ this . _sendMessageAfterDelay ( toId , `Error buying ${ amount } ${ currency } for user ${ userId } : ${ e . message } ` ) ;
176
+ }
177
+ else {
178
+ this . _sendMessageAfterDelay ( toId , `${ userId } successfully bought ${ amount } ${ currency } !` ) ;
179
+ this . _sendMessageAfterDelay ( userId , `Transfer ID is ${ xfer . id } ` ) ;
180
+ }
181
+ } ) ;
182
+ }
183
+
184
+ BitcoinTrigger . prototype . _sell = function ( toId , userId , amount , currency ) {
129
185
const that = this ;
130
186
const withName = this . chatBot . name + '/' + this . name + ': ' ;
131
187
var _db = this . db [ userId ] ;
132
188
const client = new Coinbase . Client ( {
133
189
"accessToken" : _db . accessToken ,
134
- "refreshToken" : _db . refreshToken ,
135
- 'baseApiUri' : 'https://api.sandbox.coinbase.com/v2/' ,
136
- 'tokenUri' : 'https://api.sandbox.coinbase.com/oauth/token'
190
+ "refreshToken" : _db . refreshToken
137
191
} ) ;
138
192
const account = new Coinbase . model . Account ( client , { "id" : _db . id } ) ;
139
193
140
- var args = {
141
- "qty" : amount
142
- }
194
+ const args = {
195
+ "amount" : amount ,
196
+ "currency" : currency
197
+ } ;
143
198
144
199
account . sell ( args , function ( e , xfer ) {
145
200
if ( e ) {
146
201
that . winston . error ( withName + e . stack ) ;
147
- that . _sendMessageAfterDelay ( toId , 'Error selling ' + amount + ' bitcoints for user ' + userId + ': ' + e . message ) ;
202
+ that . _sendMessageAfterDelay ( toId , 'Error selling ' + amount + ' ' + currency + ' for user ' + userId + ': ' + e . message ) ;
148
203
}
149
204
else {
150
- that . _sendMessageAfterDelay ( toId , userId + ' successfully sold ' + amount + ' bitcoins !' ) ;
151
- that . _sendMessageAfterDelay ( userId , 'Transfer id is ' + xfer . id ) ;
205
+ that . _sendMessageAfterDelay ( toId , userId + ' successfully sold ' + amount + ' ' + currency + ' !') ;
206
+ that . _sendMessageAfterDelay ( userId , 'Transfer ID is ' + xfer . id ) ;
152
207
}
153
208
} ) ;
154
209
}
@@ -177,95 +232,67 @@ BitcoinTrigger.prototype._refreshToken = function(userId) {
177
232
}
178
233
179
234
BitcoinTrigger . prototype . _getId = function ( userId ) {
180
- const that = this ;
181
- var _db = that . db [ userId ] ;
235
+ var _db = this . db [ userId ] ;
182
236
const withName = this . chatBot . name + '/' + this . name + ': ' ;
183
237
const client = new Coinbase . Client ( {
184
238
"accessToken" : _db . accessToken ,
185
- "refreshToken" : _db . refreshToken ,
186
- 'baseApiUri' : 'https://api.sandbox.coinbase.com/v2/' ,
187
- 'tokenUri' : 'https://api.sandbox.coinbase.com/oauth/token'
239
+ "refreshToken" : _db . refreshToken
188
240
} ) ;
189
- client . getAccounts ( function ( e , accounts ) {
241
+ client . getAccounts ( { } , ( e , accounts ) => {
190
242
if ( e ) {
191
- that . winston . error ( withName + e . stack ) ;
243
+ this . winston . error ( withName + e . stack ) ;
192
244
}
193
245
else {
194
- _db . id === account . id ;
246
+ accounts . forEach ( account => {
247
+ _db . id = account . id ;
248
+ } ) ;
195
249
}
196
250
} )
197
251
}
198
252
199
253
BitcoinTrigger . prototype . _authorize = function ( userId , callback ) {
200
- const that = this ;
201
254
const withName = this . chatBot . name + '/' + this . name + ': ' ;
202
255
this . express = this . chatBot . express ;
203
256
var _url ;
204
257
var _code ;
205
258
var accessToken ;
206
259
var refreshToken ;
207
- //var _scope = 'wallet:accounts:read,wallet:accounts:create,wallet:accounts:update,wallet:accounts:delete';
208
- var _scope = 'user,balance,sell' ;
209
260
var uri = "https://sandbox.coinbase.com/oauth/authorize" +
210
- "?response_type=code&redirect_uri=" + encodeURI ( that . options . redirectURI ) +
211
- "&client_id=" + that . options . clientID + '&scope=' + _scope ;
261
+ "?response_type=code&redirect_uri=" + encodeURI ( this . options . redirectURI ) +
262
+ "&client_id=" + this . options . clientID + '&scope=' + this . scope ;
212
263
Request ( {
213
264
uri : uri ,
214
265
json : true ,
215
266
followAllRedirects : true
216
- } , function ( e , response , body ) {
267
+ } , ( e , response , body ) => {
217
268
if ( e ) {
218
269
callback ( e , null , null ) ;
219
270
}
220
271
else {
221
- that . _sendMessageAfterDelay ( userId , 'Please visit the following link and click "Authorize".\n\n' + uri ) ;
272
+ this . _sendMessageAfterDelay ( userId , 'Please visit the following link and click "Authorize".\n\n' + uri ) ;
222
273
var last = / ^ [ ^ \/ ] * (?: \/ [ ^ \/ ] * ) { 2 } / g;
223
274
//console.log(that.options.redirectURI.match(last));
224
- var getter = that . options . redirectURI . replace ( last , '' ) ;
225
- that . chatBot . _addRouter ( getter ) ;
226
- that . winston . silly ( getter ) ;
227
- /*
228
- that.express.get(getter, function(req, res) {
229
- _url = req.url;
230
- _code = _url.match(/\?code=(.*)/i)[1];
231
- res.send('<h1>Response received, you may close this window now.<h2>' + '<br><h4>Your code is ' + _code + '<h4><br>');
232
- Request({
233
- method: "POST",
234
- uri: "https://sandbox.coinbase.com/oauth/token?grant_type=authorization_code&code=" +
235
- _code + "&client_id=" + that.options.clientID + "&client_secret=" +
236
- that.options.clientSecret + "&redirect_uri=" + that.options.redirectURI,
237
- json: true,
238
- followAllRedirects: true
239
- }, function(e, response, body) {
240
- if(!body.access_token) {
241
- callback(e, null, null);
242
- }
243
- else {
244
- accessToken = body.access_token;
245
- refreshToken = body.refresh_token;
246
- callback(null, accessToken, refreshToken);
247
- }
248
- });
249
- });
250
- */
251
- that . express . use ( getter , function ( req , res , next ) {
275
+ var getter = this . options . redirectURI . replace ( last , '' ) ;
276
+ this . chatBot . _addRouter ( getter ) ;
277
+ this . express . use ( getter , ( req , res , next ) => {
252
278
_url = req . url ;
253
279
_code = _url . match ( / \? c o d e = ( .* ) / i) [ 1 ] ;
254
280
res . send ( '<h1>Response received, you may close this window now.<h2>' + '<br><h4>Your code is ' + _code + '<h4><br>' ) ;
255
281
Request ( {
256
282
method : "POST" ,
257
283
uri : "https://sandbox.coinbase.com/oauth/token?grant_type=authorization_code&code=" +
258
- _code + "&client_id=" + that . options . clientID + "&client_secret=" +
259
- that . options . clientSecret + "&redirect_uri=" + that . options . redirectURI ,
284
+ _code + "&client_id=" + this . options . clientID + "&client_secret=" +
285
+ this . options . clientSecret + "&redirect_uri=" + this . options . redirectURI ,
260
286
json : true ,
261
287
followAllRedirects : true
262
- } , function ( e , response , body ) {
288
+ } , ( e , response , body ) => {
263
289
if ( ! body . access_token ) {
264
290
callback ( e , null , null ) ;
265
291
}
266
292
else {
267
293
accessToken = body . access_token ;
268
294
refreshToken = body . refresh_token ;
295
+ this . _getId ( userId ) ;
269
296
callback ( null , accessToken , refreshToken ) ;
270
297
}
271
298
} ) ;
@@ -285,32 +312,31 @@ BitcoinTrigger.prototype._getBalance = function(toId, userId) {
285
312
var _db = that . db [ userId ] ;
286
313
var client = new Coinbase . Client ( {
287
314
"accessToken" : _db . accessToken ,
288
- "refreshToken" : _db . refreshToken ,
289
- 'baseApiUri' : 'https://api.sandbox.coinbase.com/v2/' ,
290
- 'tokenUri' : 'https://api.sandbox.coinbase.com/oauth/token'
315
+ "refreshToken" : _db . refreshToken
291
316
} ) ;
292
- client . getAccounts ( { } , ( e , accounts ) => {
317
+ client . getAccount ( _db . id , ( e , account ) => {
293
318
if ( e ) {
294
319
if ( e . type === 'ExpiredAccessToken' ) {
295
- that . _refreshToken ( userId ) ;
296
- that . _sendMessageAfterDelay ( userId , 'Access token expired, refreshing. Please try again in a few seconds.' ) ;
297
- that . winston . warn ( withName + 'Refreshing access token' ) ;
320
+ this . _refreshToken ( userId ) ;
321
+ this . _sendMessageAfterDelay ( userId , 'Access token expired, refreshing. Please try again in a few seconds.' ) ;
322
+ this . winston . warn ( withName + 'Refreshing access token' ) ;
298
323
}
299
- that . winston . error ( e ) ;
300
- that . _sendMessageAfterDelay ( userId , e . message ) ;
324
+ this . winston . error ( e . stack ) ;
325
+ this . _sendMessageAfterDelay ( userId , e . message ) ;
301
326
}
302
327
else {
303
- accounts . forEach ( function ( acct ) {
304
- that . _sendMessageAfterDelay ( toId , 'The balance for ' + that . chatBot . _userString ( userId ) + ' is ' + acct . balance . amount + ' ' + acct . balance . currency ) ;
305
- } )
328
+ this . _sendMessageAfterDelay ( toId , 'The balance for ' + this . chatBot . _userString ( userId ) + ' is ' + account . balance . amount + ' ' + account . balance . currency ) ;
306
329
}
307
330
} )
308
331
}
309
332
}
310
333
311
334
BitcoinTrigger . prototype . _stripCommand = function ( message , command ) {
312
335
if ( command && message && message . toLowerCase ( ) . indexOf ( command . toLowerCase ( ) ) === 0 ) {
313
- return { message : message , params : message . split ( " " ) } ;
336
+ return {
337
+ message : message ,
338
+ params : message . split ( " " )
339
+ } ;
314
340
}
315
341
return null ;
316
342
}
0 commit comments