Skip to content

Commit

Permalink
add: customer service api
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin committed Feb 27, 2024
1 parent a3cf97c commit 18b182b
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace EcomPHP\TiktokShop;

use EcomPHP\TiktokShop\Resources\CustomerService;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Client as GuzzleHttpClient;
use GuzzleHttp\Middleware;
Expand Down Expand Up @@ -77,6 +78,7 @@ class Client
Supplychain::class,
Event::class,
ReturnRefund::class,
CustomerService::class,
];

public function __construct($app_key, $app_secret, $options = [])
Expand Down
90 changes: 90 additions & 0 deletions src/Resources/CustomerService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/*
* This file is part of tiktokshop-client.
*
* Copyright (c) 2024 Jin <[email protected]> All rights reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace EcomPHP\TiktokShop\Resources;

use EcomPHP\TiktokShop\Resource;
use GuzzleHttp\RequestOptions;

class CustomerService extends Resource
{
protected $category = 'customer_service';

public function getConversationMessages($conversation_id, $params = [])
{
$params = array_merge([
'page_size' => 20,
], $params);

return $this->call('GET', 'conversations/'.$conversation_id.'/messages', [
RequestOptions::QUERY => $params,
]);
}

public function getConversations($params = [])
{
$params = array_merge([
'page_size' => 20,
], $params);

return $this->call('GET', 'conversations', [
RequestOptions::QUERY => $params,
]);
}

public function sendMessage($conversation_id, $type, $content)
{
return $this->call('POST', 'conversations/'.$conversation_id.'/messages', [
RequestOptions::JSON => [
'type' => $type,
'content' => is_array($content) ? json_encode($content) : $content,
]
]);
}

public function getAgentSettings()
{
return $this->call('GET', 'agents/settings');
}

public function updateAgentSettings($body = [])
{
return $this->call('PUT', 'agents/settings', [
RequestOptions::JSON => $body,
]);
}

public function uploadBuyerMessagesImage($image)
{
return $this->call('POST', 'images/upload', [
RequestOptions::MULTIPART => [
[
'name' => 'data',
'filename' => 'image',
'contents' => static::dataTypeCast('image', $image),
]
]
]);
}

public function readMessage($conversation_id)
{
return $this->call('POST', 'conversations/'.$conversation_id.'/messages/read');
}

public function createConversation($buyer_user_id)
{
return $this->call('POST', 'conversations', [
RequestOptions::JSON => [
'buyer_user_id' => $buyer_user_id,
]
]);
}
}
65 changes: 65 additions & 0 deletions tests/Resources/CustomerServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/*
* This file is part of tiktokshop-client.
*
* Copyright (c) 2024 Jin <[email protected]> All rights reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace EcomPHP\TiktokShop\Tests\Resources;

use EcomPHP\TiktokShop\Resources\CustomerService;
use EcomPHP\TiktokShop\Tests\TestResource;

class CustomerServiceTest extends TestResource
{
public function testGetConversationMessages()
{
$this->caller->getConversationMessages('conversation_id');
$this->assertPreviousRequest('GET', 'customer_service/'.TestResource::TEST_API_VERSION.'/conversations/conversation_id/messages');
}

public function testGetConversations()
{
$this->caller->getConversations();
$this->assertPreviousRequest('GET', 'customer_service/'.TestResource::TEST_API_VERSION.'/conversations');
}

public function testSendMessage()
{
$this->caller->sendMessage('conversation_id', 'type', 'content');
$this->assertPreviousRequest('POST', 'customer_service/'.TestResource::TEST_API_VERSION.'/conversations/conversation_id/messages');
}

public function testGetAgentSettings()
{
$this->caller->getAgentSettings();
$this->assertPreviousRequest('GET', 'customer_service/'.TestResource::TEST_API_VERSION.'/agents/settings');
}

public function testUpdateAgentSettings()
{
$this->caller->updateAgentSettings();
$this->assertPreviousRequest('PUT', 'customer_service/'.TestResource::TEST_API_VERSION.'/agents/settings');
}

public function testUploadBuyerMessagesImage()
{
$this->caller->uploadBuyerMessagesImage('php://memory');
$this->assertPreviousRequest('POST', 'customer_service/'.TestResource::TEST_API_VERSION.'/images/upload');
}

public function testReadMessage()
{
$this->caller->readMessage('conversation_id');
$this->assertPreviousRequest('POST', 'customer_service/'.TestResource::TEST_API_VERSION.'/conversations/conversation_id/messages/read');
}

public function testCreateConversation()
{
$this->caller->createConversation('buyer_id');
$this->assertPreviousRequest('POST', 'customer_service/'.TestResource::TEST_API_VERSION.'/conversations');
}
}

0 comments on commit 18b182b

Please sign in to comment.