diff --git a/src/InstagramScraper/Endpoints.php b/src/InstagramScraper/Endpoints.php index 32df7fa9..b25b150b 100644 --- a/src/InstagramScraper/Endpoints.php +++ b/src/InstagramScraper/Endpoints.php @@ -37,7 +37,9 @@ class Endpoints const ACCOUNT_MEDIAS2 = 'https://www.instagram.com/graphql/query/?query_id=17880160963012870&id={{accountId}}&first=10&after='; const HIGHLIGHT_URL = 'https://www.instagram.com/graphql/query/?query_hash=c9100bf9110dd6361671f113dd02e7d6&variables={"user_id":"{userId}","include_chaining":false,"include_reel":true,"include_suggested_users":false,"include_logged_out_extras":false,"include_highlight_reels":true,"include_live_status":false}'; const HIGHLIGHT_STORIES = 'https://www.instagram.com/graphql/query/?query_hash=45246d3fe16ccc6577e0bd297a5db1ab'; - const THREADS_URL = 'https://www.instagram.com/direct_v2/web/inbox/?persistentBadging=true&folder=&limit={limit}&thread_message_limit={messageLimit}&cursor={cursor}'; + const THREADS_URL = 'https://i.instagram.com/api/v1/direct_v2/inbox/?persistentBadging=true&folder=&limit={limit}&thread_message_limit={messageLimit}&cursor={cursor}'; + const THREADS_PENDING_REQUESTS_URL = 'https://i.instagram.com/api/v1/direct_v2/pending_inbox/?limit={limit}&cursor={cursor}'; + const THREADS_APPROVE_MULTIPLE_URL = 'https://i.instagram.com/api/v1/direct_v2/threads/approve_multiple/'; // Look alike?? const URL_SIMILAR = 'https://www.instagram.com/graphql/query/?query_id=17845312237175864&id=4663052'; @@ -236,4 +238,19 @@ public static function getThreadsUrl($limit, $messageLimit, $cursor) return $url; } + + public static function getThreadsPendingRequestsUrl($limit, $cursor = null) + { + $url = static::THREADS_PENDING_REQUESTS_URL; + + $url = str_replace('{limit}', $limit, $url); + $url = str_replace('{cursor}', $cursor, $url); + + return $url; + } + + public static function getThreadsApproveMultipleUrl() + { + return static::THREADS_APPROVE_MULTIPLE_URL; + } } diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index 6b262225..2f5a5769 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -2248,12 +2248,18 @@ public function getPaginateThreads($limit = 10, $messageLimit = 10, $cursor = nu */ public function getThreads($count = 10, $limit = 10, $messageLimit = 10) { + $this->allowPendingRequests(); + $threads = []; $cursor = null; while (count($threads) < $count) { $result = $this->getPaginateThreads($limit, $messageLimit, $cursor); + if (!isset($result['threads'])) { + break; + } + $threads = array_merge($threads, $result['threads']); if (!$result['hasOlder'] || !$result['oldestCursor']) { @@ -2265,4 +2271,50 @@ public function getThreads($count = 10, $limit = 10, $messageLimit = 10) return $threads; } + + /** + * @param int $limit + * + * @return void + * @throws InstagramException + */ + public function allowPendingRequests($limit = 10) + { + $response = Request::get( + Endpoints::getThreadsPendingRequestsUrl($limit), + array_merge( + ['x-ig-app-id' => self::X_IG_APP_ID], + $this->generateHeaders($this->userSession) + ) + ); + + if ($response->code !== static::HTTP_OK) { + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + } + + $jsonResponse = $this->decodeRawBodyToJson($response->raw_body); + + if (!isset($jsonResponse['status']) || $jsonResponse['status'] !== 'ok') { + throw new InstagramException('Response code is not equal 200. Something went wrong. Please report issue.'); + } + + if (!isset($jsonResponse['inbox']['threads']) || empty($jsonResponse['inbox']['threads'])) { + return; + } + + $threadIds = []; + + foreach ($jsonResponse['inbox']['threads'] as $thread) { + $threadIds[] = $thread['thread_id']; + } + + Request::post( + Endpoints::getThreadsApproveMultipleUrl(), + array_merge( + ['x-ig-app-id' => self::X_IG_APP_ID], + $this->generateHeaders($this->userSession) + ), + ['thread_ids' => json_encode($threadIds)] + ); + } }