-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
131 lines (115 loc) · 3.44 KB
/
bot.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const Telegraf = require('telegraf');
const axios = require('axios');
const bot = new Telegraf('YOUR TELEGRAM API HERE');
const apiKey = 'YOUR CRYPTOCOMPARE API HERE';
bot.command('start', ctx => {
sendStartMsg(ctx);
})
bot.action("start", ctx => {
ctx.deleteMessage();
sendStartMsg(ctx);
})
bot.action("price", ctx => {
let priceMsg = "Get Price Information. Select one of the crypto below."
ctx.deleteMessage();
bot.telegram.sendMessage(ctx.chat.id, priceMsg,
{
reply_markup: {
inline_keyboard: [
[
{ text: "BTC", callback_data: "price-BTC" },
{ text: "ETH", callback_data: "price-ETH" }
],
[
{ text: "XTZ", callback_data: "price-XTZ" }
],
[
{ text: "Back to Menu", callback_data: "start" }
]
]
}
})
})
function sendStartMsg(ctx) {
let startMessage = "Welcome, this bot gives you cryptocurrency information";
bot.telegram.sendMessage(ctx.chat.id, startMessage,
{
reply_markup: {
inline_keyboard: [
[
{ text: "Crypto Prices", callback_data: "price" }
],
[
{ text: "CoinMarketCap", url: "https://coinmarketcap.com/" }
],
[
{ text: "Bot Info", callback_data: "info" }
]
]
}
})
}
let priceActionList = ["price-BTC", "price-ETH", "price-XTZ"];
bot.action(priceActionList, async ctx => {
let symbol = ctx.match.split("-")[1];
try {
let res = await axios.get(`https://min-api.cryptocompare.com/data/pricemultifull?fsyms=${symbol}&tsyms=USD&api_key=${apiKey}`);
let data = res.data.DISPLAY[symbol].USD
let message =
`
Symbol = ${symbol}
Price = ${data.PRICE}
Open = ${data.OPENDAY}
High = ${data.HIGHDAY}
Low = ${data.LOWDAY}
Supply = ${data.SUPPLY}
Market Cap = ${data.MKTCAP}
`;
ctx.deleteMessage();
bot.telegram.sendMessage(ctx.chat.id, message, {
reply_markup: {
inline_keyboard: [
[
{ text: "Back to Prices", callback_data: "price"}
]
]
}
})
} catch (err) {
console.log(err);
ctx.reply("Error encountered");
}
})
bot.action('info', ctx => {
ctx.answerCbQuery();
bot.telegram.sendMessage(ctx.chat.id, "Bot info", {
reply_markup: {
keyboard: [
[
{ text: "Credits"},
{ text: "API"}
],
[
{ text: "Remove Keyboard"}
]
],
resize_keyboard: true,
one_time_keyboard: true
}
})
})
bot.hears("Credits", ctx => {
ctx.reply('This bot was made by ya boi ethan so');
})
bot.hears("API", ctx => {
ctx.reply('This bot uses cryptocompare API');
})
bot.hears("Remove Keyboard", ctx => {
bot.telegram.sendMessage(ctx.chat.id, "Removed Keyboard",
{
reply_markup: {
remove_keyboard: true
}
})
})
bot.launch();