-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
149 lines (115 loc) · 6.7 KB
/
main.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import logging
from datetime import datetime
import bson
from aiogram import Bot, Dispatcher, types
from aiogram import F
from aiogram.filters import Command
from aiogram.fsm.context import FSMContext
from aiogram.fsm.state import State, StatesGroup
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from aiogram.utils.keyboard import ReplyKeyboardBuilder
from config import config
from database import createTicket, updateTicketStatus, setTicketRating, getTicketByID, getUser, initUser, closeTicket
bot = Bot(token=config.BOT_TOKEN)
dp = Dispatcher()
logging.basicConfig(level=logging.INFO)
class TicketForm(StatesGroup):
cabinetName = State()
description = State()
@dp.message(Command("start"))
async def startCommand(message: types.Message):
keyboard = ReplyKeyboardBuilder()
keyboard.button(text="Оставить заявку")
await message.answer(text="Выберите действие:",
reply_markup=keyboard.as_markup(resize_keyboard=True))
@dp.message(F.text == "Оставить заявку")
async def openTicket(message: types.Message, state: FSMContext):
# user = await getUser(message.from_user.id)
# if user is not None:
# await message.answer("Вы не можете создавать сами себе заявки.\n")
# return
await message.answer("Введите номер или название кабинета")
await state.set_state(TicketForm.cabinetName)
@dp.message(TicketForm.cabinetName)
async def cabinetName(message: types.Message, state: FSMContext):
await state.update_data(cabinet_name=message.text)
await message.answer("Опишите проблему")
await state.set_state(TicketForm.description)
@dp.message(TicketForm.description)
async def generateTicket(message: types.Message, state: FSMContext):
ticketID = bson.ObjectId()
await message.answer(f"✅ Заявка с номером {str(ticketID)[-4:]} принята.")
userData = await state.get_data()
ticketText = f"Кабинет: {userData['cabinet_name']}\nОписание: {message.text}"
inlineKeyboard = InlineKeyboardMarkup(inline_keyboard=[
[InlineKeyboardButton(text="❌ Закрыть заявку", callback_data=f"close_ticket:{ticketID}")]
])
sentMessage = await bot.send_message(chat_id=config.GROUP_CHAT_ID,
text="Поступила новая заявка!\n\n"
f"Номер заявки: {str(ticketID)[-4:]}\n"
f"Кабинет\n"
f"\t\t\t{userData['cabinet_name']}\n"
f"Описание заявки\n"
f"\t\t\t{message.text}",
reply_markup=inlineKeyboard)
await bot.pin_chat_message(chat_id=config.GROUP_CHAT_ID,
message_id=sentMessage.message_id)
await bot.delete_message(chat_id=config.GROUP_CHAT_ID, message_id=int(sentMessage.message_id) + 1)
await createTicket(ticketText=ticketText,
userID=message.from_user.id,
ticketID=ticketID,
ticketMessageID=sentMessage.message_id)
await state.clear()
@dp.callback_query(F.data.startswith("close_ticket"))
async def closeTicketCallback(callBackQuery: types.CallbackQuery):
ticketID = callBackQuery.data.split(":")[1]
ticket = await getTicketByID(ticketID)
messageID = ticket["TicketMessageID"]
user = await getUser(callBackQuery.from_user.id)
if user is None:
await initUser(callBackQuery.from_user.id)
await closeTicket(callBackQuery.from_user.id, ticketID)
await updateTicketStatus(ticketID, status=0)
await bot.unpin_chat_message(chat_id=config.GROUP_CHAT_ID,
message_id=messageID)
await callBackQuery.answer("Заявка закрыта.")
ratingKeyboard = InlineKeyboardMarkup(inline_keyboard=[
[InlineKeyboardButton(text="⭐️ " + str(i),
callback_data=f"rate:{i}:{ticketID}") for i in range(1, 6)],
[InlineKeyboardButton(text="❌ Отказаться от оценки ",
callback_data="rate:skip")]
])
await bot.send_message(chat_id=ticket['TelegramUserID'],
text=f"Ваша заявка c номером <code>{ticketID[-4:]}</code> "
f"от {datetime.strftime(ticket['Date'], '%d.%m.%Y %H:%M')}\n"
f"\n"
f"{ticket["TicketText"]}\n\n"
f"❌ <b>Закрыта</b>",
parse_mode="HTML")
await bot.delete_message(chat_id=config.GROUP_CHAT_ID,
message_id=messageID)
await bot.send_message(chat_id=ticket['TelegramUserID'],
text="Оцените качество работы от 1⭐️ до 5⭐️",
reply_markup=ratingKeyboard)
@dp.callback_query(F.data.startswith("rate"))
async def rateTicket(callback_query: types.CallbackQuery):
callback = callback_query.data.split(":")
await bot.edit_message_text(chat_id=callback_query.message.chat.id,
message_id=callback_query.message.message_id,
text=f"Вы поставили оценку <b>{callback[1]}</b>⭐️.",
parse_mode='HTML')
if callback[1] != 'skip':
await setTicketRating(callback[2], int(callback[1]))
await callback_query.answer(f"Вы поставили оценку <b>{callback[1]}</b>⭐️.")
await bot.send_message(chat_id=config.GROUP_CHAT_ID,
text=f"Пользователь поставил оценку {callback[1]}⭐️"
f" в заявке <code>{callback[2][-4:]}</code>.",
parse_mode='HTML')
else:
await bot.send_message(chat_id=config.GROUP_CHAT_ID,
text=f"Пользователь отказался от оценки {callback[1]}⭐️"
f" в заявке <code>{callback[2][-4:]}</code>.",
parse_mode='HTML')
await callback_query.answer("Вы отказались от оценки.")
if __name__ == "__main__":
dp.run_polling(bot)