/* ----------------------------- Send once: media + caption (title + first paragraph + link) ----------------------------- */ if (!function_exists('ptm_eitaa_send_post')) { function ptm_eitaa_send_post($post, $is_new) { $token = ptm_eitaa_get('ptm_eitaa_token'); $chat = eitaa_normalize_chat_id( ptm_eitaa_pick_chat_id('ptm_eitaa_channel','ptm_eitaa_channel_url') ); $gw = rtrim(ptm_eitaa_get('ptm_eitaa_gateway'), '/'); if (empty($token) || empty($chat)) { ptm_eitaa_log(array('phase'=>'auto','ok'=>false,'error'=>'missing_token_or_chat','post_id'=>$post->ID)); return; } $caption = ptm_eitaa_build_caption($post, $is_new); $media = ptm_eitaa_find_media($post); // اگر رسانه موجود است: فقط یک پیام رسانه با کپشن کوتاه if ($media && !empty($media['url'])) { $url = ptm_eitaa_abs_url($media['url']); $kind = (strtolower($media['type']) === 'video') ? 'video' : 'photo'; // 1) تلاش با URL مستقیم $endpoint = "{$gw}/{$token}/send" . ucfirst($kind); $req = curl_init($endpoint); curl_setopt_array($req, array( CURLOPT_POST => true, CURLOPT_POSTFIELDS => array('chat_id'=>$chat, $kind=>$url, 'caption'=>$caption), CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 20, CURLOPT_TIMEOUT => 45, )); $resp = curl_exec($req); $http_code = curl_getinfo($req, CURLINFO_HTTP_CODE); curl_close($req); $ok = (strpos((string)$resp,'"ok":true') !== false); ptm_eitaa_log(array( 'phase'=>'auto_url_media','action'=>$is_new?'new':'update', 'api'=>basename($endpoint),'http_code'=>$http_code,'api_ok'=>$ok, 'chat_id'=>$chat,'post_id'=>$post->ID,'media_type'=>$media['type'],'media_url'=>$url )); if ($ok) return; // 2) اگر URL جواب نداد، فایل واقعی $file_path = $url; ptm_eitaa_prepare_file($file_path); if (file_exists($file_path)) { $endpoint2 = "{$gw}/{$token}/send" . ucfirst($kind); $req2 = curl_init($endpoint2); curl_setopt_array($req2, array( CURLOPT_POST => true, CURLOPT_POSTFIELDS => array( 'chat_id' => $chat, $kind => new CurlFile($file_path, null, basename($file_path)), 'caption' => $caption, ), CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 25, CURLOPT_TIMEOUT => 60, )); $resp2 = curl_exec($req2); $code2 = curl_getinfo($req2, CURLINFO_HTTP_CODE); curl_close($req2); $ok2 = (strpos((string)$resp2,'"ok":true') !== false); ptm_eitaa_log(array( 'phase'=>'auto_file_media','http_code'=>$code2,'api_ok'=>$ok2, 'chat_id'=>$chat,'post_id'=>$post->ID,'file_path'=>$file_path )); if ($ok2) return; // 3) آخرین شانس: Document (sendFile) با همان کپشن $ep3 = "{$gw}/{$token}/sendFile"; $req3 = curl_init($ep3); curl_setopt_array($req3, array( CURLOPT_POST => true, CURLOPT_POSTFIELDS => array( 'chat_id' => $chat, 'file' => new CurlFile($file_path, null, basename($file_path)), 'caption' => $caption, ), CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 25, CURLOPT_TIMEOUT => 60, )); $resp3 = curl_exec($req3); $code3 = curl_getinfo($req3, CURLINFO_HTTP_CODE); curl_close($req3); $ok3 = (strpos((string)$resp3,'"ok":true') !== false); ptm_eitaa_log(array('phase'=>'auto_doc_media','http_code'=>$code3,'api_ok'=>$ok3,'post_id'=>$post->ID)); if ($ok3) return; } // اگر همهٔ حالت‌های رسانه شکست خورد → یک پیام متنی واحد $req4 = curl_init("{$gw}/{$token}/sendMessage"); curl_setopt_array($req4, array( CURLOPT_POST => true, CURLOPT_POSTFIELDS => array('chat_id'=>$chat, 'text'=>$caption), CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 15, CURLOPT_TIMEOUT => 30, )); $resp4 = curl_exec($req4); $code4 = curl_getinfo($req4, CURLINFO_HTTP_CODE); curl_close($req4); $ok4 = (strpos((string)$resp4,'"ok":true') !== false); ptm_eitaa_log(array('phase'=>'auto_fallback_text','http_code'=>$code4,'api_ok'=>$ok4,'post_id'=>$post->ID)); return; } // بدون رسانه: تنها یک پیام متنی (تیتر + پاراگراف اول + لینک) $req = curl_init("{$gw}/{$token}/sendMessage"); curl_setopt_array($req, array( CURLOPT_POST => true, CURLOPT_POSTFIELDS => array('chat_id'=>$chat, 'text'=>$caption), CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 15, CURLOPT_TIMEOUT => 30, )); $resp = curl_exec($req); $http = curl_getinfo($req, CURLINFO_HTTP_CODE); curl_close($req); $ok = (strpos((string)$resp,'"ok":true') !== false); ptm_eitaa_log(array('phase'=>'auto_text_only','http_code'=>$http,'api_ok'=>$ok,'post_id'=>$post->ID)); } }