BOT_open_sesam/main.py

69 lines
1.9 KiB
Python
Executable File

import asyncio
import logging
import sys
import requests
from aiogram.types import Message
from aiogram.filters import CommandStart
from aiogram import Bot, Dispatcher
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram import F
from os import getenv
from buttons import FBI_open_up
from init_config import check_env_file, create_env_file, load_env
if not check_env_file():
create_env_file()
load_env()
TOKEN = getenv("TOKEN")
dp = Dispatcher()
@dp.message(CommandStart())
async def command_start_handler(msg: Message):
await msg.answer("msg happens", reply_markup=FBI_open_up())
@dp.message(F.text == "Сизам вскройся")
async def handle_open_door(msg: Message):
user_id = msg.from_user.id
# print(msg.__dict__)
print(user_id)
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)}")
async def main() -> None:
# Initialize Bot instance with default bot properties which will be passed to all API calls
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
# And the run events dispatching
await dp.start_polling(bot)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
asyncio.run(main())