Skip to content

ChatAPI functionality #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Aug 15, 2016
Merged
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
33 changes: 33 additions & 0 deletions examples/chatchannels-create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

require_once(__DIR__ . '/../autoload.php');

$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.

$ChatChannel = new \MessageBird\Objects\Chat\Channel();

//Example for telegram channel

$ChatChannel->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();
}
18 changes: 18 additions & 0 deletions examples/chatchannels-delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require_once(__DIR__ . '/../autoload.php');

$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.

try {

$deleted = $MessageBird->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();
}
17 changes: 17 additions & 0 deletions examples/chatchannels-list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

require_once(__DIR__ . '/../autoload.php');

$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.

try {
$ChatChannelResult = $MessageBird->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();
}
23 changes: 23 additions & 0 deletions examples/chatchannels-update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

require_once(__DIR__ . '/../autoload.php');

$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.

$ChatChannel = new \MessageBird\Objects\Chat\Channel();
$ChatChannel->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();
}
17 changes: 17 additions & 0 deletions examples/chatchannels-view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

require_once(__DIR__ . '/../autoload.php');

$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.

try {
$ChatChannelResult = $MessageBird->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();
}
18 changes: 18 additions & 0 deletions examples/chatcontacts-delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require_once(__DIR__ . '/../autoload.php');

$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.

try {

$deleted = $MessageBird->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();
}
18 changes: 18 additions & 0 deletions examples/chatcontacts-list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require_once(__DIR__ . '/../autoload.php');

$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.

try {

$ChatContactResult = $MessageBird->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();
}
18 changes: 18 additions & 0 deletions examples/chatcontacts-view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require_once(__DIR__ . '/../autoload.php');

$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.

try {

$ChatContactResult = $MessageBird->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();
}
27 changes: 27 additions & 0 deletions examples/chatmessages-create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

require_once(__DIR__ . '/../autoload.php');

$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.

$ChatMessage = new \MessageBird\Objects\Chat\Message();
$ChatMessage->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();
}
20 changes: 20 additions & 0 deletions examples/chatmessages-list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

require_once(__DIR__ . '/../autoload.php');

$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.

$ChatMessage = new \MessageBird\Objects\Chat\Message();


try {
$ChatMessageResult = $MessageBird->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();
}
20 changes: 20 additions & 0 deletions examples/chatmessages-view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

require_once(__DIR__ . '/../autoload.php');

$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.

$ChatMessage = new \MessageBird\Objects\Chat\Message();

try {

$MessageResult = $MessageBird->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();
}
20 changes: 20 additions & 0 deletions examples/chatplatforms-list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

require_once(__DIR__ . '/../autoload.php');

$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.

$ChatPlatform = new \MessageBird\Objects\Chat\Channel();

try {

$ChatPlatformResult = $MessageBird->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();
}
19 changes: 19 additions & 0 deletions src/MessageBird/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be updated during release somehow?


Expand Down Expand Up @@ -59,20 +60,31 @@ class Client
*/
protected $HttpClient;

/**
* @var Common\HttpClient
*/
protected $ChatAPIHttpClient;

/**
* @param string $accessKey
* @param Common\HttpClient $httpClient
*/
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);
}
Expand All @@ -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);

}

/**
Expand All @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions src/MessageBird/Common/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Loading