-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelebot.py
More file actions
99 lines (82 loc) · 3.82 KB
/
Copy pathtelebot.py
File metadata and controls
99 lines (82 loc) · 3.82 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
import asyncio
import aiohttp
from aiogram import *
from aiogram import Dispatcher, Bot, types
from aiogram.types import FSInputFile
import navigator
from aiogram.filters import Command
from aiogram.fsm.context import FSMContext
from states import Choice
from vizual_photo import visual
global start_room_g
from recognition import extract_route, fixer
TOKEN = 'Your_token'
bot = Bot(token=TOKEN)
dp = Dispatcher(bot=bot)
@dp.message(Command('start'))
async def start(message: types.Message):
kb = [
[
types.KeyboardButton(text="Поиск по фото"),
types.KeyboardButton(text="Поиск по строчке")
],
]
keyboard = types.ReplyKeyboardMarkup(
keyboard=kb,
resize_keyboard=True,
input_field_placeholder="Выберите способ поиска маршрута"
)
await message.answer("Привет!\nЯ- Интеллектуальный Бот-Навигатор \nТы можешь найти маршрут от одной"
" точки до другой!\nВыбери действие", reply_markup=keyboard)
async def cmd_reply(message: types.Message):
await message.reply("Привет!\nЯ- Интеллектуальный Бот-Навигатор \nТы можешь найти маршрут от одной"
" точки до другой!\nВведи мне 2 точки в таком формате: Маршрут из Р-123 в Р-544 или Р-123 в Р-544")
@dp.message(F.text.lower() == "поиск по фото")
async def photo(message: types.Message, state: FSMContext):
await message.reply("Отправьте фото")
await state.set_state(Choice.photo)
@dp.message(F.text.lower() == "поиск по строчке")
async def string(message: types.Message, state: FSMContext):
await message.answer("Откуда и куда?")
await state.set_state(Choice.string)
@dp.message(Choice.string)
async def get_string(message: types.Message, state: FSMContext):
Result = extract_route(message.text)
start_room, end_room = Result
image_files = navigator.build_path(start_room, end_room)
if end_room == start_room: await message.answer("У самурая нет путь")
for image_path in image_files:
photo = FSInputFile(image_path)
await bot.send_photo(message.chat.id, photo=photo)
await state.clear()
@dp.message(Choice.photo)
async def get_photo(message: types.Message, state: FSMContext):
if message.photo:
photo = message.photo[-1]
file = await bot.get_file(photo.file_id)
file_url = f'https://api.telegram.org/file/bot{bot.token}/{file.file_path}'
await bot.download_file(file.file_path, "D:\\photo.png", chunk_size=1024)
async with aiohttp.ClientSession() as session:
async with session.get(file_url) as resp:
if resp.status == 200:
global start_room_g
start_room_g = visual()
if start_room_g == "Не могу распознать фото":
await message.answer(start_room_g)
else:
await message.answer('Введите конечную координату')
else:
print(f"Ошибка при загрузке файла: {resp.status}")
else:
end_room = fixer(message.text)
start_room_g = fixer(start_room_g)
# print(start_room_g, end_room)
image_files = navigator.build_path(start_room_g, end_room)
for image_path in image_files:
photo = FSInputFile(image_path)
await bot.send_photo(message.chat.id, photo=photo)
await state.clear()
async def main():
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())