Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 240 additions & 0 deletions lib/Intercom/Client/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Intercom\AbstractClient,
Intercom\Exception\UserException,
Intercom\Exception\ConversationException,
Intercom\Object\User as UserObject,
Intercom\Request\Search\UserSearch,
Intercom\Request\PaginatedResponse,
Expand Down Expand Up @@ -130,4 +131,243 @@ public function delete(UserObject $user)

return $this->hydrate($user, $response->json());
}


/**
* Get Users conversations
*
* @param string $userId The userId
* @param string $email The email
*
* @throws HttpClientException
* @throws UserException
*
* @return Array
*/
public function conversations($userId = null, $email = null)
{
if (null === $userId && null === $email) {
throw new UserException('An userId or email must be specified and are mandatory to get a User');
}

$parameters = [];

if (null !== $userId) {
$parameters['user_id'] = $userId;
}

if (null !== $email) {
$parameters['email'] = $email;
}

$response = $this->send(new Request('GET', self::INTERCOM_BASE_URL . '/message_threads', $parameters));
$threadsData = $response->json();

return $threadsData;
}

/**
* Get a User conversation
*
* @param string $userId The userId
* @param string $email The email
* @param string $threadId The thread ID
*
* @throws HttpClientException
* @throws UserException
* @throws ConversationException
*
* @return Array
*/
public function conversation($userId = null, $email = null, $threadId = null)
{
if (null === $userId && null === $email) {
throw new UserException('An userId or email must be specified and are mandatory to get a User');
}

if (null === $threadId) {
throw new ConversationException('An thread ID must be specified and are mandatory to get a conversation');
}

$parameters = [];

if (null !== $userId) {
$parameters['user_id'] = $userId;
}

if (null !== $email) {
$parameters['email'] = $email;
}

if (null !== $threadId) {
$parameters['thread_id'] = $threadId;
}

$response = $this->send(new Request('GET', self::INTERCOM_BASE_URL . '/message_threads', $parameters));
$threadData = $response->json();

return $threadData;
}

/**
* Mark a conversation as read
*
* @param string $userId The userId
* @param string $email The email
* @param string $threadId The thread
*
* @throws HttpClientException
* @throws UserException
* @throws ConversationException
*
* @return Array
*/
public function readConversation($userId = null, $email = null, $threadId = null, $read = true, $url = '')
{
if (null === $userId && null === $email) {
throw new UserException('An userId or email must be specified and are mandatory to get a User');
}

if (null === $threadId) {
throw new ConversationException('An thread ID must be specified and are mandatory to get a conversation');
}

if (null === $read || empty($read)) {
throw new ConversationException('Read cannot be empty or null');
}

$parameters = [];

if (null !== $userId) {
$parameters['user_id'] = $userId;
}

if (null !== $email) {
$parameters['email'] = $email;
}

if (null !== $threadId) {
$parameters['thread_id'] = $threadId;
}

if (null !== $read) {
$parameters['read'] = $read;
}

if (null !== $url || !empty($url)) {
$parameters['url'] = $url;
}

$response = $this->send(new Request('PUT', self::INTERCOM_BASE_URL . '/message_threads', $parameters));
$threadData = $response->json();

return $threadData;
}


/**
* Reply a conversation
*
* @param string $userId The userId
* @param string $email The email
* @param string $threadId The thread
* @param string $body The body
*
* @throws HttpClientException
* @throws UserException
* @throws ConversationException
*
* @return Array
*/
public function replyConversation($userId = null, $email = null, $threadId = null, $body = null, $url = '')
{
if (null === $userId && null === $email) {
throw new UserException('An userId or email must be specified and are mandatory to get a User');
}

if (null === $threadId) {
throw new ConversationException('An thread ID must be specified and are mandatory to get a conversation');
}

if (null === $body || empty($body)) {
throw new ConversationException('Message cannot be empty or null');
}

$parameters = [];

if (null !== $userId) {
$parameters['user_id'] = $userId;
}

if (null !== $email) {
$parameters['email'] = $email;
}

if (null !== $threadId) {
$parameters['thread_id'] = $threadId;
}

if (null !== $body) {
$parameters['body'] = $body;
}

if (null !== $url || !empty($url)) {
$parameters['url'] = $url;
}

$parameters['read'] = true;

$response = $this->send(new Request('PUT', self::INTERCOM_BASE_URL . '/message_threads', $parameters));
$threadData = $response->json();

return $threadData;
}

/**
* Create a user conversation
*
* @param string $userId The userId
* @param string $email The email
* @param string $body The body
*
* @throws HttpClientException
* @throws UserException
* @throws ConversationException
*
* @return Array
*/
public function newConversation($userId = null, $email = null, $body = null, $url = '')
{
if (null === $userId && null === $email) {
throw new UserException('An userId or email must be specified and are mandatory to get a User');
}

if (null === $body || empty($body)) {
throw new ConversationException('Message cannot be empty or null');
}

$parameters = [];

if (null !== $userId) {
$parameters['user_id'] = $userId;
}

if (null !== $email) {
$parameters['email'] = $email;
}

if (null !== $body) {
$parameters['body'] = $body;
}

if (null !== $url || !empty($url)) {
$parameters['url'] = $url;
}

$response = $this->send(new Request('POST', self::INTERCOM_BASE_URL . '/message_threads', $parameters));
$threadData = $response->json();

return $threadData;
}

}
10 changes: 10 additions & 0 deletions lib/Intercom/Exception/ConversationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Intercom\Exception;

/**
* Exception related to Conversation object
*/
class ConversationException extends AbstractIntercomException
{
}