Skip to content

Commit 0d456db

Browse files
authored
Merge pull request #263 from BoShurik/webhook-info
getWebhookInfo method
2 parents 607388f + 4b448ac commit 0d456db

File tree

3 files changed

+252
-0
lines changed

3 files changed

+252
-0
lines changed

src/BotApi.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use TelegramBot\Api\Types\Update;
1717
use TelegramBot\Api\Types\User;
1818
use TelegramBot\Api\Types\UserProfilePhotos;
19+
use TelegramBot\Api\Types\WebhookInfo;
1920

2021
/**
2122
* Class BotApi
@@ -452,6 +453,20 @@ public function deleteWebhook()
452453
return $this->call('deleteWebhook');
453454
}
454455

456+
/**
457+
* Use this method to get current webhook status. Requires no parameters.
458+
* On success, returns a WebhookInfo object. If the bot is using getUpdates,
459+
* will return an object with the url field empty.
460+
*
461+
* @return \TelegramBot\Api\Types\WebhookInfo
462+
* @throws \TelegramBot\Api\Exception
463+
* @throws \TelegramBot\Api\InvalidArgumentException
464+
*/
465+
public function getWebhookInfo()
466+
{
467+
return WebhookInfo::fromResponse($this->call('getWebhookInfo'));
468+
}
469+
455470
/**
456471
* A simple method for testing your bot's auth token.Requires no parameters.
457472
* Returns basic information about the bot in form of a User object.

src/Types/WebhookInfo.php

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
<?php
2+
/**
3+
* User: boshurik
4+
* Date: 10.06.2020
5+
* Time: 19:43
6+
*/
7+
8+
namespace TelegramBot\Api\Types;
9+
10+
use TelegramBot\Api\BaseType;
11+
use TelegramBot\Api\TypeInterface;
12+
13+
/**
14+
* Contains information about the current status of a webhook.
15+
*
16+
* @package TelegramBot\Api\Types
17+
*/
18+
class WebhookInfo extends BaseType implements TypeInterface
19+
{
20+
/**
21+
* {@inheritdoc}
22+
*
23+
* @var array
24+
*/
25+
static protected $requiredParams = ['url', 'has_custom_certificate', 'pending_update_count'];
26+
27+
/**
28+
* {@inheritdoc}
29+
*
30+
* @var array
31+
*/
32+
static protected $map = [
33+
'url' => true,
34+
'has_custom_certificate' => true,
35+
'pending_update_count' => true,
36+
'last_error_date' => true,
37+
'last_error_message' => true,
38+
'max_connections' => true,
39+
'allowed_updates' => true
40+
];
41+
42+
/**
43+
* Webhook URL, may be empty if webhook is not set up
44+
*
45+
* @var string
46+
*/
47+
protected $url;
48+
49+
/**
50+
* True, if a custom certificate was provided for webhook certificate checks
51+
*
52+
* @var bool
53+
*/
54+
protected $hasCustomCertificate;
55+
56+
/**
57+
* Number of updates awaiting delivery
58+
*
59+
* @var int
60+
*/
61+
protected $pendingUpdateCount;
62+
63+
/**
64+
* Optional. Unix time for the most recent error that happened when trying to deliver an update via webhook
65+
*
66+
* @var int
67+
*/
68+
protected $lastErrorDate;
69+
70+
/**
71+
* Optional. Error message in human-readable format for the most recent error that happened when trying to deliver
72+
* an update via webhook
73+
*
74+
* @var string
75+
*/
76+
protected $lastErrorMessage;
77+
78+
/**
79+
* Optional. Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery
80+
*
81+
* @var int
82+
*/
83+
protected $maxConnections;
84+
85+
/**
86+
* Optional. A list of update types the bot is subscribed to. Defaults to all update types
87+
*
88+
* @var array
89+
*/
90+
protected $allowedUpdates;
91+
92+
/**
93+
* @return string
94+
*/
95+
public function getUrl()
96+
{
97+
return $this->url;
98+
}
99+
100+
/**
101+
* @param string $url
102+
*/
103+
public function setUrl($url)
104+
{
105+
$this->url = $url;
106+
}
107+
108+
/**
109+
* @return bool
110+
*/
111+
public function hasCustomCertificate()
112+
{
113+
return $this->hasCustomCertificate;
114+
}
115+
116+
/**
117+
* @param bool $hasCustomCertificate
118+
*/
119+
public function setHasCustomCertificate($hasCustomCertificate)
120+
{
121+
$this->hasCustomCertificate = $hasCustomCertificate;
122+
}
123+
124+
/**
125+
* @return int
126+
*/
127+
public function getPendingUpdateCount()
128+
{
129+
return $this->pendingUpdateCount;
130+
}
131+
132+
/**
133+
* @param int $pendingUpdateCount
134+
*/
135+
public function setPendingUpdateCount($pendingUpdateCount)
136+
{
137+
$this->pendingUpdateCount = $pendingUpdateCount;
138+
}
139+
140+
/**
141+
* @return int
142+
*/
143+
public function getLastErrorDate()
144+
{
145+
return $this->lastErrorDate;
146+
}
147+
148+
/**
149+
* @param int $lastErrorDate
150+
*/
151+
public function setLastErrorDate($lastErrorDate)
152+
{
153+
$this->lastErrorDate = $lastErrorDate;
154+
}
155+
156+
/**
157+
* @return string
158+
*/
159+
public function getLastErrorMessage()
160+
{
161+
return $this->lastErrorMessage;
162+
}
163+
164+
/**
165+
* @param string $lastErrorMessage
166+
*/
167+
public function setLastErrorMessage($lastErrorMessage)
168+
{
169+
$this->lastErrorMessage = $lastErrorMessage;
170+
}
171+
172+
/**
173+
* @return int
174+
*/
175+
public function getMaxConnections()
176+
{
177+
return $this->maxConnections;
178+
}
179+
180+
/**
181+
* @param int $maxConnections
182+
*/
183+
public function setMaxConnections($maxConnections)
184+
{
185+
$this->maxConnections = $maxConnections;
186+
}
187+
188+
/**
189+
* @return array
190+
*/
191+
public function getAllowedUpdates()
192+
{
193+
return $this->allowedUpdates;
194+
}
195+
196+
/**
197+
* @param array $allowedUpdates
198+
*/
199+
public function setAllowedUpdates($allowedUpdates)
200+
{
201+
$this->allowedUpdates = $allowedUpdates;
202+
}
203+
}

tests/WebhookInfoTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* User: boshurik
4+
* Date: 10.06.2020
5+
* Time: 19:54
6+
*/
7+
8+
namespace TelegramBot\Api\Test;
9+
10+
use TelegramBot\Api\Types\WebhookInfo;
11+
12+
class WebhookInfoTest extends \PHPUnit_Framework_TestCase
13+
{
14+
public function testFromResponse()
15+
{
16+
$webhookInfo = WebhookInfo::fromResponse(array(
17+
'url' => '',
18+
'has_custom_certificate' => false,
19+
'pending_update_count' => 0,
20+
'last_error_date' => null,
21+
'last_error_message' => null,
22+
'max_connections' => 40,
23+
'allowed_updates' => null
24+
));
25+
$this->assertInstanceOf('\TelegramBot\Api\Types\WebhookInfo', $webhookInfo);
26+
$this->assertEquals('', $webhookInfo->getUrl());
27+
$this->assertEquals(false, $webhookInfo->hasCustomCertificate());
28+
$this->assertEquals(0, $webhookInfo->getPendingUpdateCount());
29+
$this->assertEquals(null, $webhookInfo->getLastErrorDate());
30+
$this->assertEquals(null, $webhookInfo->getLastErrorMessage());
31+
$this->assertEquals(40, $webhookInfo->getMaxConnections());
32+
$this->assertEquals(null, $webhookInfo->getAllowedUpdates());
33+
}
34+
}

0 commit comments

Comments
 (0)