helpscout / helpscout-api-php

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

Create Customer thread from Webhook #252

Closed helgatheviking closed 3 years ago

helgatheviking commented 3 years ago

I had previously been querying for customers by email address on reception of my new conversation webhook. But I've been in talks with support and since the replyto email is set in the initial email, HelpScout is automatically setting that as the customer for the new conversation. That's great as it removes my need to query the customer. But now I am failing to create a new Customer Thread by that initial customer.

$webhook = IncomingWebhook::makeFromGlobals( $secretKey );
$client = ApiClientFactory::createClient();
$client->useClientCredentials( $appId, $appSecret );

$conversation     = $webhook->getConversation();
$conversation_id = $conversation->getId();

$customer           = $webhook->getCustomer();

$customerThread = new CustomerThread();
$customerThread->setCustomer( $customer );
$customerThread->setText( 'tacos are awesome!' );

$client->threads()->create( $conversation_id, $customerThread );

Does IncomingWebhook::getCustomer() return the same type of object required by the CustomerThread::setCustomer() method?

helgatheviking commented 3 years ago

As a followup, I am getting a "bad request" error from $client->threads()->create( $conversation_id, $customerThread );

The error is "Customer with ID 123456789 does not belong to company with ID 12345". Trying to log $customer->getFirstEmail() and $customer->getFirstName() both return null. The customer ID though appears to match the conversation ID.

bkuhl commented 3 years ago

Hey Kathy,

Does IncomingWebhook::getCustomer() return the same type of object required by the CustomerThread::setCustomer() method?

Yep!

The customer ID though appears to match the conversation ID.

Looking at the source of IncomingWebhook::getCustomer() vs ...:getConversation()it looks like both of these hydrate using the json payload. So usinggetCustomer()on an event related to a conversation can cause this error to occur. You could usegetConversation->getCustomer()` to get the info you're looking for.

I think this will resolve this for you, please re-open if it doesn't!

helgatheviking commented 3 years ago

Hi Ben... thanks! I can confirm that $webhook->getConversation->getCustomer() does seem to resolve the issue.