-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
182 lines (150 loc) · 6.42 KB
/
app.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import telebot
import schedule
import time
import json
import random
from threading import Thread
# Load your bot token
TOKEN = 'your-bot-token'
bot = telebot.TeleBot(TOKEN)
# Load data
with open('duaa.json', 'r', encoding='utf-8') as f:
duaa_data = json.load(f)
with open('adkar.json', 'r', encoding='utf-8') as f:
adkar_data = json.load(f)
with open('quran.json', 'r', encoding='utf-8') as f:
quran_data = json.load(f)
# User preferences and scheduled jobs
user_preferences = {}
scheduled_jobs = {}
# Helper functions
def get_detailed_duaa():
duaa = random.choice(duaa_data)
content = f"الدعاء:\n"
for d in duaa['duaa']:
content += f"{d}\n"
content += f"\nالفئة: {duaa['category']}\n"
if duaa['source']['quran']:
content += "\nمصدر القرآن:\n"
for quran_source in duaa['source']['quran']:
surah = quran_source['surah']
ayah = quran_source['ayah']
content += f"سورة {surah['name']} (رقم {surah['number']}), "
if ayah['from'] == ayah['to']:
content += f"الآية {ayah['from']}\n"
else:
content += f"الآيات {ayah['from']}-{ayah['to']}\n"
if duaa['source']['hadith']:
content += "\nمصدر الحديث:\n"
for hadith in duaa['source']['hadith'][0]:
content += f"الكتاب: {hadith['book']}\n"
content += f"الرقم: {hadith['numberOrPage']}\n"
content += f"الراوي: {hadith['rawi']}\n"
if hadith['grade']:
content += f"الدرجة: {hadith['grade']}\n"
return content
def get_detailed_adkar():
category = random.choice(list(adkar_data.keys()))
thikr = random.choice(adkar_data[category])
content = f"الذكر: {thikr['content']}\n\n"
content += f"الفئة: {category}\n"
if thikr['count'] != "01":
content += f"عدد المرات: {thikr['count']}\n"
if thikr['description']:
content += f"الوصف: {thikr['description']}\n"
if thikr['reference']:
content += f"المرجع: {thikr['reference']}\n"
return content
def get_detailed_quran():
verse = random.choice(quran_data)
content = f"سورة {verse['sura_name_ar']} ({verse['sura_name_en']}) - رقم السورة: {verse['sura_no']}\n"
content += f"الآية {verse['aya_no']}:\n\n"
content += f"{verse['aya_text_emlaey']}\n\n"
content += f"الجزء: {verse['jozz']}\n"
content += f"الصفحة: {verse['page']}\n"
content += f"السطر: {verse['line_start']}"
if verse['line_start'] != verse['line_end']:
content += f" - {verse['line_end']}"
return content
def send_message(chat_id, message_type):
if message_type == 'duaa':
bot.send_message(chat_id, get_detailed_duaa())
elif message_type == 'adkar':
bot.send_message(chat_id, get_detailed_adkar())
elif message_type == 'quran':
bot.send_message(chat_id, get_detailed_quran())
# Command handlers
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.reply_to(message, "مرحبا بك! استخدم الأوامر /duaa أو /adkar أو /quran لضبط تفضيلاتك.")
@bot.message_handler(commands=['duaa', 'adkar', 'quran'])
def set_preferences(message):
chat_id = message.chat.id
command = message.text.split()[0][1:] # Remove the '/' from the command
markup = telebot.types.ReplyKeyboardMarkup(row_width=2)
markup.add('30 دقيقة', 'ساعة', 'ساعتين', 'مرة في اليوم', 'عشوائي', 'مرتب', 'إيقاف')
bot.send_message(chat_id, f"اختر تفضيلاتك لـ {get_arabic_name(command)}:", reply_markup=markup)
bot.register_next_step_handler(message, process_preference, command)
def get_arabic_name(command):
if command == 'duaa':
return 'الأدعية'
elif command == 'adkar':
return 'الأذكار'
elif command == 'quran':
return 'القرآن'
def process_preference(message, command):
chat_id = message.chat.id
preference = message.text
if preference == 'إيقاف':
stop_notifications(chat_id, command)
bot.send_message(chat_id, f"تم إيقاف إشعارات {get_arabic_name(command)}.")
return
if chat_id not in user_preferences:
user_preferences[chat_id] = {}
user_preferences[chat_id][command] = {
'frequency': get_frequency(preference),
'order': 'random' if preference == 'عشوائي' else 'ordered'
}
schedule_messages(chat_id, command)
bot.send_message(chat_id, f"تم ضبط تفضيلاتك لـ {get_arabic_name(command)}!")
def get_frequency(preference):
if preference == '30 دقيقة':
return '30 minutes'
elif preference == 'ساعة':
return '1 hour'
elif preference == 'ساعتين':
return '2 hours'
else:
return 'once a day'
def schedule_messages(chat_id, message_type):
pref = user_preferences[chat_id][message_type]
# Cancel any existing job for this chat_id and message_type
if chat_id in scheduled_jobs and message_type in scheduled_jobs[chat_id]:
schedule.cancel_job(scheduled_jobs[chat_id][message_type])
if pref['frequency'] == '30 minutes':
job = schedule.every(30).minutes.do(send_message, chat_id, message_type)
elif pref['frequency'] == '1 hour':
job = schedule.every(1).hours.do(send_message, chat_id, message_type)
elif pref['frequency'] == '2 hours':
job = schedule.every(2).hours.do(send_message, chat_id, message_type)
else: # once a day
job = schedule.every().day.at("12:00").do(send_message, chat_id, message_type)
if chat_id not in scheduled_jobs:
scheduled_jobs[chat_id] = {}
scheduled_jobs[chat_id][message_type] = job
def stop_notifications(chat_id, message_type):
if chat_id in user_preferences and message_type in user_preferences[chat_id]:
del user_preferences[chat_id][message_type]
if chat_id in scheduled_jobs and message_type in scheduled_jobs[chat_id]:
schedule.cancel_job(scheduled_jobs[chat_id][message_type])
del scheduled_jobs[chat_id][message_type]
# Function to run scheduled tasks
def run_scheduler():
while True:
schedule.run_pending()
time.sleep(1)
# Start the scheduler in a separate thread
scheduler_thread = Thread(target=run_scheduler)
scheduler_thread.start()
# Start the bot
bot.polling()