helpscout / helpscout-api-php

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

How to get the content of a conversation? #307

Closed csuard closed 1 year ago

csuard commented 1 year ago

I would like to know how to get the content of a conversation. Is it possible? Actually, I am able to get the preview and the subject of the mail but I can't find how to get the content of the mail. Do you know a way to get it?

Thanks a lot!

miguelrs commented 1 year ago

Hey @CorentinSUTT!

A Conversation itself doesn't contain any text, but it contains a list of Threads, which represent the different items within the conversation timeline (customer replies, notes, etc). The first thread in the list represents the last reply, and the last thread represents the email that initiated the conversation.

If you already fetched a Conversation object, it provides a getThreads() method that returns all the threads that were eagerly loaded when fetching the conversation.

Alternatively, you can directly fetch the paginated threads providing a conversation ID, as you can see in examples/threads.php.

Once you have the Thread objects, you can call getText() to get the content.

Hope this helps!

csuard commented 1 year ago

Thanks a lot for your answer! And how do I get only the latest thread of the conversation?

miguelrs commented 1 year ago

@CorentinSUTT

Threads are by default sorted by createdAt (from newest to oldest), which means if you want to get the most recent thread, you can just fetch the Conversation and get the first item from the list of threads:

$request = (new ConversationRequest())->withThreads();
$conversation = $client->conversations()->get($conversationId, $request);
$mostRecent = $conversation->getThreads()[0]

If you want to get the oldest thread (the initial email that started the conversation), and you want your solution to work for any number of threads, then you will need to fetch the Threads directly, and then get the last one:

$threads = $client->threads()->list($conversationId)->getLastPage();
$oldestThread = end($threads);
csuard commented 1 year ago

Hi,

Thanks a lot for all your help.

I still have a little issue. I made the following code: $request = (new ConversationRequest())->withThreads(); $conversation = $client->conversations()->get($id, $request); $mostRecent = $conversation->getThreads()[0]; $content = $mostRecent->getText();

Sometimes, it works. Sometimes, it gives me NULL as content. Do you know where it can come from?