Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('BOT_TOKEN', '7617464567');
- define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
- define('DATA_FILE', 'bot_data.json');
- // تحميل البيانات أو إنشاء ملف جديد إذا لم يكن موجوداً
- function loadData() {
- if (!file_exists(DATA_FILE)) {
- $initial_data = [
- 'channels' => [],
- 'articles' => [],
- 'quotes' => [],
- 'published' => [
- 'articles' => [],
- 'quotes' => []
- ],
- 'settings' => [
- 'publishing_active' => true,
- 'last_article_time' => 0,
- 'last_quote_time' => 0,
- 'start_time' => time()
- ],
- 'stats' => [
- 'articles_published' => 0,
- 'quotes_published' => 0
- ],
- 'user_states' => []
- ];
- file_put_contents(DATA_FILE, json_encode($initial_data, JSON_UNESCAPED_UNICODE));
- }
- return json_decode(file_get_contents(DATA_FILE), true);
- }
- $data = loadData();
- // معالجة الرسائل الواردة
- $update = json_decode(file_get_contents('php://input'), true);
- if (isset($update['message'])) {
- $message = $update['message'];
- $chat_id = $message['chat']['id'];
- $user_id = $message['from']['id'];
- $text = isset($message['text']) ? $message['text'] : '';
- // معالجة حالات المستخدم أولاً
- if (handleUserState($chat_id, $user_id, $text, $data)) {
- exit;
- }
- // معالجة الأوامر الرئيسية
- switch ($text) {
- case '/start':
- case '/menu':
- showMainMenu($chat_id);
- break;
- case 'تفعيل النشر':
- $data['settings']['publishing_active'] = true;
- saveData($data);
- sendMessage($chat_id, "✅ تم تفعيل النشر التلقائي");
- break;
- case 'إيقاف النشر':
- $data['settings']['publishing_active'] = false;
- saveData($data);
- sendMessage($chat_id, "⏸ تم إيقاف النشر التلقائي");
- break;
- case 'حالة البوت':
- showBotStatus($chat_id, $data);
- break;
- case 'نشر مقال الآن':
- if (empty($data['channels'])) {
- sendMessage($chat_id, "❌ لا توجد قنوات مضافَة. الرجاء إضافة قناة أولاً.");
- } else {
- foreach ($data['channels'] as $channel) {
- if ($channel['active']) {
- publishArticle($channel['id'], $data, true);
- }
- }
- }
- break;
- case 'نشر مقتبس الآن':
- if (empty($data['channels'])) {
- sendMessage($chat_id, "❌ لا توجد قنوات مضافَة. الرجاء إضافة قناة أولاً.");
- } else {
- foreach ($data['channels'] as $channel) {
- if ($channel['active']) {
- publishQuote($channel['id'], $data, true);
- }
- }
- }
- break;
- case 'إدارة القنوات':
- showChannelManagementMenu($chat_id, $data);
- break;
- case 'إضافة قناة جديدة':
- startAddChannelProcess($chat_id, $user_id, $data);
- break;
- }
- }
- // النشر التلقائي
- if ($data['settings']['publishing_active'] && !empty($data['channels'])) {
- $current_time = time();
- // نشر مقال كل 24 ساعة
- if ($current_time - $data['settings']['last_article_time'] > 86400) {
- foreach ($data['channels'] as $channel) {
- if ($channel['active']) {
- publishArticle($channel['id'], $data);
- }
- }
- $data['settings']['last_article_time'] = $current_time;
- saveData($data);
- }
- // نشر مقتبس كل 5 ساعات
- if ($current_time - $data['settings']['last_quote_time'] > 18000) {
- foreach ($data['channels'] as $channel) {
- if ($channel['active']) {
- publishQuote($channel['id'], $data);
- }
- }
- $data['settings']['last_quote_time'] = $current_time;
- saveData($data);
- }
- }
- // ===== الدوال المساعدة ===== //
- function saveData($data) {
- file_put_contents(DATA_FILE, json_encode($data, JSON_UNESCAPED_UNICODE));
- }
- function sendMessage($chat_id, $text, $reply_markup = null) {
- $url = API_URL . 'sendMessage';
- $post_fields = [
- 'chat_id' => $chat_id,
- 'text' => $text,
- 'parse_mode' => 'HTML'
- ];
- if ($reply_markup) {
- $post_fields['reply_markup'] = $reply_markup;
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- function showMainMenu($chat_id) {
- $keyboard = [
- 'keyboard' => [
- ['تفعيل النشر', 'إيقاف النشر'],
- ['حالة البوت', 'إدارة القنوات'],
- ['نشر مقال الآن', 'نشر مقتبس الآن']
- ],
- 'resize_keyboard' => true
- ];
- sendMessage($chat_id, "اختر أحد الخيارات:", json_encode($keyboard));
- }
- function showChannelManagementMenu($chat_id, $data) {
- $channels_list = "📋 قائمة القنوات:\n\n";
- if (empty($data['channels'])) {
- $channels_list .= "لا توجد قنوات مضافَة حتى الآن.\n";
- } else {
- foreach ($data['channels'] as $index => $channel) {
- $status = $channel['active'] ? '✅ مفعّلة' : '❌ معطّلة';
- $channels_list .= ($index + 1) . ". {$channel['title']} ({$channel['id']}) - {$status}\n";
- }
- }
- $keyboard = [
- 'keyboard' => [
- ['إضافة قناة جديدة'],
- ['تفعيل/تعطيل قناة'],
- ['حذف قناة'],
- ['العودة إلى القائمة الرئيسية']
- ],
- 'resize_keyboard' => true
- ];
- sendMessage($chat_id, $channels_list, json_encode($keyboard));
- }
- function handleUserState($chat_id, $user_id, $text, &$data) {
- if (!isset($data['user_states'][$user_id])) {
- return false;
- }
- $state = $data['user_states'][$user_id];
- if ($state['action'] === 'adding_channel') {
- if ($text === 'إلغاء') {
- unset($data['user_states'][$user_id]);
- saveData($data);
- sendMessage($chat_id, "تم إلغاء عملية إضافة القناة.");
- showMainMenu($chat_id);
- return true;
- }
- // التحقق من صحة معرف القناة
- if (preg_match('/^(@\w+|-?\d+)$/', $text)) {
- $channel_id = $text;
- $chat_info = getChatInfo($channel_id);
- if ($chat_info && $chat_info['ok']) {
- $new_channel = [
- 'id' => $channel_id,
- 'title' => $chat_info['result']['title'] ?? 'قناة بدون اسم',
- 'active' => true,
- 'added_by' => $user_id,
- 'added_at' => time()
- ];
- $data['channels'][] = $new_channel;
- unset($data['user_states'][$user_id]);
- saveData($data);
- sendMessage($chat_id, "✅ تمت إضافة القناة بنجاح:\n{$new_channel['title']} ({$channel_id})");
- showChannelManagementMenu($chat_id, $data);
- } else {
- sendMessage($chat_id, "❌ لا يمكن الوصول إلى القناة. تأكد من:\n1. أن البوت مشرف في القناة\n2. أن معرف القناة صحيح");
- }
- } else {
- sendMessage($chat_id, "❌ معرّف القناة غير صالح. يجب أن يبدأ ب @ أو أن يكون رقم ID صحيحاً.");
- }
- return true;
- }
- return false;
- }
- function startAddChannelProcess($chat_id, $user_id, &$data) {
- $data['user_states'][$user_id] = [
- 'action' => 'adding_channel',
- 'timestamp' => time()
- ];
- saveData($data);
- $keyboard = [
- 'keyboard' => [
- ['إلغاء']
- ],
- 'resize_keyboard' => true
- ];
- sendMessage($chat_id, "📌 أرسل معرّف القناة (مثال: @channelname) أو رقم ID الخاص بها.\n\nتأكد من:\n1. أن البوت مشرف في القناة\n2. لديه صلاحية نشر الرسائل", json_encode($keyboard));
- }
- function getChatInfo($chat_id) {
- $url = API_URL . 'getChat?chat_id=' . urlencode($chat_id);
- $response = file_get_contents($url);
- return json_decode($response, true);
- }
- function publishArticle($channel_id, &$data, $manual = false) {
- $article = fetchDostoevskyArticle($data);
- if ($article && !in_array($article['id'], $data['published']['articles'])) {
- $message = "<b>{$article['title']}</b>\n\n{$article['content']}";
- $result = sendMessage($channel_id, $message);
- $result_data = json_decode($result, true);
- if ($result_data && $result_data['ok']) {
- $data['stats']['articles_published']++;
- $data['published']['articles'][] = $article['id'];
- saveData($data);
- if ($manual) {
- sendMessage($data['user_states'][$article['added_by']]['chat_id'] ?? $channel_id,
- "✅ تم نشر المقال بنجاح في القناة");
- }
- return true;
- } else {
- if ($manual) {
- sendMessage($channel_id, "❌ فشل في نشر المقال. تأكد من صلاحيات البوت.");
- }
- return false;
- }
- } elseif ($manual) {
- sendMessage($channel_id, "❌ لم يتم العثور على مقالات جديدة أو حدث خطأ أثناء النشر.");
- return false;
- }
- return false;
- }
- function publishQuote($channel_id, &$data, $manual = false) {
- $quote = fetchDostoevskyQuote($data);
- if ($quote && !in_array($quote['id'], $data['published']['quotes'])) {
- $message = "✍️ <i>{$quote['text']}</i>\n\n- <b>فيودور دوستويفسكي</b>";
- if (isset($quote['work'])) {
- $message .= " ({$quote['work']})";
- }
- $result = sendMessage($channel_id, $message);
- $result_data = json_decode($result, true);
- if ($result_data && $result_data['ok']) {
- $data['stats']['quotes_published']++;
- $data['published']['quotes'][] = $quote['id'];
- saveData($data);
- if ($manual) {
- sendMessage($data['user_states'][$quote['added_by']]['chat_id'] ?? $channel_id,
- "✅ تم نشر المقتبس بنجاح في القناة");
- }
- return true;
- } else {
- if ($manual) {
- sendMessage($channel_id, "❌ فشل في نشر المقتبس. تأكد من صلاحيات البوت.");
- }
- return false;
- }
- } elseif ($manual) {
- sendMessage($channel_id, "❌ لم يتم العثور على مقتبسات جديدة أو حدث خطأ أثناء النشر.");
- return false;
- }
- return false;
- }
- // دالة لجلب المقالات (يمكن استبدالها بمصدر خارجي)
- function fetchDostoevskyArticle(&$data) {
- $articles = [
- [
- 'id' => 'article_1',
- 'title' => 'الجريمة والعقاب: تحليل نفسي',
- 'content' => 'في روايته الخالدة "الجريمة والعقاب"، يغوص دوستويفسكي في أعماق النفس البشرية...',
- 'source' => 'تحليل مستوحى من رواية الجريمة والعقاب'
- ],
- [
- 'id' => 'article_2',
- 'title' => 'الأخوة كارامازوف: الصراع بين الإيمان والعقل',
- 'content' => 'تعتبر "الأخوة كارامازوف" واحدة من أعظم الروايات التي تناولت الصراع بين...',
- 'source' => 'تحليل مستوحى من رواية الأخوة كارامازوف'
- ]
- ];
- $unpublished = array_filter($articles, function($article) use ($data) {
- return !in_array($article['id'], $data['published']['articles']);
- });
- return !empty($unpublished) ? $unpublished[array_rand($unpublished)] : null;
- }
- // دالة لجلب المقتبسات (يمكن استبدالها بمصدر خارجي)
- function fetchDostoevskyQuote(&$data) {
- $quotes = [
- [
- 'id' => 'quote_1',
- 'text' => "إذا أراد الله أن يحرم إنساناً، فإنه يحرمه أولاً من العقل.",
- 'work' => "الجريمة والعقاب"
- ],
- [
- 'id' => 'quote_2',
- 'text' => "إن السعادة لا تكمن في السعادة ذاتها، بل في تحقيقها.",
- 'work' => "مذكرات من تحت الأرض"
- ]
- ];
- $unpublished = array_filter($quotes, function($quote) use ($data) {
- return !in_array($quote['id'], $data['published']['quotes']);
- });
- return !empty($unpublished) ? $unpublished[array_rand($unpublished)] : null;
- }
Advertisement
Add Comment
Please, Sign In to add comment