-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (65 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
require('dotenv').config()
const TelegramBot = require('node-telegram-bot-api');
const CoinGecko = require('coingecko-api');
const token = process.env.TOKEN;
const bot = new TelegramBot(token, {polling: true});
const CoinGeckoClient = new CoinGecko();
const getCryptoPrice = async(crypto) => {
let data = await CoinGeckoClient.coins.fetch(crypto, {
tickers: false,
community_data: false,
developer_data: false,
localization: false,
});
return data.data.market_data.current_price.usd
};
const keyboard = [
[{text: 'Bitcoin (BTC)', callback_data: 'bitcoin'}],
[{text: 'Ethereum (ETH)', callback_data: 'ethereum'}],
[{text: 'Tether (USDT)', callback_data: 'tether'}],
];
bot.on('message', (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(chatId, 'Hello, friend! What cryptocurrency rate are you interested in?', {
reply_markup: {
inline_keyboard: keyboard
}
});
});
bot.on('callback_query', async (query) => {
const chatId = query.message.chat.id;
let coin = query.data
let result = '';
console.log('USER ===>', query.from.username, new Date());
switch (coin) {
case 'bitcoin':{
result = await getCryptoPrice(coin);
const current = `Bitcoin (BTC) -${result}$ \nClick again!`
bot.sendMessage(chatId, current, {
reply_markup: {
inline_keyboard: keyboard
}});
break;
}
case 'ethereum':{
result = await getCryptoPrice(coin);
const current = `Ethereum (ETH) -${result}$ \nClick again!`
bot.sendMessage(chatId, current, {
reply_markup: {
inline_keyboard: keyboard
}});
break;
}
case 'tether':{
result = await getCryptoPrice(coin);
const current = `Tether (USDT) -${result}$ \nClick again!`
bot.sendMessage(chatId, current, {
reply_markup: {
inline_keyboard: keyboard
}});
break;
}
default:
break;
}
});