Skip to content

Commit

Permalink
Merge pull request #9 from sendbee/feature/conversation-update-folder
Browse files Browse the repository at this point in the history
conversation: get data and update done|open
  • Loading branch information
elfle authored Sep 22, 2021
2 parents 067ed4f + bd8e4c3 commit 55f963f
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#### Conversations

- [Fetch conversations](#fetch-conversations)
- [Fetch single conversation](#fetch-single-conversation)
- [Update single conversation](#update-single-conversation)
- [Fetch messages](#fetch-messages)

#### Teams
Expand Down Expand Up @@ -754,6 +756,117 @@ if ($response->isSuccess()) {
}
```

### <a name='fetch-single-conversation'>Fetch single conversation</a>

```php
// parameters
$params = [
// Conversation UUID, MANDATORY
'conversation_id' => '...'
];

try {
$response = $sendbeeApi->getConversation($params);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when required data was not provided
echo "Missing required data. ", $ex->getMessage();
} catch (\Exception $ex) {
// handle exception thrown by GuzzleHttp
// this is most likely due to a network issue
echo "Could not contact backend endpoint. ", $ex->getMessage();
}


if ($response->isSuccess()) {
// everything is OK
/**
* @var $conversation \Sendbee\Api\Models\Conversation
*/
$conversation = $response->getData();

echo "\n ID: ", $conversation->id;
echo "\n folder: ", $conversation->folder;
echo "\n chatbot_active: ", $conversation->chatbot_active;
echo "\n platform: ", $conversation->platform;
echo "\n created_at: ", $conversation->created_at;

echo "\n contact -> id: ", $conversation->contact->id;
echo "\n contact -> name: ", $conversation->contact->name;
echo "\n contact -> phone: ", $conversation->contact->phone;

echo "\n last_message -> direction: ", $conversation->last_message->direction;
echo "\n last_message -> status: ", $conversation->last_message->status;
echo "\n last_message -> inbound_sent_at: ", $conversation->last_message->inbound_sent_at;
echo "\n last_message -> outbound_sent_at: ", $conversation->last_message->outbound_sent_at;
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
```


#update-single-conversation
### <a name='fetch-messages'>Update a single conversation</a>

```php
// parameters
$params = [
// Conversation UUID, MANDATORY
'conversation_id' => '...',
// Assigned "folder" - 'open' or 'done'
'folder' => 'open|done'
];

try {
$response = $sendbeeApi->updateConversation($params);
} catch (\Sendbee\Api\Support\DataException $ex) {
// handle missing data
// this happens when required data was not provided
echo "Missing required data. ", $ex->getMessage();
} catch (\Exception $ex) {
// handle exception thrown by GuzzleHttp
// this is most likely due to a network issue
echo "Could not contact backend endpoint. ", $ex->getMessage();
}


if ($response->isSuccess()) {
// everything is OK
$data = $response->getData();

foreach ($data as $message) {
/**
* @var $message \Sendbee\Api\Models\Message
*/
echo "\n body: ", $message->body;
echo "\n media_type: ", $message->media_type;
echo "\n media_url: ", $message->media_url;
echo "\n status: ", $message->status;
echo "\n direction: ", $message->direction;
echo "\n sent_at: ", $message->sent_at;

}
} else {
/**
* @var $error \Sendbee\Api\Transport\ResponseError
*/
$error = $response->getError();
if ($error) {
echo "\n error type: ", $error->type;
echo "\n error details: ", $error->detail;
}
}
```



### <a name='fetch-messages'>Fetch messages in a conversation</a>

```php
Expand Down
45 changes: 45 additions & 0 deletions src/Sendbee/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,51 @@ public function getConversations($params = [])
return $this->makeRequest('conversations', self::GET, $query, [], Conversation::class);
}

/**
* Get conversations
*
* @param array $params
* @return Sendbee\Api\Transport\Response|string
* @throws GuzzleException
* @throws DataException
*/
public function getConversation($params = [])
{
$requiredKeys = ['conversation_id'];
$this->requireKeys($requiredKeys, $params);

$validParams = [
'conversation_id', // Conversation UUID
];

$query = $this->filterKeys($validParams, $params);

return $this->makeRequest('conversation', self::GET, $query, [], Conversation::class);
}

/**
* Get conversations
*
* @param array $params
* @return Sendbee\Api\Transport\Response|string
* @throws GuzzleException
* @throws DataException
*/
public function updateConversation($params = [])
{
$requiredKeys = ['conversation_id'];
$this->requireKeys($requiredKeys, $params);

$validParams = [
'conversation_id', // Conversation UUID
'folder', // Folder to assign conversation to - open|done
];

$data = $this->filterKeys($validParams, $params);

return $this->makeRequest('/conversation', self::POST, [], $data, Conversation::class);
}

/**
* Get messages in a conversation
*
Expand Down

0 comments on commit 55f963f

Please sign in to comment.