-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (92 loc) · 3.38 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
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
const TelegramApi = require("node-telegram-bot-api");
const axios = require("axios");
require("dotenv").config();
const { coin } = require("./coin");
const token = process.env.TELEGRAM_TOKEN;
const bot = new TelegramApi(token, { polling: true });
bot.setMyCommands([
{ command: "/start", description: "Старт" },
{ command: "/set", description: "изменить значение в %" },
{ command: "/stop", description: "Остановить бот" },
]);
async function checkPriceChange(chatId, resMsg, bool) {
const coins = coin;
console.log(bool);
try {
// Получение списка монет
const responseSymbols = await axios.get(
"https://api.bybit.com/spot/v3/public/symbols"
);
if (responseSymbols.status === 200) {
const quotes = responseSymbols.data.result.list;
coins.push(...quotes.map((coin) => coin.name));
}
// Запрос всех пар
do {
for (const symbol of coins) {
const intervalResponse = await axios.get(
`https://api.bybit.com/spot/v3/public/quote/kline?symbol=${symbol}&interval=1m&limit=1`
);
if (intervalResponse.status === 200) {
const prices = intervalResponse.data.result.list;
// Проверка, что prices.list не пуст и имеет необходимые свойства
if (prices && prices.length > 0) {
for (const price of prices) {
const priceChangePercent = ((price.h - price.o) / price.o) * 100;
if (
priceChangePercent >= resMsg ||
priceChangePercent <= -resMsg
) {
bot.sendMessage(
chatId,
`Пара: #${symbol}, Цена изменилась на: ${priceChangePercent.toFixed(
2
)}% за 1мин. Объем: ${price.v}$.\nЦена открытия: ${price.o}$`
);
}
}
}
}
}
} while (bool);
bot.sendMessage(chatId, `Выполнение запросов окончено`);
console.log("Killed");
} catch (error) {
console.error(`For of: ${error.message}`);
}
}
bot.onText(/\/start/, async (msg) => {
const chatId = msg.chat.id;
console.log(`New user ${msg.from.first_name} connected`);
bot.sendMessage(
chatId,
`Привет ${msg.from.first_name} теперь тебе будут приходить уведомления об изменении цены криптовалют биржи Bybit. Отправте число от 1-99 для изменения разницы в процентах.`
);
});
bot.on("message", async (msg) => {
const text = msg.text;
const chatId = msg.chat.id;
if (text) {
const resMsg = parseInt(text);
if (resMsg >= 0) {
console.log(`User ${msg.from.first_name} set ${resMsg} % value`);
let bool = true;
checkPriceChange(chatId, resMsg, bool);
bot.sendMessage(chatId, `Вы задали ${resMsg} % зазора`);
return;
}
return;
}
});
bot.onText(/\/stop/, async (msg) => {
const text = msg.text;
const chatId = msg.chat.id;
if (text === "/stop") {
console.log("Bot stop");
checkPriceChange(chatId, 100, false);
bot.sendMessage(chatId, `Вы отключили бот`);
return;
}
});
const date = new Date() + 2;
console.log("Server started", date);