/**
  * @expectedException \HelpScout\ApiException
  */
 public function testCanFailCreateConversationWithMessage()
 {
     $client = $this->getTestClient('CreateConversationWithNewCustomer-400', 'post');
     // The customer associated with the conversation
     $customerRef = $client->getCustomerRefProxy(null, '*****@*****.**');
     $conversation = new \HelpScout\model\Conversation();
     $conversation->setCustomer($customerRef);
     $conversation->setCreatedBy($customerRef);
     $conversation->setMailbox($client->getMailboxProxy(2562));
     // A conversation must have at least one thread
     $thread = new \HelpScout\model\thread\Customer();
     // Create by: required
     $thread->setCreatedBy($customerRef);
     $conversation->addLineItem($thread);
     $client->createConversation($conversation);
 }
<?php

include_once 'ApiClient.php';
use HelpScout\ApiClient;
$client = ApiClient::getInstance();
$client->setKey('example-key');
// The customer associated with the conversation
$customerRef = $client->getCustomerRefProxy(null, '*****@*****.**');
$conversation = new \HelpScout\model\Conversation();
$conversation->setType('email');
$conversation->setSubject('I need help');
$conversation->setCustomer($customerRef);
$conversation->setCreatedBy($customerRef);
// The mailbox associated with the conversation
$conversation->setMailbox($client->getMailboxProxy(2431));
// A conversation must have at least one thread
$thread = new \HelpScout\model\thread\Customer();
$thread->setBody('Hello there - I need some help please.');
// Create by: required
$thread->setCreatedBy($customerRef);
$conversation->addLineItem($thread);
$client->createConversation($conversation);
echo $conversation->getId();
// if the customer id is important to you (for the customer created above),
// grab the newly created convo
$conversation = $client->getConversation($conversation->getId());
$customerId = $conversation->getCreatedBy()->getId();
<?php

use HelpScout\ApiClient;
$client = ApiClient::getInstance();
$client->setKey('example-key');
// The mailbox associated with the conversation
$mailbox = new \HelpScout\model\ref\MailboxRef();
$mailbox->setId(123);
// The customer associated with the conversation
$customer = new \HelpScout\model\ref\CustomerRef();
$customer->setId(12345);
$customer->setEmail('*****@*****.**');
$conversation = new \HelpScout\model\Conversation();
$conversation->setSubject('I need help!');
$conversation->setMailbox($mailbox);
$conversation->setCustomer($customer);
$conversation->setType('email');
// A conversation must have at least one thread
$thread = new \HelpScout\model\thread\Customer();
$thread->setType('customer');
$thread->setBody('Hello. I need some help.');
$thread->setStatus('active');
// Create by: required
$createdBy = new \HelpScout\model\ref\PersonRef();
$createdBy->setId(12345);
$createdBy->setType('customer');
$thread->setCreatedBy($createdBy);
// Assigned to: not required - defaults to 'anyone'
$assignedTo = new \HelpScout\model\ref\PersonRef();
$assignedTo->setId(100);
$assignedTo->setType('user');
<?php

include_once 'ApiClient.php';
use HelpScout\ApiClient;
$client = ApiClient::getInstance();
$client->setKey('example-key');
// In this example, I want to start a new conversation that will get
// emailed to the customer
// 1. First need to decide who I'm sending it to.
// All I have is an email address. This may or may not be an existing customer.
// Either way, Help Scout will create the customer if the customer does not yet exist.
$customerRef = $client->getCustomerRefProxy(null, '*****@*****.**');
// 2. Decide which mailbox this conversation will be created in
$mailboxRef = $client->getMailboxProxy(2431);
// 3. Now let's start constructing the conversation
$conversation = new \HelpScout\model\Conversation();
$conversation->setSubject('Thanks for contacting us');
$conversation->setMailbox($mailboxRef);
$conversation->setCustomer($customerRef);
// 4. Let's set the conversation type to "email" (as opposed to a chat or phone call)
$conversation->setType('email');
// 5. Every conversation MUST HAVE at least one thread.
// To send an email to the customer, the thread type must be a "Message" thread
$thread = new \HelpScout\model\thread\Message();
$thread->setBody('Hey there - sorry to hear you\'ve had trouble using our product. We\'ve contacted an engineer and he will be touching base shortly');
// 6. Now, we have to say "who" created the message. Message threads can only be created by
// registered users of Help Scout. So it must be from soneone on your team.
// 6.1 You could use the person associated with the current API key:
// $userRef = $client->getUserMe()->toRef();
// 6.2 You could use a specific user
$userRef = $client->getUserRefProxy(1234);
 /**
  * Process feed, create conversation.
  * 
  * @access public
  * @param array $feed The feed object to be processed.
  * @param array $entry The entry object currently being processed.
  * @param array $form The form object currently being processed.
  * @return void
  */
 public function process_feed($feed, $entry, $form)
 {
     /* If Help Scout instance is not initialized, exit. */
     if (!$this->initialize_api()) {
         $this->log_error(__METHOD__ . '(): Failed to set up the API.');
         return;
     }
     /* If this entry already has a Help Scout conversation, exit. */
     if (gform_get_meta($entry['id'], 'helpscout_conversation_id')) {
         $this->log_debug(__METHOD__ . '(): Entry already has a Help Scout conversation associated to it. Skipping processing.');
         return;
     }
     /* Prepare needed information. */
     $data = array('email' => $this->get_field_value($form, $entry, $feed['meta']['customer_email']), 'first_name' => $this->get_field_value($form, $entry, $feed['meta']['customer_first_name']), 'last_name' => $this->get_field_value($form, $entry, $feed['meta']['customer_last_name']), 'subject' => GFCommon::replace_variables($feed['meta']['subject'], $form, $entry, false, false, false, 'text'), 'body' => GFCommon::replace_variables($feed['meta']['body'], $form, $entry), 'attachments' => array(), 'tags' => GFCommon::replace_variables($feed['meta']['tags'], $form, $entry));
     /* If the email address is empty, exit. */
     if (GFCommon::is_invalid_or_empty_email($data['email'])) {
         $this->log_error(__METHOD__ . "(): Email address must be provided.");
         return false;
     }
     /* Setup the mailbox for this conversation */
     $mailbox = new \HelpScout\model\ref\MailboxRef();
     $mailbox->setId($feed['meta']['mailbox']);
     /* Create the customer object */
     $customer = $this->api->getCustomerRefProxy(null, $data['email']);
     $customer->setFirstName($data['first_name']);
     $customer->setLastName($data['last_name']);
     /* Create the conversation object */
     $conversation = new \HelpScout\model\Conversation();
     $conversation->setSubject($data['subject']);
     $conversation->setMailbox($mailbox);
     $conversation->setCustomer($customer);
     $conversation->setCreatedBy($customer);
     $conversation->setType($feed['meta']['type']);
     /* Create the message thread */
     if (gf_apply_filters('gform_helpscout_process_body_shortcodes', $form['id'], false, $form, $feed)) {
         $data['body'] = do_shortcode($data['body']);
     }
     $thread = new \HelpScout\model\thread\Customer();
     $thread->setCreatedBy($customer);
     $thread->setBody($data['body']);
     $thread->setStatus($feed['meta']['status']);
     /* Assign this conversation to user if set */
     if (!rgempty('user', $feed['meta'])) {
         $user = new \HelpScout\model\ref\PersonRef();
         $user->setId($feed['meta']['user']);
         $user->setType('user');
         $thread->setAssignedTo($user);
     }
     /* If feed has an attachments field assign, process the attachments. */
     if (!empty($feed['meta']['attachments'])) {
         $attachment_fields = array_keys($feed['meta']['attachments']);
         $attachment_files = array();
         foreach ($attachment_fields as $attachment_field) {
             $field_value = $this->get_field_value($form, $entry, $attachment_field);
             $field_value = $this->is_json($field_value) ? json_decode($field_value, true) : $field_value;
             $field_value = strpos($field_value, ' , ') !== FALSE ? explode(' , ', $field_value) : $field_value;
             if (empty($field_value)) {
                 continue;
             }
             if (is_array($field_value)) {
                 $attachment_files = array_merge($attachment_files, $field_value);
             } else {
                 $attachment_files[] = $field_value;
             }
         }
         if (!empty($attachment_files)) {
             $attachments = $this->process_feed_attachments($attachment_files);
             $thread->setAttachments($attachments);
         }
     }
     /* Add tags to conversation */
     $tags = !empty($data['tags']) ? array_map('trim', explode(',', $data['tags'])) : array();
     $tags = gf_apply_filters('gform_helpscout_tags', $form['id'], $tags, $feed, $entry, $form);
     if (!empty($tags)) {
         $conversation->setTags($tags);
     }
     /* Add CC and BCC support if set. */
     if (isset($feed['meta']['cc'])) {
         $data['cc'] = GFCommon::replace_variables($feed['meta']['cc'], $form, $entry);
         $data['cc'] = is_array($data['cc']) ? $data['cc'] : explode(',', $data['cc']);
         if (!empty($data['cc'])) {
             $thread->setCcList($data['cc']);
         }
     }
     if (isset($feed['meta']['bcc'])) {
         $data['bcc'] = GFCommon::replace_variables($feed['meta']['bcc'], $form, $entry);
         $data['bcc'] = is_array($data['bcc']) ? $data['bcc'] : explode(',', $data['bcc']);
         if (!empty($data['bcc'])) {
             $thread->setCcList($data['bcc']);
         }
     }
     /* Assign the message thread to the conversation */
     $conversation->setThreads(array($thread));
     /* Set thread count to 1 so Help Scout will include the conversation in the mailbox folder count */
     $conversation->setThreadCount(1);
     $this->log_debug(__METHOD__ . "(): Conversation to be created => " . print_r($conversation, true));
     try {
         $auto_reply = rgars($feed, 'meta/auto_reply') == '1';
         /* Create the conversation. */
         $this->api->createConversation($conversation, false, $auto_reply, true);
         gform_update_meta($entry['id'], 'helpscout_conversation_id', $conversation->getId());
         /* Log that conversation was created. */
         $this->log_debug(__METHOD__ . "(): Conversation has been created.");
     } catch (Exception $e) {
         /* Log that conversation was not created. */
         $this->log_error(__METHOD__ . "(): Conversation was not created; {$e->getMessage()}");
         return;
     }
     /* Add conversation note if set. */
     if (rgars($feed, 'meta/note')) {
         /* Replace variables for note. */
         $note_text = GFCommon::replace_variables($feed['meta']['note'], $form, $entry);
         /* Get API user. */
         $api_user = $this->api->getUserMe();
         /* Create note object. */
         $note = new \HelpScout\model\thread\Message();
         $note->setCreatedBy($this->api->getUserRefProxy($api_user->getId()));
         $note->setBody($note_text);
         $note->setType('note');
         try {
             /* Post note to conversation. */
             $this->api->createThread($conversation->getId(), $note);
             /* Log that note was added. */
             $this->log_debug(__METHOD__ . '(): Note was successfully added to conversation.');
         } catch (Exception $e) {
             /* Log that note was not added. */
             $this->log_error(__METHOD__ . '(): Note was not added to conversation; ' . $e->getMessage());
             return;
         }
     }
 }
             $attachment->setMimeType('image/jpeg');
         }
         $attachment->setFileName(filename_from_url($url));
         $attachment->setData(file_get_contents($url));
         $client->createAttachment($attachment);
         $attachments[] = $attachment;
     }
 }
 // Create the customer
 $customer = new \HelpScout\model\ref\CustomerRef();
 $customer->setId(null);
 $customer->setEmail($row['email']);
 $customer->setFirstName($row['f_name']);
 $customer->setLastName($row['l_name']);
 // Create the conversation
 $conversation = new \HelpScout\model\Conversation();
 $conversation->setSubject($row['reason'] . ': ' . $row['subject']);
 $conversation->setCreatedAt(format_api_date($row['date']));
 $conversation->setMailbox($mailbox);
 $conversation->setCustomer($customer);
 $conversation->setType('email');
 $tags = array('backlog', strtolower($row['reason']));
 if ($row['extension'] == 'Yes') {
     $tags[] = strtolower($row['extension_name']);
 }
 $conversation->setTags($tags);
 // A conversation must have at least one thread
 $thread = new \HelpScout\model\thread\Customer();
 $thread->setCreatedAt(format_api_date($row['date']));
 $body = '<strong>Website: </strong>' . $row['website'] . "\r\n";
 if ($row['extension'] == 'Yes') {