forked from AmirBasuti/jozvekhubot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
116 lines (84 loc) · 4.16 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
from aiogram import Bot, Dispatcher, executor, types
from aiogram.types import ReplyKeyboardRemove
import datetime
from dotenv import load_dotenv
from text import start_text, help_text, get_text, supoort_text, error_text
from keys import (
person_keyboard,
course_keyboard,
valid_person,
valid_course,
bye_me_coffee,
)
from user_manegment import save_user_data, user_exists
import os
load_dotenv()
TOKEN = os.getenv("TELEGRAM_TOKEN")
# Initialize the bot and dispatcher
bot = Bot(TOKEN)
dp = Dispatcher(bot)
async def send_document_to_user(message, person, tmp_course):
try:
wait_message = await message.reply(text="لطفا کمی صبر کنید", reply_markup=ReplyKeyboardRemove())
file_path = f"jozve/{valid_person[person]}/{valid_course[tmp_course]}.pdf"
with open(file_path, "rb") as file:
await message.reply_document(document=file, caption=f"جزوه {tmp_course} توسط {person}", reply_markup=bye_me_coffee)
await message.answer(text="شما میتوانید برای حمایت از کسی که جزوه را تهیه کرده است, قهوه بخرید D:", reply_markup=ReplyKeyboardRemove())
await bot.delete_message(message.chat.id, wait_message.message_id)
except:
await message.answer(text="/support جزوه مورد نظر یافت نشد. با سازنده در ارتباط باشید", reply_markup=ReplyKeyboardRemove())
async def send_welcome_message(message):
if not user_exists(message.from_user.id):
save_user_data(
message.from_user.id,
message.from_user.username,
message.from_user.first_name,
message.from_user.last_name,
)
await message.answer(text=start_text, reply_markup=ReplyKeyboardRemove())
async def send_help_message(message):
await message.answer(text=help_text, reply_markup=ReplyKeyboardRemove())
async def send_get_message(message):
await message.answer(text=get_text, reply_markup=course_keyboard)
async def send_support_message(message):
await message.answer(text=supoort_text, reply_markup=ReplyKeyboardRemove())
@dp.message_handler(commands=['start'])
async def start_command(message: types.Message):
await send_welcome_message(message)
@dp.message_handler(commands=['help'])
async def help_command(message: types.Message):
await send_help_message(message)
@dp.message_handler(commands=['get'])
async def get_command(message: types.Message):
await send_get_message(message)
@dp.message_handler(commands=['support'])
async def support_command(message: types.Message):
await send_support_message(message)
@dp.message_handler(lambda message: message.text in valid_course.keys())
async def course_handler(message: types.Message):
global selection_course
global course_input
selection_course = message.text
course_input = True
await message.answer(text="فرد مورد نظر خود را انتخاب کنید", reply_markup=person_keyboard)
@dp.message_handler(lambda message: message.text in valid_person.keys())
async def person_handler(message: types.Message):
global course_input, selection_course
if course_input and message.text != "برکشت":
tmp_course = selection_course
course_input = False
person = message.text
await send_document_to_user(message, person, tmp_course)
elif message.text == "برکشت":
await message.answer(text="درس مورد نظر خود را انتخاب کنید", reply_markup=course_keyboard)
else:
await message.answer(text="ابتدا درس مورد نظر خود را انتخاب کنید", reply_markup=course_keyboard)
@dp.message_handler()
async def handle_other_messages(message: types.Message):
await message.answer(text=error_text, reply_markup=ReplyKeyboardRemove())
async def start(_):
print(datetime.datetime.now(), "Bot Started")
async def shutdown(_):
print(datetime.datetime.now(), "Bot Stopped")
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True ,on_startup=start , on_shutdown=shutdown )