Skip to content
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ try {
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);

// Set webhook
$result = $telegram->setWebHook($hook_url);
$result = $telegram->setWebhook($hook_url);
if ($result->isOk()) {
echo $result->getDescription();
}
Expand Down Expand Up @@ -242,7 +242,7 @@ try {

To upload the certificate, add the certificate path as a parameter in *set.php*:
```php
$result = $telegram->setWebHook($hook_url, $certificate_path);
$result = $telegram->setWebhook($hook_url, ['certificate' => $certificate_path]);
```

### Unset Webhook
Expand Down
4 changes: 2 additions & 2 deletions examples/set.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);

// Set webhook
$result = $telegram->setWebHook($hook_url);
$result = $telegram->setWebhook($hook_url);

// Uncomment to use certificate
//$result = $telegram->setWebHook($hook_url, $path_certificate);
//$result = $telegram->setWebhook($hook_url, ['certificate' => $path_certificate]);

if ($result->isOk()) {
echo $result->getDescription();
Expand Down
2 changes: 1 addition & 1 deletion examples/unset.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);

// Unset webhook
$result = $telegram->unsetWebHook();
$result = $telegram->unsetWebhook();

if ($result->isOk()) {
echo $result->getDescription();
Expand Down
13 changes: 7 additions & 6 deletions src/Entities/WebhookInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
*
* @link https://core.telegram.org/bots/api#webhookinfo
*
* @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 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 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 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 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 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
{
Expand Down
15 changes: 11 additions & 4 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -800,16 +800,23 @@ public static function getUpdates(array $data)
* @link https://core.telegram.org/bots/api#setwebhook
*
* @param string $url
* @param string $file
* @param array $data Optional parameters.
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @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);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,19 +727,19 @@ public function getVersion()
/**
* Set Webhook for bot
*
* @param string $url
* @param string|null $path_certificate
* @param string $url
* @param array $data Optional parameters.
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function setWebHook($url, $path_certificate = null)
public function setWebhook($url, array $data = [])
{
if (empty($url)) {
throw new TelegramException('Hook url is empty!');
}

$result = Request::setWebhook($url, $path_certificate);
$result = Request::setWebhook($url, $data);

if (!$result->isOk()) {
throw new TelegramException(
Expand All @@ -756,7 +756,7 @@ public function setWebHook($url, $path_certificate = null)
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function unsetWebHook()
public function unsetWebhook()
{
$result = Request::setWebhook();

Expand Down
144 changes: 49 additions & 95 deletions tests/unit/Entities/WebhookInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,155 +21,109 @@
*/
class WebhookInfoTest extends TestCase
{

/**
* webhook data
*
* @var array
*
*/
/**
* @var array Webhook data
*/
public $data;

/**
*
* Set Up
*
*/
public function setUp()
{
$this->data = [
'url' => 'http://phpunit',
'has_custom_certificate' => (bool)mt_rand(0, 1),
'pending_update_count' => (int)mt_rand(1, 9),
'url' => 'http://phpunit',
'has_custom_certificate' => (bool) mt_rand(0, 1),
'pending_update_count' => (int) mt_rand(1, 9),
'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()
{
$webhook = new WebhookInfo($this->data);
$this->assertInstanceOf('Longman\TelegramBot\Entities\WebhookInfo', $webhook);
}

/**
*
* Testing getUrl
*
*/
public function testGetUrl()
{
$webhook = new WebhookInfo($this->data);
$url = $webhook->getUrl();
$url = $webhook->getUrl();
$this->assertEquals($this->data['url'], $url);
}

/**
*
* Testing getHasCustomCertificate
*
*/

public function testGetHasCustomCertificate()
{
$webhook = new WebhookInfo($this->data);
$webhook = new WebhookInfo($this->data);
$custom_certificate = $webhook->getHasCustomCertificate();
$this->assertInternalType('bool', $custom_certificate);
$this->assertEquals($this->data['has_custom_certificate'], $custom_certificate);
}

/**
*
* Testing getPendingUpdateCount
*
*/
public function testGetPendingUpdateCount()
{
$webhook = new WebhookInfo($this->data);
$webhook = new WebhookInfo($this->data);
$update_count = $webhook->getPendingUpdateCount();
$this->assertInternalType('int', $update_count);
$this->assertEquals($this->data['pending_update_count'], $update_count);
}
}

/**
*
* Testing getLastErrorDate
*
*/
public function testGetLastErrorDate()
{
$webhook = new WebhookInfo($this->data);
$webhook = new WebhookInfo($this->data);
$error_date = $webhook->getLastErrorDate();
$this->assertInternalType('int', $error_date);
#$this->assertRegExp('/([0-9]{10,})/', $error_date);
$this->assertEquals($this->data['last_error_date'], $error_date);
}

/**
*
* Testing getLastErrorMessage
*
*/
public function testGetLastErrorMessage()
{
$webhook = new WebhookInfo($this->data);
$webhook = new WebhookInfo($this->data);
$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);
}

/**
*
* Testing get data without params
*
*/
public function testGetMaxConnections()
{
$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()
{
unset($this->data['url']);
$webhook = new WebhookInfo($this->data);
$result = $webhook->getUrl();
$this->assertNull($result);
// Make a copy to not risk failed tests if not run in proper order.
$data = $this->data;

unset($webhook, $result);
unset($data['url']);
$this->assertNull((new WebhookInfo($data))->getUrl());

unset($this->data['has_custom_certificate']);
$webhook = new WebhookInfo($this->data);
$result = $webhook->getHasCustomCertificate();
$this->assertNull($result);
unset($data['has_custom_certificate']);
$this->assertNull((new WebhookInfo($data))->getHasCustomCertificate());

unset($webhook, $result);
unset($data['pending_update_count']);
$this->assertNull((new WebhookInfo($data))->getPendingUpdateCount());

unset($this->data['pending_update_count']);
$webhook = new WebhookInfo($this->data);
$result = $webhook->getPendingUpdateCount();
$this->assertNull($result);

unset($webhook, $result);
unset($data['last_error_date']);
$this->assertNull((new WebhookInfo($data))->getLastErrorDate());

unset($this->data['last_error_date']);
$webhook = new WebhookInfo($this->data);
$result = $webhook->getLastErrorDate();
$this->assertNull($result);
unset($data['last_error_message']);
$this->assertNull((new WebhookInfo($data))->getLastErrorMessage());

unset($webhook, $result);
unset($data['max_connections']);
$this->assertNull((new WebhookInfo($data))->getMaxConnections());

unset($this->data['last_error_message']);
$webhook = new WebhookInfo($this->data);
$result = $webhook->getLastErrorMessage();
$this->assertNull($result);
unset($data['allowed_updates']);
$this->assertNull((new WebhookInfo($data))->getAllowedUpdates());
}
}