-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
96 lines (75 loc) · 3.25 KB
/
main.py
File metadata and controls
96 lines (75 loc) · 3.25 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
import asyncio
import json
import random
import websockets
from aiogram import Bot, Dispatcher
from aiogram.filters import Command
from aiogram.types import Message
from settings import BOT_TOKEN, SUBSCRIBED_CHAT_ID
# Глобальные переменные
last_notified_price = None # Цена последнего уведомления
current_price = None # Текущая цена
subscribed_chat_id = SUBSCRIBED_CHAT_ID # ID чата для уведомлений
async def on_message(websocket, bot):
global last_notified_price, current_price
async for message in websocket:
data = json.loads(message)
price_str = data.get("p")
if not price_str:
continue
new_price = float(price_str)
current_price = new_price # Всегда обновляем текущую цену
# Инициализация при первом запуске
if last_notified_price is None:
last_notified_price = new_price
continue
# Проверяем изменение цены относительно последнего уведомления
price_diff = abs(new_price - last_notified_price)
if price_diff >= 0.1 and subscribed_chat_id:
direction = (
"🟢 ВЗЛЕТЕЛА" if new_price > last_notified_price else "🔪 РУХНУЛА"
)
await bot.send_message(
subscribed_chat_id,
f"🚨 {direction} НА {price_diff:.2f}$!\n"
f"👉 Текущая: ${new_price:.2f}\n"
f"⏳ Предыдущий порог: ${last_notified_price:.2f}\n"
f"#{'TO_THE_MOON' if direction == '🟢 ВЗЛЕТЕЛА' else 'DUMPSTER_FIRE'}",
)
last_notified_price = new_price
async def subscribe(bot):
uri = "wss://fstream.binance.com/ws/trumpusdt@aggTrade"
while True:
try:
async with websockets.connect(uri) as websocket:
await websocket.send(
json.dumps(
{
"method": "SUBSCRIBE",
"params": ["trumpusdt@aggTrade"],
"id": 1,
}
)
)
await on_message(websocket, bot)
except Exception as e:
print(f"Connection error: {e}")
await asyncio.sleep(5)
async def main():
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher()
# Команда /p - текущая цена
@dp.message(Command("p"))
async def price_cmd(message: Message):
if current_price is not None:
await message.answer(
f"📊 Текущая цена TRUMP: ${current_price:.4f}\n"
f"📈 Изменение от последнего уведомления: "
f"{'🟢 +' if current_price >= last_notified_price else '🔴 '}"
f"{(current_price - last_notified_price):.2f}$"
)
else:
await message.answer("⏳ Данные о цене еще не получены...")
await asyncio.gather(dp.start_polling(bot), subscribe(bot))
if __name__ == "__main__":
asyncio.run(main())