helpscout / helpscout-api-php

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

Is it possible to start a conversation with a customer? #297

Closed JosephGabito closed 1 year ago

JosephGabito commented 1 year ago

Hello! We know that we can create a new conversation as if the customer sent an email to our mailbox. This is working well, but we're wondering if it is possible to create a new conversation to be sent to the customer making us the conversation starter?

Something that behaves like the following form:

Screenshot 2022-11-16 at 9 29 17 PM

Thanks!

jchristopher commented 1 year ago

Hi @JosephGabito would you be able to post your solution to this?

jchristopher commented 1 year ago

Ah ha! I poked around a bit more and the key is to use ReplyThread not CustomerThread and then publishing a Reply draft.

Here's an adaptation of the example:

/**
 * Create Conversation: Initiate Conversation with Customer
 */
$customer = new Customer();
$customer->addEmail('my-customer@company.com');

$thread = new \HelpScout\Api\Conversations\Threads\ReplyThread();
$thread->setCustomer($customer);
$thread->setText('Test');

$conversation = new Conversation();
$conversation->setSubject('Testing the PHP SDK v2: Phone Thread');
$conversation->setStatus('active');
$conversation->setType('email');
$conversation->setMailboxId(80261);
$conversation->setCustomer($customer);
$conversation->makeDraft();
$conversation->setThreads(new Collection([
    $thread,
]));
try {
    $conversationId = $client->conversations()->create($conversation);
    $client->conversations()->publishDraft($conversationId);
} catch (\HelpScout\Api\Exception\ValidationErrorException $e) {
    var_dump($e->getError()->getErrors());
}
JosephGabito commented 1 year ago

@jchristopher, we're directly sending an HTTP request to Helpscout's API, without utilizing the SDK. However, I believe we've arrived at a similar solution.

Here is an example array that gets encoded into a JSON string and then sent to Helpscout's API:

$json_body = array(
    'subject'   => $subject,
    'customer'  => array(
        'email' => $email,
    ),
    'mailboxId' => $mailbox,
    'type'      => 'email',
    'status'    => $status,
    'threads'   => array(
         array(
            'type'     => $type,
        'customer' => array(
            'email' => $email,
        ),
        'text'     => $body,
     ),
    ),
);

We simply change the threads[['type']] to 'reply'. :)

jchristopher commented 1 year ago

Thanks so much for taking the time to reply @JosephGabito! Are you still using the SDK to get the Bearer token to send your requests? If so do you have a snippet of that? I'm trying to avoid dealing with oAuth if at all possible, but wrestling with how to get the token from the SDK.

jchristopher commented 1 year ago

Turns out we can get the Authorization header with $client->getAuthenticator()->getAuthHeader() 👍