zxyu

Untitled

Aug 7th, 2025
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.93 KB | None | 0 0
  1. <?php
  2. define('BOT_TOKEN', '7617464567');
  3. define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
  4. define('DATA_FILE', 'bot_data.json');
  5.  
  6. // تحميل البيانات أو إنشاء ملف جديد إذا لم يكن موجوداً
  7. function loadData() {
  8.     if (!file_exists(DATA_FILE)) {
  9.         $initial_data = [
  10.             'channels' => [],
  11.             'articles' => [],
  12.             'quotes' => [],
  13.             'published' => [
  14.                 'articles' => [],
  15.                 'quotes' => []
  16.             ],
  17.             'settings' => [
  18.                 'publishing_active' => true,
  19.                 'last_article_time' => 0,
  20.                 'last_quote_time' => 0,
  21.                 'start_time' => time()
  22.             ],
  23.             'stats' => [
  24.                 'articles_published' => 0,
  25.                 'quotes_published' => 0
  26.             ],
  27.             'user_states' => []
  28.         ];
  29.         file_put_contents(DATA_FILE, json_encode($initial_data, JSON_UNESCAPED_UNICODE));
  30.     }
  31.     return json_decode(file_get_contents(DATA_FILE), true);
  32. }
  33.  
  34. $data = loadData();
  35.  
  36. // معالجة الرسائل الواردة
  37. $update = json_decode(file_get_contents('php://input'), true);
  38. if (isset($update['message'])) {
  39.     $message = $update['message'];
  40.     $chat_id = $message['chat']['id'];
  41.     $user_id = $message['from']['id'];
  42.     $text = isset($message['text']) ? $message['text'] : '';
  43.  
  44.     // معالجة حالات المستخدم أولاً
  45.     if (handleUserState($chat_id, $user_id, $text, $data)) {
  46.         exit;
  47.     }
  48.  
  49.     // معالجة الأوامر الرئيسية
  50.     switch ($text) {
  51.         case '/start':
  52.         case '/menu':
  53.             showMainMenu($chat_id);
  54.             break;
  55.         case 'تفعيل النشر':
  56.             $data['settings']['publishing_active'] = true;
  57.             saveData($data);
  58.             sendMessage($chat_id, "✅ تم تفعيل النشر التلقائي");
  59.             break;
  60.         case 'إيقاف النشر':
  61.             $data['settings']['publishing_active'] = false;
  62.             saveData($data);
  63.             sendMessage($chat_id, "⏸ تم إيقاف النشر التلقائي");
  64.             break;
  65.         case 'حالة البوت':
  66.             showBotStatus($chat_id, $data);
  67.             break;
  68.         case 'نشر مقال الآن':
  69.             if (empty($data['channels'])) {
  70.                 sendMessage($chat_id, "❌ لا توجد قنوات مضافَة. الرجاء إضافة قناة أولاً.");
  71.             } else {
  72.                 foreach ($data['channels'] as $channel) {
  73.                     if ($channel['active']) {
  74.                         publishArticle($channel['id'], $data, true);
  75.                     }
  76.                 }
  77.             }
  78.             break;
  79.         case 'نشر مقتبس الآن':
  80.             if (empty($data['channels'])) {
  81.                 sendMessage($chat_id, "❌ لا توجد قنوات مضافَة. الرجاء إضافة قناة أولاً.");
  82.             } else {
  83.                 foreach ($data['channels'] as $channel) {
  84.                     if ($channel['active']) {
  85.                         publishQuote($channel['id'], $data, true);
  86.                     }
  87.                 }
  88.             }
  89.             break;
  90.         case 'إدارة القنوات':
  91.             showChannelManagementMenu($chat_id, $data);
  92.             break;
  93.         case 'إضافة قناة جديدة':
  94.             startAddChannelProcess($chat_id, $user_id, $data);
  95.             break;
  96.     }
  97. }
  98.  
  99. // النشر التلقائي
  100. if ($data['settings']['publishing_active'] && !empty($data['channels'])) {
  101.     $current_time = time();
  102.    
  103.     // نشر مقال كل 24 ساعة
  104.     if ($current_time - $data['settings']['last_article_time'] > 86400) {
  105.         foreach ($data['channels'] as $channel) {
  106.             if ($channel['active']) {
  107.                 publishArticle($channel['id'], $data);
  108.             }
  109.         }
  110.         $data['settings']['last_article_time'] = $current_time;
  111.         saveData($data);
  112.     }
  113.    
  114.     // نشر مقتبس كل 5 ساعات
  115.     if ($current_time - $data['settings']['last_quote_time'] > 18000) {
  116.         foreach ($data['channels'] as $channel) {
  117.             if ($channel['active']) {
  118.                 publishQuote($channel['id'], $data);
  119.             }
  120.         }
  121.         $data['settings']['last_quote_time'] = $current_time;
  122.         saveData($data);
  123.     }
  124. }
  125.  
  126. // ===== الدوال المساعدة ===== //
  127.  
  128. function saveData($data) {
  129.     file_put_contents(DATA_FILE, json_encode($data, JSON_UNESCAPED_UNICODE));
  130. }
  131.  
  132. function sendMessage($chat_id, $text, $reply_markup = null) {
  133.     $url = API_URL . 'sendMessage';
  134.     $post_fields = [
  135.         'chat_id' => $chat_id,
  136.         'text' => $text,
  137.         'parse_mode' => 'HTML'
  138.     ];
  139.    
  140.     if ($reply_markup) {
  141.         $post_fields['reply_markup'] = $reply_markup;
  142.     }
  143.    
  144.     $ch = curl_init();
  145.     curl_setopt($ch, CURLOPT_URL, $url);
  146.     curl_setopt($ch, CURLOPT_POST, true);
  147.     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
  148.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  149.     $result = curl_exec($ch);
  150.     curl_close($ch);
  151.    
  152.     return $result;
  153. }
  154.  
  155. function showMainMenu($chat_id) {
  156.     $keyboard = [
  157.         'keyboard' => [
  158.             ['تفعيل النشر', 'إيقاف النشر'],
  159.             ['حالة البوت', 'إدارة القنوات'],
  160.             ['نشر مقال الآن', 'نشر مقتبس الآن']
  161.         ],
  162.         'resize_keyboard' => true
  163.     ];
  164.    
  165.     sendMessage($chat_id, "اختر أحد الخيارات:", json_encode($keyboard));
  166. }
  167.  
  168. function showChannelManagementMenu($chat_id, $data) {
  169.     $channels_list = "📋 قائمة القنوات:\n\n";
  170.     if (empty($data['channels'])) {
  171.         $channels_list .= "لا توجد قنوات مضافَة حتى الآن.\n";
  172.     } else {
  173.         foreach ($data['channels'] as $index => $channel) {
  174.             $status = $channel['active'] ? '✅ مفعّلة' : '❌ معطّلة';
  175.             $channels_list .= ($index + 1) . ". {$channel['title']} ({$channel['id']}) - {$status}\n";
  176.         }
  177.     }
  178.    
  179.     $keyboard = [
  180.         'keyboard' => [
  181.             ['إضافة قناة جديدة'],
  182.             ['تفعيل/تعطيل قناة'],
  183.             ['حذف قناة'],
  184.             ['العودة إلى القائمة الرئيسية']
  185.         ],
  186.         'resize_keyboard' => true
  187.     ];
  188.    
  189.     sendMessage($chat_id, $channels_list, json_encode($keyboard));
  190. }
  191.  
  192. function handleUserState($chat_id, $user_id, $text, &$data) {
  193.     if (!isset($data['user_states'][$user_id])) {
  194.         return false;
  195.     }
  196.    
  197.     $state = $data['user_states'][$user_id];
  198.    
  199.     if ($state['action'] === 'adding_channel') {
  200.         if ($text === 'إلغاء') {
  201.             unset($data['user_states'][$user_id]);
  202.             saveData($data);
  203.             sendMessage($chat_id, "تم إلغاء عملية إضافة القناة.");
  204.             showMainMenu($chat_id);
  205.             return true;
  206.         }
  207.        
  208.         // التحقق من صحة معرف القناة
  209.         if (preg_match('/^(@\w+|-?\d+)$/', $text)) {
  210.             $channel_id = $text;
  211.             $chat_info = getChatInfo($channel_id);
  212.            
  213.             if ($chat_info && $chat_info['ok']) {
  214.                 $new_channel = [
  215.                     'id' => $channel_id,
  216.                     'title' => $chat_info['result']['title'] ?? 'قناة بدون اسم',
  217.                     'active' => true,
  218.                     'added_by' => $user_id,
  219.                     'added_at' => time()
  220.                 ];
  221.                
  222.                 $data['channels'][] = $new_channel;
  223.                 unset($data['user_states'][$user_id]);
  224.                 saveData($data);
  225.                
  226.                 sendMessage($chat_id, "✅ تمت إضافة القناة بنجاح:\n{$new_channel['title']} ({$channel_id})");
  227.                 showChannelManagementMenu($chat_id, $data);
  228.             } else {
  229.                 sendMessage($chat_id, "❌ لا يمكن الوصول إلى القناة. تأكد من:\n1. أن البوت مشرف في القناة\n2. أن معرف القناة صحيح");
  230.             }
  231.         } else {
  232.             sendMessage($chat_id, "❌ معرّف القناة غير صالح. يجب أن يبدأ ب @ أو أن يكون رقم ID صحيحاً.");
  233.         }
  234.         return true;
  235.     }
  236.    
  237.     return false;
  238. }
  239.  
  240. function startAddChannelProcess($chat_id, $user_id, &$data) {
  241.     $data['user_states'][$user_id] = [
  242.         'action' => 'adding_channel',
  243.         'timestamp' => time()
  244.     ];
  245.     saveData($data);
  246.    
  247.     $keyboard = [
  248.         'keyboard' => [
  249.             ['إلغاء']
  250.         ],
  251.         'resize_keyboard' => true
  252.     ];
  253.    
  254.     sendMessage($chat_id, "📌 أرسل معرّف القناة (مثال: @channelname) أو رقم ID الخاص بها.\n\nتأكد من:\n1. أن البوت مشرف في القناة\n2. لديه صلاحية نشر الرسائل", json_encode($keyboard));
  255. }
  256.  
  257. function getChatInfo($chat_id) {
  258.     $url = API_URL . 'getChat?chat_id=' . urlencode($chat_id);
  259.     $response = file_get_contents($url);
  260.     return json_decode($response, true);
  261. }
  262.  
  263. function publishArticle($channel_id, &$data, $manual = false) {
  264.     $article = fetchDostoevskyArticle($data);
  265.    
  266.     if ($article && !in_array($article['id'], $data['published']['articles'])) {
  267.         $message = "<b>{$article['title']}</b>\n\n{$article['content']}";
  268.        
  269.         $result = sendMessage($channel_id, $message);
  270.         $result_data = json_decode($result, true);
  271.        
  272.         if ($result_data && $result_data['ok']) {
  273.             $data['stats']['articles_published']++;
  274.             $data['published']['articles'][] = $article['id'];
  275.             saveData($data);
  276.            
  277.             if ($manual) {
  278.                 sendMessage($data['user_states'][$article['added_by']]['chat_id'] ?? $channel_id,
  279.                     "✅ تم نشر المقال بنجاح في القناة");
  280.             }
  281.             return true;
  282.         } else {
  283.             if ($manual) {
  284.                 sendMessage($channel_id, "❌ فشل في نشر المقال. تأكد من صلاحيات البوت.");
  285.             }
  286.             return false;
  287.         }
  288.     } elseif ($manual) {
  289.         sendMessage($channel_id, "❌ لم يتم العثور على مقالات جديدة أو حدث خطأ أثناء النشر.");
  290.         return false;
  291.     }
  292.     return false;
  293. }
  294.  
  295. function publishQuote($channel_id, &$data, $manual = false) {
  296.     $quote = fetchDostoevskyQuote($data);
  297.    
  298.     if ($quote && !in_array($quote['id'], $data['published']['quotes'])) {
  299.         $message = "✍️ <i>{$quote['text']}</i>\n\n- <b>فيودور دوستويفسكي</b>";
  300.         if (isset($quote['work'])) {
  301.             $message .= " ({$quote['work']})";
  302.         }
  303.        
  304.         $result = sendMessage($channel_id, $message);
  305.         $result_data = json_decode($result, true);
  306.        
  307.         if ($result_data && $result_data['ok']) {
  308.             $data['stats']['quotes_published']++;
  309.             $data['published']['quotes'][] = $quote['id'];
  310.             saveData($data);
  311.            
  312.             if ($manual) {
  313.                 sendMessage($data['user_states'][$quote['added_by']]['chat_id'] ?? $channel_id,
  314.                     "✅ تم نشر المقتبس بنجاح في القناة");
  315.             }
  316.             return true;
  317.         } else {
  318.             if ($manual) {
  319.                 sendMessage($channel_id, "❌ فشل في نشر المقتبس. تأكد من صلاحيات البوت.");
  320.             }
  321.             return false;
  322.         }
  323.     } elseif ($manual) {
  324.         sendMessage($channel_id, "❌ لم يتم العثور على مقتبسات جديدة أو حدث خطأ أثناء النشر.");
  325.         return false;
  326.     }
  327.     return false;
  328. }
  329.  
  330. // دالة لجلب المقالات (يمكن استبدالها بمصدر خارجي)
  331. function fetchDostoevskyArticle(&$data) {
  332.     $articles = [
  333.         [
  334.             'id' => 'article_1',
  335.             'title' => 'الجريمة والعقاب: تحليل نفسي',
  336.             'content' => 'في روايته الخالدة "الجريمة والعقاب"، يغوص دوستويفسكي في أعماق النفس البشرية...',
  337.             'source' => 'تحليل مستوحى من رواية الجريمة والعقاب'
  338.         ],
  339.         [
  340.             'id' => 'article_2',
  341.             'title' => 'الأخوة كارامازوف: الصراع بين الإيمان والعقل',
  342.             'content' => 'تعتبر "الأخوة كارامازوف" واحدة من أعظم الروايات التي تناولت الصراع بين...',
  343.             'source' => 'تحليل مستوحى من رواية الأخوة كارامازوف'
  344.         ]
  345.     ];
  346.    
  347.     $unpublished = array_filter($articles, function($article) use ($data) {
  348.         return !in_array($article['id'], $data['published']['articles']);
  349.     });
  350.    
  351.     return !empty($unpublished) ? $unpublished[array_rand($unpublished)] : null;
  352. }
  353.  
  354. // دالة لجلب المقتبسات (يمكن استبدالها بمصدر خارجي)
  355. function fetchDostoevskyQuote(&$data) {
  356.     $quotes = [
  357.         [
  358.             'id' => 'quote_1',
  359.             'text' => "إذا أراد الله أن يحرم إنساناً، فإنه يحرمه أولاً من العقل.",
  360.             'work' => "الجريمة والعقاب"
  361.         ],
  362.         [
  363.             'id' => 'quote_2',
  364.             'text' => "إن السعادة لا تكمن في السعادة ذاتها، بل في تحقيقها.",
  365.             'work' => "مذكرات من تحت الأرض"
  366.         ]
  367.     ];
  368.    
  369.     $unpublished = array_filter($quotes, function($quote) use ($data) {
  370.         return !in_array($quote['id'], $data['published']['quotes']);
  371.     });
  372.    
  373.     return !empty($unpublished) ? $unpublished[array_rand($unpublished)] : null;
  374. }
Advertisement
Add Comment
Please, Sign In to add comment