-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (61 loc) · 1.86 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
// https://github.com/yagop/node-telegram-bot-api
const TelegramBot = require('node-telegram-bot-api');
const dotenv = require('dotenv');
dotenv.config();
const token = process.env.API_TOKEN;
const bot = new TelegramBot(token, {
polling: true
});
// Create USD currency formatter.
let formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
// Matches /start
bot.onText(/\/start/, function onStartText(msg) {
const opts = {
reply_to_message_id: msg.message_id,
reply_markup: JSON.stringify({
keyboard: [
[{
text: 'Gime Price Bitcoin'
}],
[{
text: 'Gime Price Etherum'
}]
]
})
};
bot.sendMessage(msg.chat.id, 'Choose the Crypto Currency', opts);
});
// Matches Bitcoin
bot.onText(/Bitcoin/, function onBitcoinText(msg) {
const chatId = msg.chat.id;
sendCryptoPrice("btc-usd", chatId);
});
// Matches Etherum
bot.onText(/Etherum/, function onEtherumText(msg) {
const chatId = msg.chat.id;
sendCryptoPrice("eth-usd", chatId);
});
bot.on('polling_error', (err) => {
console.log(err);
})
function sendCryptoPrice(currency, chatId) {
const https = require('https');
https.get(`https://api.cryptonator.com/api/ticker/${currency}`, (resp) => {
let data;
// A chunk of data has been received.
resp.on('data', (chunk) => {
data = chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
var jsonData = JSON.parse(data);
var amount = jsonData.ticker.price;
bot.sendMessage(chatId, 'The price is: ' + formatter.format(amount));
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}