Skip to content

Commit c3de997

Browse files
author
Alexander Tarkhanov
authored
allow pending requests (#868)
1 parent cdcb044 commit c3de997

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

src/InstagramScraper/Endpoints.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class Endpoints
3737
const ACCOUNT_MEDIAS2 = 'https://www.instagram.com/graphql/query/?query_id=17880160963012870&id={{accountId}}&first=10&after=';
3838
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}';
3939
const HIGHLIGHT_STORIES = 'https://www.instagram.com/graphql/query/?query_hash=45246d3fe16ccc6577e0bd297a5db1ab';
40-
const THREADS_URL = 'https://www.instagram.com/direct_v2/web/inbox/?persistentBadging=true&folder=&limit={limit}&thread_message_limit={messageLimit}&cursor={cursor}';
40+
const THREADS_URL = 'https://i.instagram.com/api/v1/direct_v2/inbox/?persistentBadging=true&folder=&limit={limit}&thread_message_limit={messageLimit}&cursor={cursor}';
41+
const THREADS_PENDING_REQUESTS_URL = 'https://i.instagram.com/api/v1/direct_v2/pending_inbox/?limit={limit}&cursor={cursor}';
42+
const THREADS_APPROVE_MULTIPLE_URL = 'https://i.instagram.com/api/v1/direct_v2/threads/approve_multiple/';
4143

4244
// Look alike??
4345
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)
236238

237239
return $url;
238240
}
241+
242+
public static function getThreadsPendingRequestsUrl($limit, $cursor = null)
243+
{
244+
$url = static::THREADS_PENDING_REQUESTS_URL;
245+
246+
$url = str_replace('{limit}', $limit, $url);
247+
$url = str_replace('{cursor}', $cursor, $url);
248+
249+
return $url;
250+
}
251+
252+
public static function getThreadsApproveMultipleUrl()
253+
{
254+
return static::THREADS_APPROVE_MULTIPLE_URL;
255+
}
239256
}

src/InstagramScraper/Instagram.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,12 +2248,18 @@ public function getPaginateThreads($limit = 10, $messageLimit = 10, $cursor = nu
22482248
*/
22492249
public function getThreads($count = 10, $limit = 10, $messageLimit = 10)
22502250
{
2251+
$this->allowPendingRequests();
2252+
22512253
$threads = [];
22522254
$cursor = null;
22532255

22542256
while (count($threads) < $count) {
22552257
$result = $this->getPaginateThreads($limit, $messageLimit, $cursor);
22562258

2259+
if (!isset($result['threads'])) {
2260+
break;
2261+
}
2262+
22572263
$threads = array_merge($threads, $result['threads']);
22582264

22592265
if (!$result['hasOlder'] || !$result['oldestCursor']) {
@@ -2265,4 +2271,50 @@ public function getThreads($count = 10, $limit = 10, $messageLimit = 10)
22652271

22662272
return $threads;
22672273
}
2274+
2275+
/**
2276+
* @param int $limit
2277+
*
2278+
* @return void
2279+
* @throws InstagramException
2280+
*/
2281+
public function allowPendingRequests($limit = 10)
2282+
{
2283+
$response = Request::get(
2284+
Endpoints::getThreadsPendingRequestsUrl($limit),
2285+
array_merge(
2286+
['x-ig-app-id' => self::X_IG_APP_ID],
2287+
$this->generateHeaders($this->userSession)
2288+
)
2289+
);
2290+
2291+
if ($response->code !== static::HTTP_OK) {
2292+
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
2293+
}
2294+
2295+
$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);
2296+
2297+
if (!isset($jsonResponse['status']) || $jsonResponse['status'] !== 'ok') {
2298+
throw new InstagramException('Response code is not equal 200. Something went wrong. Please report issue.');
2299+
}
2300+
2301+
if (!isset($jsonResponse['inbox']['threads']) || empty($jsonResponse['inbox']['threads'])) {
2302+
return;
2303+
}
2304+
2305+
$threadIds = [];
2306+
2307+
foreach ($jsonResponse['inbox']['threads'] as $thread) {
2308+
$threadIds[] = $thread['thread_id'];
2309+
}
2310+
2311+
Request::post(
2312+
Endpoints::getThreadsApproveMultipleUrl(),
2313+
array_merge(
2314+
['x-ig-app-id' => self::X_IG_APP_ID],
2315+
$this->generateHeaders($this->userSession)
2316+
),
2317+
['thread_ids' => json_encode($threadIds)]
2318+
);
2319+
}
22682320
}

0 commit comments

Comments
 (0)