53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
import asyncio
|
|
import requests
|
|
|
|
from aiogram import Dispatcher
|
|
from aiogram.types import Message
|
|
|
|
from auth import AUTHORIZED_USERS, is_user_auth
|
|
from config import config
|
|
|
|
|
|
def register_open_door_handler(dp: Dispatcher):
|
|
@dp.message()
|
|
async def open_door_handler(msg: Message):
|
|
user_id = msg.from_user.id
|
|
if not is_user_auth(user_id):
|
|
await msg.answer(
|
|
"Доступ запрщен. Необходимо предоставить свой номер телефона."
|
|
)
|
|
|
|
user_data = AUTHORIZED_USERS.get(user_id)
|
|
if not user_data:
|
|
await msg.answer("Ошибка авторизации пользователя.")
|
|
return
|
|
|
|
lock_key = msg.text
|
|
lock_conf = config.get("locks", {}).get(msg.text)
|
|
if not lock_conf:
|
|
await msg.answer("Информации по замку не найдено")
|
|
return
|
|
|
|
url = f"http://{lock_conf['ip']}/cgi-bin/ext"
|
|
auth_info = ("ext", lock_conf["pass"])
|
|
payload = f"CARD={user_data['card']}&DIR=0"
|
|
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
|
|
|
try:
|
|
response = await asyncio.to_thread(
|
|
requests.post,
|
|
url,
|
|
auth=auth_info,
|
|
data=payload,
|
|
headers=headers,
|
|
timeout=5,
|
|
)
|
|
if response.status_code == 200:
|
|
await msg.answer("Открыто")
|
|
else:
|
|
await msg.answer(
|
|
f"Ошибка при открытии замка. Код ошибки: {response.status_code}"
|
|
)
|
|
except Exception as e:
|
|
await msg.answer(f"Исключение: {str(e)}")
|