-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_unified.py
More file actions
1455 lines (1226 loc) · 62 KB
/
app_unified.py
File metadata and controls
1455 lines (1226 loc) · 62 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import asyncio
import threading
from flask import Flask, request, jsonify, render_template
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
ApplicationBuilder,
CommandHandler,
CallbackQueryHandler,
ConversationHandler,
MessageHandler,
ContextTypes,
filters
)
import logging
import json
from datetime import datetime, time, timedelta
from typing import Dict, List
import random
from gtts import gTTS
import openai # Required for AI chat feature
# Configure logging for both Flask and Telegram Bot
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# --- Content from config.py ---
TOKEN = "8085016643:AAEHAO1BlQzhdo39N7MSkx0NEZK3P0d5M58" # ضع هنا التوكن الخاص بك
ADMIN_USER_IDS = [953696547, 7942066919] # ضع هنا معرفات المستخدمين المشرفين
# Data file (from main.py)
DB = "data.json"
if os.path.exists(DB):
with open(DB, encoding='utf-8') as f:
data = json.load(f)
else:
keys = [
"HSK1","HSK2","HSK3","HSK4","HSK5","HSK6",
"Quran","Dictionary","Stories","Gramnalessons","GrammarReview",
"Dialogues","Flashcards","Quizzes","PictureDictionary","GrammarTerms","Proverbs",
"Applications"
]
data = {k: [] for k in keys}
with open(DB, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
def save():
with open(DB, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
def is_admin(user_id):
return user_id in ADMIN_USER_IDS
# Conversation states (from main.py)
ADMIN_SECTION, ADMIN_TITLE, ADMIN_CONTENT, UPLOAD_FILE = range(4)
# --- Content from ai_chat_feature.py ---
"""
ميزة المحادثة الذكية بالذكاء الاصطناعي
AI Chat Feature - أولوية عالية للتنفيذ
"""
SYSTEM_PROMPTS = {
"teacher": """أنت معلم لغة صينية محترف وصبور. \n - ساعد المتعلم على تحسين لغته الصينية\n - صحح الأخطاء بلطف مع شرح السبب\n - قدم أمثلة عملية\n - استخدم العربية للشرح والصينية للأمثلة\n - كن مشجعاً ومحفزاً""",
"conversation": """أنت صديق صيني يتحدث الصينية المبسطة.\n - تحدث بالصينية بشكل طبيعي\n - استخدم جمل بسيطة ومفهومة\n - أضف الترجمة العربية بين قوسين عند الحاجة\n - تحدث عن مواضيع يومية ممتعة""",
"translator": """أنت مترجم محترف بين العربية والصينية.\n - ترجم بدقة وبشكل طبيعي\n - قدم ترجمات بديلة إن وجدت\n - اشرح السياق الثقافي عند الحاجة\n - قدم النطق بالبينيين (Pinyin)""",
"academic_advisor": """أنت مستشار أكاديمي خبير في نظام التعليم السعودي (الابتدائي، المتوسط، الثانوي، الجامعي). مهمتك هي تقديم إجابات دقيقة ومفصلة ونصائح تحفيزية للطلاب حول مساراتهم التعليمية، وأفضل طرق الاستعداد لاختبارات القدرات والتحصيلي، وكيفية اختيار التخصصات الجامعية بما يتوافق مع رؤية 2030. استخدم لغة عربية فصحى ومحفزة."""
}
async def ai_chat_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Start AI chat conversation"""
keyboard = [
[
InlineKeyboardButton("🎓 معلم", callback_data="ai_mode_teacher"),
InlineKeyboardButton("💬 محادثة", callback_data="ai_mode_conversation")
],
[
InlineKeyboardButton("🔤 مترجم", callback_data="ai_mode_translator"),
InlineKeyboardButton("🧑🏫 مرشد أكاديمي", callback_data="ai_mode_academic_advisor")
],
[
InlineKeyboardButton("❌ إلغاء", callback_data="ai_cancel")
]
]
await update.message.reply_text(
"🤖 **مرحباً بك في المحادثة الذكية!**\n\n"
"اختر وضع المحادثة:\n\n"
"🎓 **معلم**: للتعلم والتصحيح\n"
"💬 **محادثة**: للممارسة الطبيعية\n"
"🔤 **مترجم**: للترجمة الفورية",
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode='Markdown'
)
async def ai_mode_select(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Select AI chat mode"""
query = update.callback_query
await query.answer()
mode = query.data.split("_")[-1]
if mode == "cancel":
await query.edit_message_text("تم إلغاء المحادثة الذكية.")
return
# Save mode in user context
context.user_data["ai_mode"] = mode
context.user_data["ai_history"] = []
mode_names = {
"teacher": "🎓 وضع المعلم",
"conversation": "💬 وضع المحادثة",
"translator": "🔤 وضع المترجم",
"academic_advisor": "🧑🏫 وضع المرشد الأكاديمي"
}
await query.edit_message_text(
f"✅ تم اختيار {mode_names[mode]}\n\n"
"ابدأ بإرسال رسالتك الآن!\n"
"استخدم /stop_ai لإنهاء المحادثة."
)
async def ai_chat_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle AI chat messages"""
# Check if AI mode is active
if "ai_mode" not in context.user_data:
return
user_message = update.message.text
mode = context.user_data["ai_mode"]
history = context.user_data.get("ai_history", [])
# Send typing indicator
await update.message.chat.send_action("typing")
try:
# Prepare messages
messages = [
{"role": "system", "content": SYSTEM_PROMPTS[mode]}
]
# Add conversation history (last 10 messages)
messages.extend(history[-10:])
# Add current message
messages.append({"role": "user", "content": user_message})
# Update academic advisor usage count
if mode == "academic_advisor":
user_id = update.message.from_user.id
achievement_system = AchievementSystem(user_id)
achievement_system.increment_stat("academic_advisor_uses")
# Call Groq API (compatible with OpenAI API)
openai.api_key = os.environ.get("GROQ_API_KEY", "") # Ensure GROQ_API_KEY is set in Render environment variables
openai.api_base = "https://api.groq.com/openai/v1"
response = openai.ChatCompletion.create(
model="llama-3.3-70b-versatile", # Groq's free model
messages=messages,
max_tokens=500,
temperature=0.7
)
ai_response = response.choices[0].message.content
# Update history
history.append({"role": "user", "content": user_message})
history.append({"role": "assistant", "content": ai_response})
context.user_data["ai_history"] = history
# Send response
await update.message.reply_text(ai_response, parse_mode='Markdown')
except Exception as e:
await update.message.reply_text(
f"⚠️ حدث خطأ في المحادثة: {str(e)}\n"
"يرجى المحاولة مرة أخرى."
)
async def ai_chat_stop(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Stop AI chat"""
if "ai_mode" in context.user_data:
messages_count = len(context.user_data.get("ai_history", [])) // 2
# Clear AI data
context.user_data.pop("ai_mode", None)
context.user_data.pop("ai_history", None)
await update.message.reply_text(
f"✅ تم إنهاء المحادثة الذكية.\n\n"
f"📊 عدد الرسائل: {messages_count}\n\n"
"استخدم /ai_chat للبدء من جديد."
)
else:
await update.message.reply_text("لا توجد محادثة نشطة حالياً.")
async def ai_chat_stats(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Show AI chat statistics"""
if "ai_mode" not in context.user_data:
await update.message.reply_text("لا توجد محادثة نشطة حالياً.")
return
mode = context.user_data["ai_mode"]
history = context.user_data.get("ai_history", [])
messages_count = len(history) // 2
mode_names = {
"teacher": "🎓 وضع المعلم",
"conversation": "💬 وضع المحادثة",
"translator": "🔤 وضع المترجم"
}
await update.message.reply_text(
f"📊 **إحصائيات المحادثة الحالية**\n\n"
f"الوضع: {mode_names[mode]}\n"
f"عدد الرسائل: {messages_count}\n"
f"سجل المحادثة: {len(history)} رسالة\n\n"
"استخدم /stop_ai لإنهاء المحادثة."
)
# --- Content from achievements_system.py ---
"""
نظام الإنجازات والشارات
Achievements & Badges System
"""
# Achievement definitions
ACHIEVEMENTS = {
"first_steps": {"id": "first_steps", "name": "الخطوات الأولى", "name_en": "First Steps", "description": "أكمل أول درس", "icon": "👶", "points": 10, "condition": {"type": "lessons_completed", "value": 1}},
"word_collector": {"id": "word_collector", "name": "جامع الكلمات", "name_en": "Word Collector", "description": "تعلم 50 كلمة جديدة", "icon": "📚", "points": 50, "condition": {"type": "words_learned", "value": 50}},
"consistent_learner": {"id": "consistent_learner", "name": "المتعلم المثابر", "name_en": "Consistent Learner", "description": "سلسلة 7 أيام متتالية", "icon": "🔥", "points": 100, "condition": {"type": "streak_days", "value": 7}},
"month_warrior": {"id": "month_warrior", "name": "محارب الشهر", "name_en": "Month Warrior", "description": "سلسلة 30 يوم متتالية", "icon": "⚡", "points": 500, "condition": {"type": "streak_days", "value": 30}},
"quiz_master": {"id": "quiz_master", "name": "سيد الاختبارات", "name_en": "Quiz Master", "description": "احصل على 100% في 10 اختبارات", "icon": "🎯", "points": 200, "condition": {"type": "perfect_quizzes", "value": 10}},
"bookworm": {"id": "bookworm", "name": "دودة الكتب", "name_en": "Bookworm", "description": "اقرأ 50 قصة", "icon": "📖", "points": 150, "condition": {"type": "stories_read", "value": 50}},
"hsk1_master": {"id": "hsk1_master", "name": "خبير HSK1", "name_en": "HSK1 Master", "description": "أكمل جميع دروس HSK1", "icon": "🥉", "points": 300, "condition": {"type": "hsk_level_completed", "value": 1}},
"hsk6_master": {"id": "hsk6_master", "name": "خبير HSK6", "name_en": "HSK6 Master", "description": "أكمل جميع دروس HSK6", "icon": "🏆", "points": 2000, "condition": {"type": "hsk_level_completed", "value": 6}},
"saudi_vision_2030": {"id": "saudi_vision_2030", "name": "نجم الرؤية 2030", "name_en": "Vision 2030 Star", "description": "استخدم المرشد الأكاديمي 10 مرات", "icon": "🇸🇦", "points": 500, "condition": {"type": "academic_advisor_uses", "value": 10}},
"qiyas_pro": {"id": "qiyas_pro", "name": "متقن القياس", "name_en": "Qiyas Pro", "description": "أكمل 5 اختبارات قدرات/تحصيلي (وهمية)", "icon": "📝", "points": 750, "condition": {"type": "qiyas_quizzes_completed", "value": 5}},
"dedicated_student": {"id": "dedicated_student", "name": "الطالب المجتهد", "name_en": "Dedicated Student", "description": "أمضِ 50 ساعة في التعلم", "icon": "⏰", "points": 400, "condition": {"type": "study_hours", "value": 50}},
"helpful_friend": {"id": "helpful_friend", "name": "الصديق المساعد", "name_en": "Helpful Friend", "description": "ساعد 10 متعلمين آخرين", "icon": "🤝", "points": 250, "condition": {"type": "helped_users", "value": 10}},
"early_bird": {"id": "early_bird", "name": "الطائر المبكر", "name_en": "Early Bird", "description": "تعلم قبل الساعة 7 صباحاً 10 مرات", "icon": "🌅", "points": 100, "condition": {"type": "early_sessions", "value": 10}},
"night_owl": {"id": "night_owl", "name": "بومة الليل", "name_en": "Night Owl", "description": "تعلم بعد الساعة 11 مساءً 10 مرات", "icon": "🦉", "points": 100, "condition": {"type": "late_sessions", "value": 10}}
}
class AchievementSystem:
"""Achievement tracking and management system"""
def __init__(self, user_id: int):
self.user_id = user_id
self.user_data = self.load_user_data()
def load_user_data(self) -> Dict:
"""Load user achievement data"""
try:
with open(f"user_achievements_{self.user_id}.json", 'r', encoding='utf-8') as f:
return json.load(f)
except FileNotFoundError:
return {
"unlocked_achievements": [],
"progress": {},
"total_points": 0,
"stats": {
"lessons_completed": 0,
"words_learned": 0,
"streak_days": 0,
"perfect_quizzes": 0,
"stories_read": 0,
"study_hours": 0,
"helped_users": 0,
"early_sessions": 0,
"late_sessions": 0,
"hsk_levels_completed": []
}
}
def save_user_data(self):
"""Save user achievement data"""
with open(f"user_achievements_{self.user_id}.json", 'w', encoding='utf-8') as f:
json.dump(self.user_data, f, ensure_ascii=False, indent=2)
def update_stat(self, stat_name: str, value: int = 1):
"""Update a user statistic"""
if stat_name in self.user_data["stats"]:
if isinstance(self.user_data["stats"][stat_name], list):
if value not in self.user_data["stats"][stat_name]:
self.user_data["stats"][stat_name].append(value)
else:
self.user_data["stats"][stat_name] += value
self.save_user_data()
return self.check_achievements()
return []
def check_achievements(self) -> List[Dict]:
"""Check for newly unlocked achievements"""
newly_unlocked = []
for achievement_id, achievement in ACHIEVEMENTS.items():
# Skip if already unlocked
if achievement_id in self.user_data["unlocked_achievements"]:
continue
# Check condition
condition = achievement["condition"]
stat_value = self.user_data["stats"].get(condition["type"], 0)
# Handle different condition types
if condition["type"] == "hsk_level_completed":
if condition["value"] in stat_value:
newly_unlocked.append(achievement)
self.unlock_achievement(achievement_id)
else:
if isinstance(stat_value, (int, float)) and stat_value >= condition["value"]:
newly_unlocked.append(achievement)
self.unlock_achievement(achievement_id)
return newly_unlocked
def unlock_achievement(self, achievement_id: str):
"""Unlock an achievement"""
if achievement_id not in self.user_data["unlocked_achievements"]:
self.user_data["unlocked_achievements"].append(achievement_id)
achievement = ACHIEVEMENTS[achievement_id]
self.user_data["total_points"] += achievement["points"]
self.save_user_data()
def get_user_level(self) -> Dict:
"""Calculate user level based on points"""
points = self.user_data["total_points"]
# Level thresholds
levels = [
(0, "مبتدئ", "Beginner", "🌱"),
(100, "متعلم", "Learner", "🌿"),
(500, "متقدم", "Advanced", "🌳"),
(1000, "خبير", "Expert", "⭐"),
(2000, "محترف", "Professional", "💎"),
(5000, "أسطورة", "Legend", "👑")
]
for i, (threshold, name_ar, name_en, icon) in enumerate(levels):
if i == len(levels) - 1 or points < levels[i + 1][0]:
next_threshold = levels[i + 1][0] if i < len(levels) - 1 else None
return {
"level": i + 1,
"name_ar": name_ar,
"name_en": name_en,
"icon": icon,
"points": points,
"next_level_points": next_threshold,
"progress": ((points - threshold) / (next_threshold - threshold) * 100) if next_threshold else 100
}
return levels[0]
def get_achievement_summary(self) -> str:
"""Get formatted achievement summary"""
level_info = self.get_user_level()
unlocked_count = len(self.user_data["unlocked_achievements"])
total_count = len(ACHIEVEMENTS)
summary = f"""
🏆 **ملخص الإنجازات**
📊 المستوى: {level_info["icon"]} {level_info["name_ar"]} (المستوى {level_info["level"]})
💎 النقاط: {level_info["points"]}
🎯 التقدم للمستوى التالي: {level_info["progress"]:.1f}%
🏅 الإنجازات: {unlocked_count}/{total_count}
📈 **الإحصائيات:**
• دروس مكتملة: {self.user_data["stats"]["lessons_completed"]}
• كلمات متعلمة: {self.user_data["stats"]["words_learned"]}
• سلسلة الأيام: {self.user_data["stats"]["streak_days"]}
• اختبارات كاملة: {self.user_data["stats"]["perfect_quizzes"]}
• قصص مقروءة: {self.user_data["stats"]["stories_read"]}
• ساعات الدراسة: {self.user_data["stats"]["study_hours"]:.1f}
"""
return summary
def get_unlocked_achievements(self) -> List[Dict]:
"""Get list of unlocked achievements"""
return [
ACHIEVEMENTS[aid]
for aid in self.user_data["unlocked_achievements"]
]
def get_locked_achievements(self) -> List[Dict]:
"""Get list of locked achievements with progress"""
locked = []
for achievement_id, achievement in ACHIEVEMENTS.items():
if achievement_id not in self.user_data["unlocked_achievements"]:
condition = achievement["condition"]
current = self.user_data["stats"].get(condition["type"], 0)
if condition["type"] == "hsk_level_completed":
progress = condition["value"] in current
else:
progress = (current / condition["value"] * 100) if condition["value"] > 0 else 0
achievement_copy = achievement.copy()
achievement_copy["progress"] = progress
achievement_copy["current"] = current
achievement_copy["target"] = condition["value"]
locked.append(achievement_copy)
return sorted(locked, key=lambda x: x["progress"], reverse=True)
def format_achievement_notification(achievement: Dict) -> str:
"""Format achievement unlock notification"""
return f"""
🎉 **إنجاز جديد مفتوح!**
{achievement["icon"]} **{achievement["name"]}**
{achievement["name_en"]}
{achievement["description"]}
💎 +{achievement["points"]} نقطة
"""
# --- Achievement Handlers (moved outside class for direct use) ---
async def show_achievements(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
if query:
await query.answer()
user_id = query.from_user.id
else:
user_id = update.effective_user.id
ach_system = AchievementSystem(user_id)
summary = ach_system.get_achievement_summary()
keyboard = [
[InlineKeyboardButton("✅ إنجازات مفتوحة", callback_data="ACH_UNLOCKED")],
[InlineKeyboardButton("🔒 إنجازات مغلقة", callback_data="ACH_LOCKED")],
[InlineKeyboardButton("◀️ رجوع", callback_data="BACK")]
]
reply_markup = InlineKeyboardMarkup(keyboard)
if query:
await query.edit_message_text(summary, reply_markup=reply_markup, parse_mode='Markdown')
else:
await update.message.reply_text(summary, reply_markup=reply_markup, parse_mode='Markdown')
async def show_unlocked_achievements(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
user_id = query.from_user.id
ach_system = AchievementSystem(user_id)
unlocked = ach_system.get_unlocked_achievements()
if not unlocked:
await query.edit_message_text("لا توجد إنجازات مفتوحة بعد.", reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("◀️ رجوع", callback_data="MENU_ACHIEVEMENTS")]]))
return
text = "✅ **إنجازاتك المفتوحة:**\n\n"
for ach in unlocked:
text += f"{ach["icon"]} **{ach["name"]}** - {ach["description"]}\n"
await query.edit_message_text(text, reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("◀️ رجوع", callback_data="MENU_ACHIEVEMENTS")]]), parse_mode='Markdown')
async def show_locked_achievements(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
user_id = query.from_user.id
ach_system = AchievementSystem(user_id)
locked = ach_system.get_locked_achievements()
if not locked:
await query.edit_message_text("لقد فتحت جميع الإنجازات! رائع!", reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("◀️ رجوع", callback_data="MENU_ACHIEVEMENTS")]]))
return
text = "🔒 **إنجازاتك المغلقة:**\n\n"
for ach in locked:
progress_bar = "⬜" * (ach["progress"] // 10) + "⬛" * (10 - (ach["progress"] // 10))
text += f"{ach["icon"]} **{ach["name"]}**\n"
text += f" {ach["description"]}\n"
if isinstance(ach["current"], list):
text += f" التقدم: {len(ach["current"])} / {ach["target"]}\n\n"
else:
text += f" التقدم: {ach["current"]}/{ach["target"]} ({ach["progress"]:.1f}%)\n\n"
await query.edit_message_text(text, reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("◀️ رجوع", callback_data="MENU_ACHIEVEMENTS")]]), parse_mode='Markdown')
# --- Content from text_to_speech_feature.py ---
async def text_to_speech_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Start text-to-speech feature"""
keyboard = [
[InlineKeyboardButton("◀️ رجوع", callback_data="BACK")]
]
await update.message.reply_text(
"""🔊 **ميزة النطق الصوتي!**\n\n"
"أرسل لي أي نص صيني وسأقوم بنطقه لك.\n"
"استخدم /stop_tts لإنهاء هذه الميزة.""",
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode='Markdown'
)
context.user_data["tts_active"] = True
async def text_to_speech_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle text messages for TTS"""
if not context.user_data.get("tts_active", False):
return
text = update.message.text
if not text:
return
await update.message.chat.send_action("record_audio")
try:
tts = gTTS(text=text, lang='zh-CN')
audio_path = f"temp_audio_{update.effective_user.id}.mp3"
tts.save(audio_path)
with open(audio_path, 'rb') as audio_file:
await update.message.reply_audio(audio=audio_file)
os.remove(audio_path)
except Exception as e:
await update.message.reply_text(f"⚠️ حدث خطأ أثناء تحويل النص إلى صوت: {str(e)}")
async def text_to_speech_stop(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Stop text-to-speech feature"""
if context.user_data.get("tts_active", False):
context.user_data["tts_active"] = False
await update.message.reply_text("✅ تم إنهاء ميزة النطق الصوتي. استخدم /tts للبدء من جديد.")
else:
await update.message.reply_text("لا توجد ميزة نطق صوتي نشطة حالياً.")
# --- Content from daily_reminders_feature.py ---
REMINDERS_DB = "user_reminders.json"
def load_reminders():
if os.path.exists(REMINDERS_DB):
with open(REMINDERS_DB, "r", encoding="utf-8") as f:
return json.load(f)
return {}
def save_reminders(reminders):
with open(REMINDERS_DB, "w", encoding="utf-8") as f:
json.dump(reminders, f, ensure_ascii=False, indent=2)
async def send_daily_reminder_message(context: ContextTypes.DEFAULT_TYPE):
job_data = context.job.data
user_id = job_data["user_id"]
chat_id = job_data["chat_id"]
message = (
"""🔔 **تذكيرك اليومي لتعلم الصينية!**\n\n"
"حان وقت الدراسة! إليك كلمة اليوم:\n"
"**你好 (Nǐ hǎo) - مرحباً**\n\n"
"استمر في التقدم! 🚀"""
)
await context.bot.send_message(chat_id=chat_id, text=message, parse_mode='Markdown')
async def start_reminders_setup(update: Update, context: ContextTypes.DEFAULT_TYPE):
keyboard = [
[InlineKeyboardButton("⏰ 09:00 صباحاً", callback_data="set_reminder_0900")],
[InlineKeyboardButton("⏰ 13:00 ظهراً", callback_data="set_reminder_1300")],
[InlineKeyboardButton("⏰ 20:00 مساءً", callback_data="set_reminder_2000")],
[InlineKeyboardButton("❌ إلغاء التذكير", callback_data="cancel_reminder")],
[InlineKeyboardButton("◀️ رجوع", callback_data="BACK")]
]
await update.message.reply_text(
"""🔔 **تذكيرات يومية ذكية!**\n\n"
"اختر الوقت الذي تفضل أن أذكرك فيه بالدراسة:\n"
"(يمكنك إلغاء التذكير في أي وقت)""",
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode='Markdown'
)
async def set_daily_reminder(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
user_id = query.from_user.id
chat_id = query.message.chat_id
if query.data == "cancel_reminder":
reminders = load_reminders()
if str(user_id) in reminders:
if reminders[str(user_id)].get("job_name"):
current_jobs = context.job_queue.get_jobs_by_name(reminders[str(user_id)]["job_name"])
for job in current_jobs:
job.schedule_removal()
reminders.pop(str(user_id))
save_reminders(reminders)
await query.edit_message_text("✅ تم إلغاء التذكير اليومي بنجاح.")
else:
await query.edit_message_text("لا يوجد تذكير يومي نشط لإلغائه.")
return
selected_time_str = query.data.split("_")[-1]
hour = int(selected_time_str[:2])
minute = int(selected_time_str[2:])
reminder_time = time(hour, minute)
job_name = f"daily_reminder_{user_id}"
# Remove existing job if any
current_jobs = context.job_queue.get_jobs_by_name(job_name)
for job in current_jobs:
job.schedule_removal()
# Schedule new job
context.job_queue.run_daily(
send_daily_reminder_message,
reminder_time,
days=(0, 1, 2, 3, 4, 5, 6), # Every day
chat_id=chat_id,
name=job_name,
data={
"user_id": user_id,
"chat_id": chat_id,
"time": selected_time_str
}
)
reminders = load_reminders()
reminders[str(user_id)] = {"time": selected_time_str, "chat_id": chat_id, "job_name": job_name}
save_reminders(reminders)
await query.edit_message_text(
f"✅ تم تعيين تذكير يومي في الساعة {hour:02d}:{minute:02d}.\n"
"سأرسل لك كلمة اليوم أو تحديًا بسيطًا."
)
async def re_schedule_all_reminders(application):
reminders = load_reminders()
for user_id_str, reminder_info in reminders.items():
user_id = int(user_id_str)
chat_id = reminder_info["chat_id"]
selected_time_str = reminder_info["time"]
job_name = reminder_info["job_name"]
hour = int(selected_time_str[:2])
minute = int(selected_time_str[2:])
reminder_time = time(hour, minute)
application.job_queue.run_daily(
send_daily_reminder_message,
reminder_time,
days=(0, 1, 2, 3, 4, 5, 6),
chat_id=chat_id,
name=job_name,
data={
"user_id": user_id,
"chat_id": chat_id,
"time": selected_time_str
}
)
print(f"Rescheduled reminder for user {user_id} at {reminder_time}")
# --- Content from word_matching_game.py ---
SELECTING_ANSWER = 0 # Conversation state
GAME_WORDS = [
{"chinese": "你好", "pinyin": "Nǐ hǎo", "arabic": "مرحباً"},
{"chinese": "谢谢", "pinyin": "Xièxiè", "arabic": "شكراً"},
{"chinese": "再见", "pinyin": "Zàijiàn", "arabic": "إلى اللقاء"},
{"chinese": "爱", "pinyin": "Ài", "arabic": "حب"},
{"chinese": "水", "pinyin": "Shuǐ", "arabic": "ماء"},
{"chinese": "吃", "pinyin": "Chī", "arabic": "يأكل"},
{"chinese": "喝", "pinyin": "Hē", "arabic": "يشرب"},
{"chinese": "大", "pinyin": "Dà", "arabic": "كبير"},
{"chinese": "小", "pinyin": "Xiǎo", "arabic": "صغير"},
{"chinese": "是", "pinyin": "Shì", "arabic": "نعم / يكون"},
{"chinese": "不", "pinyin": "Bù", "arabic": "لا / ليس"},
{"chinese": "人", "pinyin": "Rén", "arabic": "شخص"},
{"chinese": "学生", "pinyin": "Xuésheng", "arabic": "طالب"},
{"chinese": "老师", "pinyin": "Lǎoshī", "arabic": "معلم"},
{"chinese": "中国", "pinyin": "Zhōngguó", "arabic": "الصين"},
{"chinese": "美国", "pinyin": "Měiguó", "arabic": "أمريكا"},
]
async def start_word_matching_game(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Starts the word matching game"""
if len(GAME_WORDS) < 4:
await update.message.reply_text("لا توجد كلمات كافية لبدء اللعبة.")
return ConversationHandler.END
await generate_new_question(update, context)
return SELECTING_ANSWER
async def generate_new_question(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Generates a new word matching question"""
correct_word = random.choice(GAME_WORDS)
incorrect_words = random.sample([w for w in GAME_WORDS if w != correct_word], 3)
options = [correct_word] + incorrect_words
random.shuffle(options)
context.user_data["game_correct_answer"] = correct_word["chinese"]
context.user_data["game_options"] = [w["chinese"] for w in options]
keyboard = []
for i, word in enumerate(options):
keyboard.append([InlineKeyboardButton(f"{chr(65+i)}) {word['chinese']}", callback_data=f"game_answer_{word['chinese']}")])
keyboard.append([InlineKeyboardButton("❌ إنهاء اللعبة", callback_data="game_end")])
reply_markup = InlineKeyboardMarkup(keyboard)
question_text = (
f"🎮 **لعبة تطابق الكلمات!**\n\n"
f"ما معنى كلمة \"**{correct_word['arabic']}**\" بالصينية؟\n\n"
f"**Pinyin:** {correct_word['pinyin']}\n"
)
if update.callback_query:
await update.callback_query.edit_message_text(question_text, reply_markup=reply_markup, parse_mode='Markdown')
else:
await update.message.reply_text(question_text, reply_markup=reply_markup, parse_mode='Markdown')
async def check_answer(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Checks the user's answer for the word matching game"""
query = update.callback_query
await query.answer()
user_answer = query.data.split("game_answer_")[1]
correct_answer = context.user_data.get("game_correct_answer")
if user_answer == correct_answer:
await query.edit_message_text("✅ إجابة صحيحة! أحسنت!\n\nلنلعب جولة أخرى.")
await generate_new_question(update, context)
return SELECTING_ANSWER
else:
await query.edit_message_text(
f"❌ إجابة خاطئة. الإجابة الصحيحة كانت: **{correct_answer}**\n\nلنلعب جولة أخرى.",
parse_mode='Markdown'
)
await generate_new_question(update, context)
return SELECTING_ANSWER
async def end_word_matching_game(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Ends the word matching game"""
query = update.callback_query
await query.answer()
await query.edit_message_text("👋 تم إنهاء لعبة تطابق الكلمات. نأمل أن تكون قد استمتعت!")
context.user_data.pop("game_correct_answer", None)
context.user_data.pop("game_options", None)
return ConversationHandler.END
# --- Content from leaderboard_feature.py ---
USER_ACHIEVEMENTS_PREFIX = "user_achievements_"
def get_all_user_ids() -> List[int]:
"""Get a list of all user IDs who have achievement data files."""
user_ids = []
for filename in os.listdir("."):
if filename.startswith(USER_ACHIEVEMENTS_PREFIX) and filename.endswith(".json"):
try:
user_id_str = filename[len(USER_ACHIEVEMENTS_PREFIX):-len(".json")]
user_ids.append(int(user_id_str))
except ValueError:
continue # Skip files with invalid user ID format
return user_ids
def load_user_points(user_id: int) -> int:
"""Load total points for a specific user."""
try:
# Load achievement data for the user and return total_points
with open(f"{USER_ACHIEVEMENTS_PREFIX}{user_id}.json", "r", encoding="utf-8") as f:
user_ach_data = json.load(f)
return user_ach_data.get("total_points", 0)
except FileNotFoundError:
return 0
except json.JSONDecodeError:
return 0
async def show_leaderboard(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Display the leaderboard."""
query = update.callback_query
if query:
await query.answer()
all_user_ids = get_all_user_ids()
leaderboard_data = []
for user_id in all_user_ids:
points = load_user_points(user_id)
username = f"User {user_id}" # Placeholder
try:
user_info = await context.bot.get_chat(user_id)
if user_info.username:
username = f"@{user_info.username}"
elif user_info.first_name:
username = user_info.first_name
except Exception as e:
print(f"Could not fetch user info for {user_id}: {e}")
leaderboard_data.append({"user_id": user_id, "points": points, "username": username})
# Sort by points in descending order
leaderboard_data.sort(key=lambda x: x["points"], reverse=True)
leaderboard_text = "🏆 **لوحة الصدارة** 🏆\n\n"
if not leaderboard_data:
leaderboard_text += "لا توجد بيانات في لوحة الصدارة بعد."
else:
for i, entry in enumerate(leaderboard_data):
if i == 0: # Gold medal for 1st place
leaderboard_text += f"🥇 1. {entry['username']}: **{entry['points']} نقطة**\n"
elif i == 1: # Silver medal for 2nd place
leaderboard_text += f"🥈 2. {entry['username']}: **{entry['points']} نقطة**\n"
elif i == 2: # Bronze medal for 3rd place
leaderboard_text += f"🥉 3. {entry['username']}: **{entry['points']} نقطة**\n"
else:
leaderboard_text += f"{i+1}. {entry['username']}: {entry['points']} نقطة\n"
if i >= 9: # Show top 10 only
break
keyboard = [
[InlineKeyboardButton("◀️ رجوع", callback_data="BACK")]
]
reply_markup = InlineKeyboardMarkup(keyboard)
if query:
await query.edit_message_text(leaderboard_text, parse_mode='Markdown', reply_markup=reply_markup)
else:
await update.message.reply_text(leaderboard_text, parse_mode='Markdown', reply_markup=reply_markup)
# --- Content from main.py (core bot logic) ---
# Build main keyboard
def build_main_menu():
items = [
("📚 التعليم المخصص", "MENU_CUSTOM_EDU"),
("🕌 القرآن", "SKIP_Quran"),
("🗂️ القاموس", "SKIP_Dictionary"),
("📖 القصص", "SKIP_Stories"),
("🔤 قواعد", "SKIP_GrammarLessons"),
("📑 مراجعة", "SKIP_GrammarReview"),
("💬 محادثات", "SKIP_Dialogues"),
("🃏 Flashcards", "SKIP_Flashcards"),
("❓ كويزات", "SKIP_Quizzes"),
("📷 معجم صور", "SKIP_PictureDictionary"),
("🧑🏫 مرشد أكاديمي", "MENU_ACADEMIC_ADVISOR"),
("🤖 AI Chat", "MENU_AI_CHAT"),
("🏆 الإنجازات", "MENU_ACHIEVEMENTS"),
("🔊 نطق صوتي", "MENU_TTS"),
("🔔 تذكيرات", "MENU_REMINDERS"),
("🎮 لعبة كلمات", "MENU_WORD_GAME"),
("🏅 لوحة الصدارة", "MENU_LEADERBOARD"),
("⚙️ Admin", "MENU_Admin")
]
kb, row = [], []
for i, (t, c) in enumerate(items, 1):
row.append(InlineKeyboardButton(t, callback_data=c))
if i % 3 == 0:
kb.append(row)
row = []
if row:
kb.append(row)
return kb
# /start handler
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("مرحبًا! اختر قسمًا:", reply_markup=InlineKeyboardMarkup(build_main_menu()))
# Main callback handler
async def main_h(update: Update, context: ContextTypes.DEFAULT_TYPE):
q = update.callback_query
await q.answer()
d = q.data
# Skip placeholders
if d.startswith("SKIP_"):
sec = d.split("_", 1)[1]
return await q.edit_message_text(f"{sec}: قريبًا🔥")
# Applications submenu
if d == "MENU_Apps":
apps = [
("🚧 قصص سكب", "SKIP_Stories"),
("🚧 قواعد سكب", "SKIP_Rules"),
("🚧 مراجعة سكب", "SKIP_Review"),
("🚧 محادثات سكب", "SKIP_Convo"),
("🚧 فلاش كاردز سكب", "SKIP_Flashcards"),
("🚧 كويزات سكب", "SKIP_Quizzes")
]
kb, row = [], []
for i, (t, c) in enumerate(apps, 1):
row.append(InlineKeyboardButton(t, callback_data=c))
if len(row) == 2:
kb.append(row)
row = []
if row:
kb.append(row)
kb.append([InlineKeyboardButton("◀️ رجوع", callback_data="BACK")])
return await q.edit_message_text("قسم التطبيقات:", reply_markup=InlineKeyboardMarkup(kb))
# Custom Education Menu
if d == "MENU_CUSTOM_EDU":
kb = [
[InlineKeyboardButton("📚 مناهج STEM", callback_data="SKIP_STEM")],
[InlineKeyboardButton("💡 مهارات التفكير النقدي", callback_data="SKIP_CRITICAL_THINKING")],
[InlineKeyboardButton("📝 اختبارات القدرات والتحصيلي", callback_data="SKIP_TESTS")],
[InlineKeyboardButton("◀️ رجوع", callback_data="BACK")]
]
return await q.edit_message_text("اختر قسم التعليم المخصص:", reply_markup=InlineKeyboardMarkup(kb))
# Academic Advisor shortcut
if d == "MENU_ACADEMIC_ADVISOR":
# Note: This will trigger the AI Chat start and select the mode automatically
context.user_data["ai_mode"] = "academic_advisor"
context.user_data["ai_history"] = []
await q.edit_message_text(
"✅ تم تفعيل وضع المرشد الأكاديمي.\n\n"
"اسأل عن التخصصات، اختبارات القدرات، أو أي نصيحة دراسية!\n"
"استخدم /stop_ai لإنهاء المحادثة."
)
return
# Back to main
if d == "BACK":
return await start(update, context)
# AI Chat
if d == "MENU_AI_CHAT":
return await ai_chat_start(update, context)
# Achievements
if d == "MENU_ACHIEVEMENTS":
return await show_achievements(update, context)
# Text-to-Speech
if d == "MENU_TTS":
return await text_to_speech_start(update, context)
# Daily Reminders
if d == "MENU_REMINDERS":
return await start_reminders_setup(update, context)
# Word Game
if d == "MENU_WORD_GAME":
return await start_word_matching_game(update, context)
# Leaderboard
if d == "MENU_LEADERBOARD":
return await show_leaderboard(update, context)
# Admin panel
if d == "MENU_Admin":
if not is_admin(q.from_user.id):
return await q.edit_message_text("⛔ للمشرفين فقط.")
kb = [
[InlineKeyboardButton("➕ إضافة", callback_data="ADM_ADD")],
[InlineKeyboardButton("📝 استعراض", callback_data="ADM_VIEW")],
[InlineKeyboardButton("❌ حذف", callback_data="ADM_DEL")],
[InlineKeyboardButton("📁 رفع ملف", callback_data="ADM_UP")],
[InlineKeyboardButton("◀️ رجوع", callback_data="BACK")]
]
return await q.edit_message_text("لوحة المشرف:", reply_markup=InlineKeyboardMarkup(kb))
# Section display or upload entry
if d.startswith("SEC_") or d.startswith("MENU_"):
sec = d.split("_",1)[1]
items = data.get(sec, [])
kb, row = [], []
for it in items:
row.append(InlineKeyboardButton(it["title"], callback_data=f"VIEW_{sec}_{it['id']}"))
if len(row) == 2:
kb.append(row)
row = []
if row:
kb.append(row)
if is_admin(q.from_user.id):
kb.append([InlineKeyboardButton("📁 رفع هنا", callback_data=f"UP_{sec}")])
kb.append([InlineKeyboardButton("◀️ رجوع", callback_data="BACK")])
return await q.edit_message_text(f"قسم {sec}:", reply_markup=InlineKeyboardMarkup(kb))
# view item -> send document by file_id
async def view_i(update: Update, context: ContextTypes.DEFAULT_TYPE):
q = update.callback_query
await q.answer()
_, sec, sid = q.data.split("_")
idx = int(sid)
itm = next((x for x in data.get(sec, []) if x['id'] == idx), None)
if not itm:
return await q.edit_message_text("⚠️ غير موجود.")
await q.message.reply_document(document=itm['content'], filename=itm['title'])