37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
import asyncio
|
|
import requests
|
|
from os import getenv
|
|
|
|
from aiogram import Dispatcher, F
|
|
from aiogram.types import Message
|
|
from auth import is_user_auth
|
|
|
|
|
|
def register_open_door_handler(dp: Dispatcher):
|
|
@dp.message(F.text == "Открыть дверь")
|
|
async def open_door_handler(msg: Message):
|
|
user_id = msg.from_user.id
|
|
if not is_user_auth(user_id):
|
|
await msg.answer(
|
|
"Доступ запрщен. Необходимо предоставить свой номер телефона."
|
|
)
|
|
return
|
|
|
|
url = f"http://{getenv('LOCK_IP')}/cgi-bin/ext"
|
|
auth = ("ext", f"{getenv('AUTH_API')}")
|
|
payload = f"CARD={getenv('CARD_ID')}&DIR=0"
|
|
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
|
|
|
try:
|
|
response = await asyncio.to_thread(
|
|
requests.post, url, auth=auth, 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)}")
|