-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
84 lines (69 loc) · 1.93 KB
/
index.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
'use strict';
require('dotenv').config();
const request = require('request');
const express = require('express');
const bodyParser = require('body-parser');
const bittrex = require('node.bittrex.api');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
bittrex.options({
'apikey' : process.env.BITTREX_API_KEY,
'apisecret' : process.env.BITTREX_API_SECRET,
'stream' : false,
'verbose' : false,
'cleartext' : false
});
const server = app.listen(process.env.PORT || 8080, () => {
console.log('Express server listening on port %d in %s mode', server.address().port, app.settings.env);
});
app.get('/', (req, res) => {
handleQueries(req.query, res);
});
app.post('/', (req, res) => {
handleQueries(req.body, res);
});
/*
response:
{ token: 'some token',
team_id: 'T1L---',
team_domain: 'some-domain',
channel_id: 'C1L---',
channel_name: 'general',
user_id: 'U1L----',
user_name: 'some user',
command: '/satoshi',
text: '1000',
response_url: 'https://hooks.slack.com/commands/--- }
*/
function handleQueries(q, res) {
const { body } = req;
if (body.text) {
const amountOfSatoshi = parseFloat(body.text);
if (isNaN(amountOfSatoshi)) {
res.send('Please enter a valid amount');
return;
}
bittrex.getmarketsummary({ market: 'USDT-BTC' }, (data) => {
const satoshi = 0.00000001;
const bitcoinPriceUsd = data.result[0].Last;
const usd = bitcoinPriceUsd * (amountOfSatoshi * satoshi);
const json = {
response_type: 'in_channel', // public to the channel
text: `$${usd.toFixed(4).toString()}`,
};
res.json(json);
});
} else {
const json = {
response_type: 'ephemeral', // private message
text: 'How to use /satoshi command:',
attachments: [
{
text: 'Type a value after the command, e.g. `/satoshi 1000`',
},
],
};
res.json(json);
}
}