helpscout / helpscout-api-php

PHP Wrapper for the Help Scout API
MIT License
98 stars 62 forks source link

How to get Conversation ID? #288

Closed yaphawhann closed 8 months ago

yaphawhann commented 2 years ago

This is not an issue, but I need help.

After calling:

$conversations = $client->conversations()->list($filters); echo json_encode($conversations);

All I get was:

{"0":{}}

How do I get the conversation ID?

bkuhl commented 2 years ago

👋 It looks like the $filters aren't matching any conversations which is the result set is empty. You can adjust your filters to provide some Conversations provided there are some within the mailbox you're working with. Once you're seeing conversations there, you can follow this example for how to access the results.

yaphawhann commented 2 years ago

I removed all possible filters, as below:

<?php
require __DIR__ . '/vendor/autoload.php';
require '_credentials.php';

use HelpScout\Api\ApiClientFactory;
use HelpScout\Api\Conversations\Conversation;
use HelpScout\Api\Conversations\ConversationFilters;
use HelpScout\Api\Conversations\ConversationRequest;
use HelpScout\Api\Conversations\CustomField;
use HelpScout\Api\Conversations\Threads\ChatThread;
use HelpScout\Api\Conversations\Threads\PhoneThread;
use HelpScout\Api\Tags\Tag;
use HelpScout\Api\Customers\Customer;
use HelpScout\Api\Entity\Collection;

    //Creating Client Object
$client = ApiClientFactory::createClient();
$client = $client->useClientCredentials(APP_ID, APP_SECRET);

// List conversations
$conversations = $client->conversations()
    ->list()
    ->getFirstPage()
    ->toArray();

$filters = (new ConversationFilters())
    ->inMailbox(8089)
    ->inStatus('all')
    ->sortField('createdAt')
    ->sortOrder('asc');

$conversations = $client->conversations()->list($filters);
echo json_encode($conversations);
?>

And it returned this:

{"0":{},"1":{},"2":{},"3":{},"4":{},"5":{},"6":{},"7":{},"8":{},"9":{},"10":{},"11":{},"12":{},"13":{},"14":{},"15":{},"16":{},"17":{},"18":{},"19":{},"20":{},"21":{},"22":{},"23":{},"24":{}}

Can't figure what I did wrong... will appreciate some/any help.

bkuhl commented 2 years ago

I think it's querying the results successfully, which is why it's showing there's 24 empty objects in that last comment. All of the properties on Conversation are private which is why I think it's coming through as an empty object in the output. I suspect if you iterated over them and echoed the data, you'd be able to access the data you're looking for.

foreach ($conversations as $conversation) {
    echo $conversation->getId().PHP_EOL;
}
miguelrs commented 8 months ago

Closing this issue due to inactivity. Please feel free to reopen if needed!