diff --git a/examples/chatchannels-create.php b/examples/chatchannels-create.php new file mode 100644 index 00000000..9d825b92 --- /dev/null +++ b/examples/chatchannels-create.php @@ -0,0 +1,33 @@ +name = 'Test Channel Telegram'; +$ChatChannel->platformId = 'e82d332c5649a5f911e569n69040697'; + +// Channel details is a hash with name-value pairs indicating which channel details (and their respective data types) +// are required when creating a channel for this platform. + +$ChatChannel->channelDetails = + array( + 'botName' => 'testBot', + 'token' => '1234566778:A34JT44Yr4amk234352et5hvRnHeAEHA' + ); + +try { + $ChatChannelResult = $MessageBird->chatchannels->create($ChatChannel); + var_dump($ChatChannelResult); + +} catch (\MessageBird\Exceptions\AuthenticateException $e) { + // That means that your accessKey is unknown + echo 'wrong login'; + +} catch (\Exception $e) { + echo $e->getMessage(); +} diff --git a/examples/chatchannels-delete.php b/examples/chatchannels-delete.php new file mode 100644 index 00000000..b26ee67e --- /dev/null +++ b/examples/chatchannels-delete.php @@ -0,0 +1,18 @@ +chatchannels->delete('4affac2c577fb22e373921n52675409'); // Set a message id here + var_dump('Deleted: ' . $deleted); + +} catch (\MessageBird\Exceptions\AuthenticateException $e) { + // That means that your accessKey is unknown + echo 'wrong login'; + +} catch (\Exception $e) { + echo $e->getMessage(); +} diff --git a/examples/chatchannels-list.php b/examples/chatchannels-list.php new file mode 100644 index 00000000..766b462a --- /dev/null +++ b/examples/chatchannels-list.php @@ -0,0 +1,17 @@ +chatchannels->getList(); + var_dump($ChatChannelResult); + +} catch (\MessageBird\Exceptions\AuthenticateException $e) { + // That means that your accessKey is unknown + echo 'wrong login'; + +} catch (\Exception $e) { + echo $e->getMessage(); +} diff --git a/examples/chatchannels-update.php b/examples/chatchannels-update.php new file mode 100644 index 00000000..5f2c9d01 --- /dev/null +++ b/examples/chatchannels-update.php @@ -0,0 +1,23 @@ +name = 'New name'; +$ChatChannel->callbackUrl = 'http://newurl.dev'; + + +try { + + $ChatChannelResult = $MessageBird->chatchannels->update($ChatChannel, '331af4c577e3asbbc3631455680736'); + var_dump($ChatChannelResult); + +} catch (\MessageBird\Exceptions\AuthenticateException $e) { + // That means that your accessKey is unknown + echo 'wrong login'; + +} catch (\Exception $e) { + echo $e->getMessage(); +} diff --git a/examples/chatchannels-view.php b/examples/chatchannels-view.php new file mode 100644 index 00000000..aefd99aa --- /dev/null +++ b/examples/chatchannels-view.php @@ -0,0 +1,17 @@ +chatchannels->read('0051af4c577e3eebbc3631n95680736'); // Set a channel id here + var_dump($ChatChannelResult); + +} catch (\MessageBird\Exceptions\AuthenticateException $e) { + // That means that your accessKey is unknown + echo 'wrong login'; + +} catch (\Exception $e) { + echo $e->getMessage(); +} diff --git a/examples/chatcontacts-delete.php b/examples/chatcontacts-delete.php new file mode 100644 index 00000000..acedb94c --- /dev/null +++ b/examples/chatcontacts-delete.php @@ -0,0 +1,18 @@ +chatcontacts->delete('4affa2345d7fb22e373921n524df5409'); // Set a contact id + var_dump('Deleted : ' . $deleted); + +} catch (\MessageBird\Exceptions\AuthenticateException $e) { + // That means that your accessKey is unknown + echo 'wrong login'; + +} catch (\Exception $e) { + echo $e->getMessage(); +} diff --git a/examples/chatcontacts-list.php b/examples/chatcontacts-list.php new file mode 100644 index 00000000..b44ee6fd --- /dev/null +++ b/examples/chatcontacts-list.php @@ -0,0 +1,18 @@ +chatcontacts->getList(); + var_dump($ChatContactResult); + +} catch (\MessageBird\Exceptions\AuthenticateException $e) { + // That means that your accessKey is unknown + echo 'wrong login'; + +} catch (\Exception $e) { + echo $e->getMessage(); +} diff --git a/examples/chatcontacts-view.php b/examples/chatcontacts-view.php new file mode 100644 index 00000000..4f62ac3d --- /dev/null +++ b/examples/chatcontacts-view.php @@ -0,0 +1,18 @@ +chatcontacts->read('0051af4c577e3eebbc3631n95680736'); // Set a contact id here + var_dump($ChatContactResult); + +} catch (\MessageBird\Exceptions\AuthenticateException $e) { + // That means that your accessKey is unknown + echo 'wrong login'; + +} catch (\Exception $e) { + echo $e->getMessage(); +} diff --git a/examples/chatmessages-create.php b/examples/chatmessages-create.php new file mode 100644 index 00000000..55b98061 --- /dev/null +++ b/examples/chatmessages-create.php @@ -0,0 +1,27 @@ +contactId = '9d754dac577e3ff103cdf4n29856560'; +$ChatMessage->payload = 'This is a test message to test the Chat API'; +$ChatMessage->type = 'text'; + + +try { + $ChatMessageResult = $MessageBird->chatmessages->create($ChatMessage); + var_dump($ChatMessageResult); + +} catch (\MessageBird\Exceptions\AuthenticateException $e) { + // That means that your accessKey is unknown + echo 'wrong login'; + +} catch (\MessageBird\Exceptions\BalanceException $e) { + // That means that you are out of credits, so do something about it. + echo 'no balance'; + +} catch (\Exception $e) { + echo $e->getMessage(); +} diff --git a/examples/chatmessages-list.php b/examples/chatmessages-list.php new file mode 100644 index 00000000..fd193b2f --- /dev/null +++ b/examples/chatmessages-list.php @@ -0,0 +1,20 @@ +chatmessages->getList(); + var_dump($ChatMessageResult); + +} catch (\MessageBird\Exceptions\AuthenticateException $e) { + // That means that your accessKey is unknown + echo 'wrong login'; + +} catch (\Exception $e) { + echo $e->getMessage(); +} diff --git a/examples/chatmessages-view.php b/examples/chatmessages-view.php new file mode 100644 index 00000000..2356d0c7 --- /dev/null +++ b/examples/chatmessages-view.php @@ -0,0 +1,20 @@ +chatmessages->read('d6508edc578ca7641e3919n79796670'); // Set a message id here + var_dump($MessageResult); + +} catch (\MessageBird\Exceptions\AuthenticateException $e) { + // That means that your accessKey is unknown + echo 'wrong login'; + +} catch (\Exception $e) { + echo $e->getMessage(); +} diff --git a/examples/chatplatforms-list.php b/examples/chatplatforms-list.php new file mode 100644 index 00000000..828f5965 --- /dev/null +++ b/examples/chatplatforms-list.php @@ -0,0 +1,20 @@ +chatplatforms->getList(); + var_dump($ChatPlatformResult); + +} catch (\MessageBird\Exceptions\AuthenticateException $e) { + // That means that your accessKey is unknown + echo 'wrong login'; + +} catch (\Exception $e) { + echo $e->getMessage(); +} diff --git a/src/MessageBird/Client.php b/src/MessageBird/Client.php index 156dd4d8..2d8d10b0 100644 --- a/src/MessageBird/Client.php +++ b/src/MessageBird/Client.php @@ -11,6 +11,7 @@ class Client { const ENDPOINT = 'https://rest.messagebird.com'; + const CHATAPI_ENDPOINT = 'https://chat.messagebird.com/1'; const CLIENT_VERSION = '1.4.1'; @@ -59,6 +60,11 @@ class Client */ protected $HttpClient; + /** + * @var Common\HttpClient + */ + protected $ChatAPIHttpClient; + /** * @param string $accessKey * @param Common\HttpClient $httpClient @@ -66,13 +72,19 @@ class Client public function __construct($accessKey = null, Common\HttpClient $httpClient = null) { if ($httpClient == null) { + $this->ChatAPIHttpClient = new Common\HttpClient(self::CHATAPI_ENDPOINT); $this->HttpClient = new Common\HttpClient(self::ENDPOINT); } else { + $this->ChatAPIHttpClient = $httpClient; $this->HttpClient = $httpClient; } + $this->HttpClient->addUserAgentString('MessageBird/ApiClient/' . self::CLIENT_VERSION); $this->HttpClient->addUserAgentString($this->getPhpVersion()); + $this->ChatAPIHttpClient->addUserAgentString('MessageBird/ApiClient/' . self::CLIENT_VERSION); + $this->ChatAPIHttpClient->addUserAgentString($this->getPhpVersion()); + if ($accessKey !== null) { $this->setAccessKey($accessKey); } @@ -84,6 +96,11 @@ public function __construct($accessKey = null, Common\HttpClient $httpClient = n $this->voicemessages = new Resources\VoiceMessage($this->HttpClient); $this->lookup = new Resources\Lookup($this->HttpClient); $this->lookupHlr = new Resources\LookupHlr($this->HttpClient); + $this->chatmessages = new Resources\Chat\Message($this->ChatAPIHttpClient); + $this->chatchannels = new Resources\Chat\Channel($this->ChatAPIHttpClient); + $this->chatplatforms = new Resources\Chat\Platform($this->ChatAPIHttpClient); + $this->chatcontacts = new Resources\Chat\Contact($this->ChatAPIHttpClient); + } /** @@ -92,6 +109,8 @@ public function __construct($accessKey = null, Common\HttpClient $httpClient = n public function setAccessKey ($accessKey) { $Authentication = new Common\Authentication($accessKey); + + $this->ChatAPIHttpClient->setAuthentication($Authentication); $this->HttpClient->setAuthentication($Authentication); } diff --git a/src/MessageBird/Common/HttpClient.php b/src/MessageBird/Common/HttpClient.php index 5db9a9ed..d065d9f6 100644 --- a/src/MessageBird/Common/HttpClient.php +++ b/src/MessageBird/Common/HttpClient.php @@ -15,6 +15,7 @@ class HttpClient const REQUEST_GET = "GET"; const REQUEST_POST = "POST"; const REQUEST_DELETE = "DELETE"; + const REQUEST_PUT = "PUT"; const HTTP_NO_CONTENT = 204; @@ -110,6 +111,9 @@ public function performHttpRequest($method, $resourceName, $query = null, $body curl_setopt($curl, CURLOPT_POSTFIELDS, $body); } elseif ($method === self::REQUEST_DELETE) { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, self::REQUEST_DELETE); + } elseif ($method === self::REQUEST_PUT){ + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, self::REQUEST_PUT); + curl_setopt($curl, CURLOPT_POSTFIELDS, $body); } // Some servers have outdated or incorrect certificates, Use the included CA-bundle diff --git a/src/MessageBird/Objects/Chat/Channel.php b/src/MessageBird/Objects/Chat/Channel.php new file mode 100644 index 00000000..378635c5 --- /dev/null +++ b/src/MessageBird/Objects/Chat/Channel.php @@ -0,0 +1,102 @@ +id; + } +} \ No newline at end of file diff --git a/src/MessageBird/Objects/Chat/Platform.php b/src/MessageBird/Objects/Chat/Platform.php new file mode 100644 index 00000000..bd0ea120 --- /dev/null +++ b/src/MessageBird/Objects/Chat/Platform.php @@ -0,0 +1,89 @@ +getErrorString()); } + /** + * @param $object + * @param $id + * @return $this ->Object + * @internal param array $parameters + * + */ + public function update($object, $id) + { + + $objVars = get_object_vars($object); + $body = array(); + foreach ($objVars as $key => $value) { + if (!is_null($value)) { + $body[$key] = $value; + } + } + + $ResourceName = $this->resourceName . (($id) ? '/' . $id : null); + $body = json_encode($body); + list(, , $body) = $this->HttpClient->performHttpRequest(Common\HttpClient::REQUEST_PUT, $ResourceName, false, $body); + return $this->processRequest($body); + } } diff --git a/src/MessageBird/Resources/Chat/Channel.php b/src/MessageBird/Resources/Chat/Channel.php new file mode 100644 index 00000000..bfdfee24 --- /dev/null +++ b/src/MessageBird/Resources/Chat/Channel.php @@ -0,0 +1,28 @@ +setObject(new Objects\Chat\Channel()); + $this->setResourceName('channels'); + + parent::__construct($HttpClient); + } +} diff --git a/src/MessageBird/Resources/Chat/Contact.php b/src/MessageBird/Resources/Chat/Contact.php new file mode 100644 index 00000000..36551627 --- /dev/null +++ b/src/MessageBird/Resources/Chat/Contact.php @@ -0,0 +1,28 @@ +setObject(new Objects\Chat\Contact()); + $this->setResourceName('contacts'); + + parent::__construct($HttpClient); + } +} diff --git a/src/MessageBird/Resources/Chat/Message.php b/src/MessageBird/Resources/Chat/Message.php new file mode 100644 index 00000000..b482b9ca --- /dev/null +++ b/src/MessageBird/Resources/Chat/Message.php @@ -0,0 +1,28 @@ +setObject(new Objects\Chat\Message()); + $this->setResourceName('messages'); + + parent::__construct($HttpClient); + } +} diff --git a/src/MessageBird/Resources/Chat/Platform.php b/src/MessageBird/Resources/Chat/Platform.php new file mode 100644 index 00000000..e2e626b2 --- /dev/null +++ b/src/MessageBird/Resources/Chat/Platform.php @@ -0,0 +1,28 @@ +setObject(new Objects\Chat\Platform()); + $this->setResourceName('platforms'); + + parent::__construct($HttpClient); + } +} diff --git a/tests/integration/BaseTest.php b/tests/integration/BaseTest.php index 4632cf2b..b944a193 100644 --- a/tests/integration/BaseTest.php +++ b/tests/integration/BaseTest.php @@ -14,6 +14,11 @@ public function testClientConstructor() $this->assertInstanceOf('MessageBird\Resources\Messages', $MessageBird->messages); $this->assertInstanceOf('MessageBird\Resources\VoiceMessage', $MessageBird->voicemessages); $this->assertInstanceOf('MessageBird\Resources\Verify', $MessageBird->verify); + $this->assertInstanceOf('MessageBird\Resources\Chat\Message', $MessageBird->chatmessages); + $this->assertInstanceOf('MessageBird\Resources\Chat\Platform', $MessageBird->chatplatforms); + $this->assertInstanceOf('MessageBird\Resources\Chat\Channel', $MessageBird->chatchannels); + $this->assertInstanceOf('MessageBird\Resources\Chat\Contact', $MessageBird->chatcontacts); + } public function testHttpClientMock() diff --git a/tests/integration/chat/ChatTest.php b/tests/integration/chat/ChatTest.php new file mode 100644 index 00000000..c8b90bcf --- /dev/null +++ b/tests/integration/chat/ChatTest.php @@ -0,0 +1,140 @@ +client = new \MessageBird\Client('YOUR_ACCESS_KEY', $this->mockClient); + } + + public function testCreateChatMessage() + { + $ChatMessage = new \MessageBird\Objects\Chat\Message(); + $ChatMessage->contactId = '9d754dac577e3ff103cdf4n29856560'; + $ChatMessage->payload = 'This is a test message to test the Chat API'; + $ChatMessage->type = 'text'; + + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(200, '', '{"type":"text","payload":"This is a test message to test the Chat API","contactId":"9d754dac577e3ff103cdf4n29856560","endpoint":null}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("POST", 'messages', null, '{"type":"text","payload":"This is a test message to test the Chat API","contactId":"9d754dac577e3ff103cdf4n29856560","endpoint":null}'); + $this->client->chatmessages->create($ChatMessage); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListChatMessage() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'messages', array ('offset' => 100, 'limit' => 30), null); + $ChatMessageList = $this->client->chatmessages->getList(array ('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadChatMessage() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'messages/id', null, null); + $ChatMessageList = $this->client->chatmessages->read("id"); + } + + + public function testCreateChatChannel() + { + $ChatChannel = new \MessageBird\Objects\Chat\Channel(); + $ChatChannel->name = 'Test Channel Telegram'; + $ChatChannel->platformId = 'e84f332c5649a5f911e569n69330697'; + + $ChatChannel->channelDetails = + array( + 'botName' => 'testBot', + 'token' => '1234566778:A34JT44Yr4amk234352et5hvRnHeAEHA' + ); + + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(200, '', '{"name":"Test Channel Telegram","platformId":"e84f332c5649a5f911e569n69330697","channelDetails":{"botName":"testBot","token":"1234566778:A34JT44Yr4amk234352et5hvRnHeAEHA"},"callbackUrl":null,"endpoint":null}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("POST", 'channels', null, '{"name":"Test Channel Telegram","platformId":"e84f332c5649a5f911e569n69330697","channelDetails":{"botName":"testBot","token":"1234566778:A34JT44Yr4amk234352et5hvRnHeAEHA"},"callbackUrl":null,"endpoint":null}'); + $this->client->chatchannels->create($ChatChannel); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListChatChannels() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'channels', array ('offset' => 100, 'limit' => 30), null); + $ChannelList = $this->client->chatchannels->getList(array ('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadChatChannel() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'channels/id', null, null); + $Channel = $this->client->chatchannels->read("id"); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testDeleteChannel() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("DELETE", 'channels/id', null, null); + $Channel = $this->client->chatchannels->delete("id"); + } + + public function testUpdateChatChannel() + { + $ChatChannel = new \MessageBird\Objects\Chat\Channel(); + $ChatChannel->name = '9d2345ac577e4f103cd3d4529856560'; + $ChatChannel->callbackUrl = 'http://testurl.dev'; + + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(200, '', '{"name":"9d2345ac577e4f103cd3d4529856560","callbackUrl":"http:\/\/testurl.dev"}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("PUT", 'channels/234agfgADFH2974gaADFH3hudf9h', null, '{"name":"9d2345ac577e4f103cd3d4529856560","callbackUrl":"http:\/\/testurl.dev"}'); + $this->client->chatchannels->update($ChatChannel,'234agfgADFH2974gaADFH3hudf9h'); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListChatPlatforms() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'platforms', array ('offset' => 100, 'limit' => 30), null); + $ChannelList = $this->client->chatplatforms->getList(array ('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadChatPlatform() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'platforms/id', null, null); + $Channel = $this->client->chatplatforms->read("id"); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListChatContacts() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'contacts', array ('offset' => 100, 'limit' => 30), null); + $ContactList = $this->client->chatcontacts->getList(array ('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadChatContact() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'contacts/id', null, null); + $Contact = $this->client->chatcontacts->read("id"); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testDeleteContact() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("DELETE", 'contacts/id', null, null); + $contact = $this->client->chatcontacts->delete("id"); + } +}