Repository navigation
Expand file tree
/
Copy pathbot.js
More file actions
243 lines (218 loc) · 6.54 KB
/
Copy pathbot.js
File metadata and controls
243 lines (218 loc) · 6.54 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// CRYPTO BOT
// API CRYPTO COMPARE
// name: CryptoBot ; username : crypto_2020_bot
// We will practice Keyboards in Telegram:
// 1. Reply Keyboard
// 2. Inline Keyboard
// We will study interactive Menues
// IMPORT
const Telegraf = require('telegraf');
const axios = require('axios');
require('dotenv').config();
// CONSTANTs
const textHelp =
`
CRYPTO BOT
Welcome, this bot gives you cryptocurrency information.
/start -
/help -
/info -
-----------
/test -
`
// FUNCTIONS
const sendStartMessage = (ctx) => {
bot.telegram.sendMessage(ctx.chat.id, textHelp,
{
reply_markup : {
inline_keyboard : [
[
{text : 'Crypto Prices', callback_data : 'price'}
],
[
{text : 'Coin Market Cap', url : 'https://coinmarketcap.com/'}
],
[
{text : 'Bot Info', callback_data : 'info'}
],
]
}
}
);
}
// Create a new bot
const bot = new Telegraf(process.env.TOKEN);
// BOT's CODE
// start and help command
bot.command(['start', 'help'], ctx => {
sendStartMessage(ctx);
});
// price action
bot.action('price', ctx => {
ctx.deleteMessage();
let priceMessage = `Get price information. Select one of the cryptocurrencies below`;
bot.telegram.sendMessage(ctx.chat.id, priceMessage,
{
reply_markup : {
inline_keyboard : [
[
{text : 'BTC', callback_data : 'price-BTC'},
{text : 'ETH', callback_data : 'price-ETH'},
],
[
{text : 'BCH', callback_data : 'price-BCH'},
{text : 'LTC', callback_data : 'price-LTC'},
],
[
{text : 'Back to Menu', callback_data : 'start'},
],
]
}
}
);
});
// start action
bot.action('start', ctx => {
ctx.deleteMessage();
sendStartMessage(ctx);
});
//
let priceActionList = ['price-BTC', 'price-ETH', 'price-BCH', 'price-LTC'];
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=${process.env.API_KEY_CRYPTO}`)
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(error){
console.log(error);
ctx.reply('Error has encountered');
}
// https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD
});
//
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 @meifius');
});
//
bot.hears('API', ctx => {
ctx.reply('This bot uses cryptocompare API');
});
//
bot.hears('Remove Keyboard', ctx => {
bot.telegram.sendMessage(ctx.chat.id, 'Remove Keyboard', {
reply_markup : {
remove_keyboard : true
}
});
});
// --------------------------------------------------------
// test command
bot.command('test', ctx => {
bot.telegram.sendMessage(ctx.chat.id, 'Hi',
{
reply_markup : {
inline_keyboard : [
[ // first row
{text : 'oooohhhh God1', callback_data : 'one'},
{text : 'oooohhhh God11', callback_data : 'one'},
],
[ // second row
{text : 'Express Routing Doc', url : 'https://expressjs.com/es/guide/routing.html', callback_data : 'one'},
],
[ // third row
{text : 'oooohhhh God3', callback_data : 'one'},
{text : 'oooohhhh God33', callback_data : 'one'},
{text : 'Second Menu', callback_data : 'two'},
],
]
}
});
});
// actions "functions"
// one test action
bot.action('one', ctx => {
ctx.answerCbQuery('Pop-up Hello!');
// bot.telegram.sendChatAction('upload_photo');
ctx.reply('You have pressed the Button!');
console.log('The user press the "one" button');
});
// two test action to Send another Menu, deleting the actual menu first
bot.action('two', ctx => {
ctx.deleteMessage();
bot.telegram.sendMessage(ctx.chat.id, 'Second Menu',
{
reply_markup : {
inline_keyboard : [
[ // second row
{text : 'Back to the Menu', callback_data : 'three'},
]
]
}
});
});
// action to Back to the Menu
bot.action('three', ctx => {
ctx.deleteMessage();
bot.telegram.sendMessage(ctx.chat.id, 'Welcome Menu',
{
reply_markup : {
inline_keyboard : [
[ // first row
{text : 'oooohhhh God1', callback_data : 'one'},
{text : 'oooohhhh God11', callback_data : 'one'},
],
[ // second row
{text : 'Express Routing Doc', url : 'https://expressjs.com/es/guide/routing.html', callback_data : 'one'},
],
[ // third row
{text : 'oooohhhh God3', callback_data : 'one'},
{text : 'oooohhhh God33', callback_data : 'one'},
{text : 'Second Menu', callback_data : 'two'},
],
]
}
});
});
// --------------------------------------------------------
// Listen
bot.launch();