<?php use HelpScout\ApiClient; $client = ApiClient::getInstance(); $client->setKey('example-key'); $conversation = $client->getConversation(1); $conversation->setSubject('New Subject'); $conversation->setStatus('pending'); // Change the owner of the conversation $owner = new \HelpScout\model\ref\PersonRef(); $owner->setId(100); $owner->setType('user'); $conversation->setOwner($owner); // Change the mailbox of the conversation $mailbox = new \HelpScout\model\ref\MailboxRef(); $mailbox->setId(1); $conversation->setMailbox($mailbox); // Update the conversation tags. Existing tags not // in this list will be deleted. $conversation->setTags(array('tag1', 'tag2')); $client->updateConversation($conversation);
/** * 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; } } }
/** * Get MailboxRef for the current Mailbox object * * @return \HelpScout\model\ref\MailboxRef */ public function toRef() { $ref = new \HelpScout\model\ref\MailboxRef(); $ref->setId($this->getId()); $ref->setName($this->getName()); return $ref; }
<?php require_once 'init.php'; $backlog = (include 'uploads/backlog-current.php'); use HelpScout\ApiClient; $client = ApiClient::getInstance(); $client->setKey(HS_API_KEY); // The mailbox associated with the conversation $mailbox = new \HelpScout\model\ref\MailboxRef(); $mailbox->setId(49350); $failed = array(); foreach ($backlog as $key => $row) { try { // Attachments: attachments must be sent to the API before they can // be used when creating a thread. Use the hash value returned when // creating the attachment to associate it with a ticket. $attachments = array(); if (!empty($row['attachment'])) { foreach (explode(' , ', $row['attachment']) as $url) { $attachment = new HelpScout\model\Attachment(); if (endswith($url, '.png')) { $attachment->setMimeType('image/png'); } if (endswith($url, '.jpg') || endswith($url, '.jpeg')) { $attachment->setMimeType('image/jpeg'); } $attachment->setFileName(filename_from_url($url)); $attachment->setData(file_get_contents($url)); $client->createAttachment($attachment); $attachments[] = $attachment; }