-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
102 lines (77 loc) · 2.97 KB
/
Copy pathmain.py
File metadata and controls
102 lines (77 loc) · 2.97 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
import asyncio
import re
import aiohttp
import logging
import os
from aiogram import Bot, Dispatcher, F
from aiogram.types import Message
from aiogram.filters import Command
from dotenv import load_dotenv
load_dotenv()
BOT_TOKEN = os.getenv("BOT_TOKEN")
FALLBACK_RATE = 450.0 # курс на случай если API недоступно
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher()
async def get_usd_rate() -> float:
"""Берём актуальный курс KZT/USD с открытого API."""
try:
async with aiohttp.ClientSession() as session:
url = "https://api.exchangerate-api.com/v4/latest/USD"
async with session.get(url, timeout=aiohttp.ClientTimeout(total=5)) as resp:
if resp.status == 200:
data = await resp.json()
rate = data["rates"].get("KZT")
if rate:
return float(rate)
except Exception as e:
logger.warning(f"Не удалось получить курс: {e}. Используем fallback {FALLBACK_RATE}")
return FALLBACK_RATE
# Ловим: 500тг / 500 тг / 500тенге / 500 тенге / 500₸ / 500 ₸
PATTERN = re.compile(
r"(\d[\d\s,.']*)\s*(тг|тенге|₸)",
re.IGNORECASE | re.UNICODE
)
def parse_amount(raw: str) -> float:
"""Чистим строку от пробелов и запятых, возвращаем число."""
cleaned = re.sub(r"[\s,']", "", raw).replace(".", "")
return float(cleaned)
def format_usd(amount: float) -> str:
if amount < 0.01:
return f"{amount:.4f} $"
elif amount < 1:
return f"{amount:.2f} $"
return f"{amount:,.2f} $".replace(",", " ")
@dp.message(Command("start"))
async def cmd_start(message: Message):
await message.reply(
"👋 Пишите суммы в тенге — переведу в доллары автоматически.\n\n"
"Примеры: <b>500тг</b>, <b>1500 тенге</b>, <b>10000₸</b>",
parse_mode="HTML"
)
@dp.message(F.text)
async def handle_message(message: Message):
text = message.text or ""
matches = PATTERN.findall(text)
if not matches:
return # нет суммы в тенге — молчим
rate = await get_usd_rate()
lines = []
for raw_amount, _ in matches:
try:
kzt = parse_amount(raw_amount)
usd = kzt / rate
kzt_str = f"{kzt:,.0f}".replace(",", " ")
lines.append(f"💰 <b>{kzt_str} ₸</b> = <b>{format_usd(usd)}</b>")
except ValueError:
continue
if lines:
result = "\n".join(lines)
result += f"\n\n<i>📈 Курс: 1 $ = {rate:.1f} ₸</i>"
await message.reply(result, parse_mode="HTML")
async def main():
logger.info("Бот запущен")
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())