-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaginate_messages.php
More file actions
53 lines (41 loc) · 1.33 KB
/
paginate_messages.php
File metadata and controls
53 lines (41 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types=1);
/**
* Paginate through every message in a thread.
*
* The `lastMessageHash` query parameter is a cursor: pass the hash of
* the last message you've already seen to get the next batch. The
* Customer API returns messages in chronological order; the loop
* below stops when an empty page comes back.
*
* Run with:
* STROMCOM_TOKEN=… STROMCOM_THREAD=order-1234 php examples/paginate_messages.php
*/
require __DIR__ . '/../vendor/autoload.php';
use Stromcom\Sdk\Client;
$token = getenv('STROMCOM_TOKEN') ?: '';
$threadCode = getenv('STROMCOM_THREAD') ?: '';
if ($token === '' || $threadCode === '') {
fwrite(STDERR, "Set STROMCOM_TOKEN and STROMCOM_THREAD.\n"); exit(1);
}
$stromcom = new Client($token);
$cursor = null;
$total = 0;
do {
$page = $stromcom->messages()->list($threadCode, lastMessageHash: $cursor);
$count = count($page->messages);
$total += $count;
foreach ($page->messages as $msg) {
printf("%-12s %s\n", $msg->hash, strip_tags($msg->message));
}
if ($count === 0) {
break;
}
$cursor = $page->messages[$count - 1]->hash;
// First-page-only single fetch when the API serves the whole thread
// in one shot — guard against an infinite loop on small threads.
if ($count < 50) {
break;
}
} while (true);
printf("\nTotal messages walked: %d\n", $total);