diff --git a/src/Client.php b/src/Client.php index 43f8b7f..e45956c 100644 --- a/src/Client.php +++ b/src/Client.php @@ -10,6 +10,7 @@ namespace EcomPHP\TiktokShop; +use EcomPHP\TiktokShop\Resources\CustomerService; use GuzzleHttp\HandlerStack; use GuzzleHttp\Client as GuzzleHttpClient; use GuzzleHttp\Middleware; @@ -77,6 +78,7 @@ class Client Supplychain::class, Event::class, ReturnRefund::class, + CustomerService::class, ]; public function __construct($app_key, $app_secret, $options = []) diff --git a/src/Resources/CustomerService.php b/src/Resources/CustomerService.php new file mode 100644 index 0000000..464b019 --- /dev/null +++ b/src/Resources/CustomerService.php @@ -0,0 +1,90 @@ + 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, + ] + ]); + } +} diff --git a/tests/Resources/CustomerServiceTest.php b/tests/Resources/CustomerServiceTest.php new file mode 100644 index 0000000..39b5f8d --- /dev/null +++ b/tests/Resources/CustomerServiceTest.php @@ -0,0 +1,65 @@ + 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'); + } +}