-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
50 lines (40 loc) · 2.14 KB
/
app.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
const request = require('request-promise-native');
const API_TOKEN = "your_api_token";
const USER_ID = "your_phone_number"; //Example: "+48452353934"
const CRYPTO_CURRENCY = "your_crypto_currency"; //Example: "bitcoin", "litecoin"
const TIME_OUT = "600000";
const CURRENCY = 'your_currency'; // Example: "EUR", "USD"
const getPrice = (coin, currency) => {
return request(`https://api.coinmarketcap.com/v1/ticker/${coin}/?convert=${currency}`).then(data => {
const prices = JSON.parse(data);
return Promise.resolve(prices.map(price => price[`price_${currency.toLowerCase()}`]));
}).catch(e => Promise.reject(`Unable to get data, error: ${e}`));
};
const sendMessage = (token, userId, message) => {
return request({
uri: `https://graph.facebook.com/v2.6/me/messages?access_token=${token}`,
method: 'POST',
body: { recipient: { phone_number: userId }, message: { text: message } },
json: true
}).then(() => Promise.resolve('Message was send')).catch(() => Promise.reject('Unable to send message'));
};
let wait = ms => new Promise(resolve => setTimeout(resolve, ms));
const sendInfo = async (token, userId, cryptoCurrency, currency, timeOut) => {
const previousPrice = await getPrice(cryptoCurrency, currency);
console.log(`INFO: Previous price ${cryptoCurrency}: ${previousPrice}`);
await wait(timeOut);
const actualPrice = await getPrice(cryptoCurrency, currency);
console.log(`INFO: Actual price ${cryptoCurrency}: ${actualPrice}`);
const pricesLength = previousPrice.length > actualPrice.length ? previousPrice.length : actualPrice.length;
for (let i = 0; i < pricesLength; i++) {
previousPrice[i] = parseFloat(previousPrice[i]).toFixed(2);
actualPrice[i] = parseFloat(actualPrice[i]).toFixed(2);
if (previousPrice[i] !== actualPrice[i]) {
const message = `${cryptoCurrency}: ${actualPrice[i]}`;
sendMessage(token, userId, message).then(() => {
console.log(`INFO: Message sended: ${message}`);
}).catch(e => console.log(e));
}
}
};
sendInfo(API_TOKEN, USER_ID, CRYPTO_CURRENCY, CURRENCY, TIME_OUT);