Commit 070a08fa authored by Armando Lüscher's avatar Armando Lüscher Committed by GitHub

Merge pull request #346 from noplanman/update_webhookinfo

Update webhookinfo entity with the latest additions by Telegram.

As this only breaks BC for users with self-signed certificates, I'm merging here...
parents 6453222f 8f37591f
...@@ -206,7 +206,7 @@ try { ...@@ -206,7 +206,7 @@ try {
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME); $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
// Set webhook // Set webhook
$result = $telegram->setWebHook($hook_url); $result = $telegram->setWebhook($hook_url);
if ($result->isOk()) { if ($result->isOk()) {
echo $result->getDescription(); echo $result->getDescription();
} }
...@@ -242,7 +242,7 @@ try { ...@@ -242,7 +242,7 @@ try {
To upload the certificate, add the certificate path as a parameter in *set.php*: To upload the certificate, add the certificate path as a parameter in *set.php*:
```php ```php
$result = $telegram->setWebHook($hook_url, $certificate_path); $result = $telegram->setWebhook($hook_url, ['certificate' => $certificate_path]);
``` ```
### Unset Webhook ### Unset Webhook
......
...@@ -10,10 +10,10 @@ try { ...@@ -10,10 +10,10 @@ try {
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME); $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
// Set webhook // Set webhook
$result = $telegram->setWebHook($hook_url); $result = $telegram->setWebhook($hook_url);
// Uncomment to use certificate // Uncomment to use certificate
//$result = $telegram->setWebHook($hook_url, $path_certificate); //$result = $telegram->setWebhook($hook_url, ['certificate' => $path_certificate]);
if ($result->isOk()) { if ($result->isOk()) {
echo $result->getDescription(); echo $result->getDescription();
......
...@@ -9,7 +9,7 @@ try { ...@@ -9,7 +9,7 @@ try {
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME); $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
// Unset webhook // Unset webhook
$result = $telegram->unsetWebHook(); $result = $telegram->unsetWebhook();
if ($result->isOk()) { if ($result->isOk()) {
echo $result->getDescription(); echo $result->getDescription();
......
...@@ -15,12 +15,13 @@ namespace Longman\TelegramBot\Entities; ...@@ -15,12 +15,13 @@ namespace Longman\TelegramBot\Entities;
* *
* @link https://core.telegram.org/bots/api#webhookinfo * @link https://core.telegram.org/bots/api#webhookinfo
* *
* @method string getUrl() Webhook URL, may be empty if webhook is not set up * @method string getUrl() Webhook URL, may be empty if webhook is not set up
* @method bool getHasCustomCertificate() True, if a custom certificate was provided for webhook certificate checks * @method bool getHasCustomCertificate() True, if a custom certificate was provided for webhook certificate checks
* @method int getPendingUpdateCount() Number of updates awaiting delivery * @method int getPendingUpdateCount() Number of updates awaiting delivery
* @method int getLastErrorDate() Optional. Unix time for the most recent error that happened when trying to deliver an update via webhook * @method int getLastErrorDate() Optional. Unix time for the most recent error that happened when trying to deliver an update via webhook
* @method string getLastErrorMessage() Optional. Error message in human-readable format for the most recent error that happened when trying to deliver an update via webhook * @method string getLastErrorMessage() Optional. Error message in human-readable format for the most recent error that happened when trying to deliver an update via webhook
* * @method int getMaxConnections() Optional. Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery
* @method string[] getAllowedUpdates() Optional. A list of update types the bot is subscribed to. Defaults to all update types
*/ */
class WebhookInfo extends Entity class WebhookInfo extends Entity
{ {
......
...@@ -800,16 +800,23 @@ class Request ...@@ -800,16 +800,23 @@ class Request
* @link https://core.telegram.org/bots/api#setwebhook * @link https://core.telegram.org/bots/api#setwebhook
* *
* @param string $url * @param string $url
* @param string $file * @param array $data Optional parameters.
* *
* @return \Longman\TelegramBot\Entities\ServerResponse * @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public static function setWebhook($url = '', $file = null) public static function setWebhook($url = '', array $data = [])
{ {
$data = ['url' => $url]; $data = array_intersect_key($data, array_flip([
'certificate',
'max_connections',
'allowed_updates',
]));
$data['url'] = $url;
self::assignEncodedFile($data, 'certificate', $file); if (isset($data['certificate'])) {
self::assignEncodedFile($data, 'certificate', $data['certificate']);
}
return self::send('setWebhook', $data); return self::send('setWebhook', $data);
} }
......
...@@ -729,19 +729,19 @@ class Telegram ...@@ -729,19 +729,19 @@ class Telegram
/** /**
* Set Webhook for bot * Set Webhook for bot
* *
* @param string $url * @param string $url
* @param string|null $path_certificate * @param array $data Optional parameters.
* *
* @return \Longman\TelegramBot\Entities\ServerResponse * @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function setWebHook($url, $path_certificate = null) public function setWebhook($url, array $data = [])
{ {
if (empty($url)) { if (empty($url)) {
throw new TelegramException('Hook url is empty!'); throw new TelegramException('Hook url is empty!');
} }
$result = Request::setWebhook($url, $path_certificate); $result = Request::setWebhook($url, $data);
if (!$result->isOk()) { if (!$result->isOk()) {
throw new TelegramException( throw new TelegramException(
...@@ -758,7 +758,7 @@ class Telegram ...@@ -758,7 +758,7 @@ class Telegram
* @return mixed * @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function unsetWebHook() public function unsetWebhook()
{ {
$result = Request::setWebhook(); $result = Request::setWebhook();
......
...@@ -21,155 +21,109 @@ use Longman\TelegramBot\Entities\WebhookInfo; ...@@ -21,155 +21,109 @@ use Longman\TelegramBot\Entities\WebhookInfo;
*/ */
class WebhookInfoTest extends TestCase class WebhookInfoTest extends TestCase
{ {
/**
/** * @var array Webhook data
* webhook data */
*
* @var array
*
*/
public $data; public $data;
/**
*
* Set Up
*
*/
public function setUp() public function setUp()
{ {
$this->data = [ $this->data = [
'url' => 'http://phpunit', 'url' => 'http://phpunit',
'has_custom_certificate' => (bool)mt_rand(0, 1), 'has_custom_certificate' => (bool) mt_rand(0, 1),
'pending_update_count' => (int)mt_rand(1, 9), 'pending_update_count' => (int) mt_rand(1, 9),
'last_error_date' => time(), 'last_error_date' => time(),
'last_error_message' => 'Same_error_message' 'last_error_message' => 'Some_error_message',
'max_connections' => (int) mt_rand(1, 100),
'allowed_updates' => ['message', 'edited_channel_post', 'callback_query'],
]; ];
} }
/**
*
* TearDown
*
*/
public function tearDown()
{
//pass
}
/**
*
* Testing base stage with data object creating
*
*/
public function testBaseStageWebhookInfo() public function testBaseStageWebhookInfo()
{ {
$webhook = new WebhookInfo($this->data); $webhook = new WebhookInfo($this->data);
$this->assertInstanceOf('Longman\TelegramBot\Entities\WebhookInfo', $webhook); $this->assertInstanceOf('Longman\TelegramBot\Entities\WebhookInfo', $webhook);
} }
/**
*
* Testing getUrl
*
*/
public function testGetUrl() public function testGetUrl()
{ {
$webhook = new WebhookInfo($this->data); $webhook = new WebhookInfo($this->data);
$url = $webhook->getUrl(); $url = $webhook->getUrl();
$this->assertEquals($this->data['url'], $url); $this->assertEquals($this->data['url'], $url);
} }
/**
*
* Testing getHasCustomCertificate
*
*/
public function testGetHasCustomCertificate() public function testGetHasCustomCertificate()
{ {
$webhook = new WebhookInfo($this->data); $webhook = new WebhookInfo($this->data);
$custom_certificate = $webhook->getHasCustomCertificate(); $custom_certificate = $webhook->getHasCustomCertificate();
$this->assertInternalType('bool', $custom_certificate); $this->assertInternalType('bool', $custom_certificate);
$this->assertEquals($this->data['has_custom_certificate'], $custom_certificate); $this->assertEquals($this->data['has_custom_certificate'], $custom_certificate);
} }
/**
*
* Testing getPendingUpdateCount
*
*/
public function testGetPendingUpdateCount() public function testGetPendingUpdateCount()
{ {
$webhook = new WebhookInfo($this->data); $webhook = new WebhookInfo($this->data);
$update_count = $webhook->getPendingUpdateCount(); $update_count = $webhook->getPendingUpdateCount();
$this->assertInternalType('int', $update_count); $this->assertInternalType('int', $update_count);
$this->assertEquals($this->data['pending_update_count'], $update_count); $this->assertEquals($this->data['pending_update_count'], $update_count);
} }
/**
*
* Testing getLastErrorDate
*
*/
public function testGetLastErrorDate() public function testGetLastErrorDate()
{ {
$webhook = new WebhookInfo($this->data); $webhook = new WebhookInfo($this->data);
$error_date = $webhook->getLastErrorDate(); $error_date = $webhook->getLastErrorDate();
$this->assertInternalType('int', $error_date); $this->assertInternalType('int', $error_date);
#$this->assertRegExp('/([0-9]{10,})/', $error_date);
$this->assertEquals($this->data['last_error_date'], $error_date); $this->assertEquals($this->data['last_error_date'], $error_date);
} }
/**
*
* Testing getLastErrorMessage
*
*/
public function testGetLastErrorMessage() public function testGetLastErrorMessage()
{ {
$webhook = new WebhookInfo($this->data); $webhook = new WebhookInfo($this->data);
$error_msg = $webhook->getLastErrorMessage(); $error_msg = $webhook->getLastErrorMessage();
$this->assertInternalType('string', $error_msg, $error_msg); $this->assertInternalType('string', $error_msg);
$this->assertEquals($this->data['last_error_message'], $error_msg); $this->assertEquals($this->data['last_error_message'], $error_msg);
} }
/** public function testGetMaxConnections()
* {
* Testing get data without params $webhook = new WebhookInfo($this->data);
* $max_connections = $webhook->getMaxConnections();
*/ $this->assertInternalType('int', $max_connections);
$this->assertEquals($this->data['max_connections'], $max_connections);
}
public function testGetAllowedUpdates()
{
$webhook = new WebhookInfo($this->data);
$allowed_updates = $webhook->getAllowedUpdates();
$this->assertInternalType('array', $allowed_updates);
$this->assertEquals($this->data['allowed_updates'], $allowed_updates);
}
public function testGetDataWithoutParams() public function testGetDataWithoutParams()
{ {
unset($this->data['url']); // Make a copy to not risk failed tests if not run in proper order.
$webhook = new WebhookInfo($this->data); $data = $this->data;
$result = $webhook->getUrl();
$this->assertNull($result);
unset($webhook, $result); unset($data['url']);
$this->assertNull((new WebhookInfo($data))->getUrl());
unset($this->data['has_custom_certificate']); unset($data['has_custom_certificate']);
$webhook = new WebhookInfo($this->data); $this->assertNull((new WebhookInfo($data))->getHasCustomCertificate());
$result = $webhook->getHasCustomCertificate();
$this->assertNull($result);
unset($webhook, $result); unset($data['pending_update_count']);
$this->assertNull((new WebhookInfo($data))->getPendingUpdateCount());
unset($this->data['pending_update_count']); unset($data['last_error_date']);
$webhook = new WebhookInfo($this->data); $this->assertNull((new WebhookInfo($data))->getLastErrorDate());
$result = $webhook->getPendingUpdateCount();
$this->assertNull($result);
unset($webhook, $result);
unset($this->data['last_error_date']); unset($data['last_error_message']);
$webhook = new WebhookInfo($this->data); $this->assertNull((new WebhookInfo($data))->getLastErrorMessage());
$result = $webhook->getLastErrorDate();
$this->assertNull($result);
unset($webhook, $result); unset($data['max_connections']);
$this->assertNull((new WebhookInfo($data))->getMaxConnections());
unset($this->data['last_error_message']); unset($data['allowed_updates']);
$webhook = new WebhookInfo($this->data); $this->assertNull((new WebhookInfo($data))->getAllowedUpdates());
$result = $webhook->getLastErrorMessage();
$this->assertNull($result);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment