|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CSlant\TelegramGitNotifier; |
|
4
|
|
|
|
|
5
|
|
|
use CSlant\TelegramGitNotifier\Exceptions\WebhookException; |
|
6
|
|
|
use CSlant\TelegramGitNotifier\Interfaces\WebhookInterface; |
|
7
|
|
|
use GuzzleHttp\Client; |
|
8
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
9
|
|
|
|
|
10
|
|
|
class Webhook implements WebhookInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private string $token; |
|
13
|
|
|
|
|
14
|
|
|
private string $url; |
|
15
|
|
|
|
|
16
|
|
|
private Client $client; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->client = new Client(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function setToken(string $token): void |
|
24
|
|
|
{ |
|
25
|
|
|
$this->token = $token; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function setUrl(string $url): void |
|
29
|
|
|
{ |
|
30
|
|
|
$this->url = $url; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function setWebhook(): string |
|
34
|
|
|
{ |
|
35
|
|
|
$url = "https://api.telegram.org/bot{$this->token}/setWebhook?url={$this->url}"; |
|
36
|
|
|
|
|
37
|
|
|
try { |
|
38
|
|
|
$response = $this->client->request('GET', $url); |
|
39
|
|
|
|
|
40
|
|
|
return $response->getBody()->getContents(); |
|
41
|
|
|
} catch (GuzzleException) { |
|
42
|
|
|
throw WebhookException::set(); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function deleteWebHook(): string |
|
47
|
|
|
{ |
|
48
|
|
|
$url = "https://api.telegram.org/bot{$this->token}/deleteWebhook"; |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
$response = $this->client->request('GET', $url); |
|
52
|
|
|
|
|
53
|
|
|
return $response->getBody()->getContents(); |
|
54
|
|
|
} catch (GuzzleException) { |
|
55
|
|
|
throw WebhookException::delete(); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getWebHookInfo(): string |
|
60
|
|
|
{ |
|
61
|
|
|
$url = "https://api.telegram.org/bot{$this->token}/getWebhookInfo"; |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
|
|
$response = $this->client->request('GET', $url); |
|
65
|
|
|
|
|
66
|
|
|
return $response->getBody()->getContents(); |
|
67
|
|
|
} catch (GuzzleException) { |
|
68
|
|
|
throw WebhookException::getWebHookInfo(); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getUpdates(): string |
|
73
|
|
|
{ |
|
74
|
|
|
$url = "https://api.telegram.org/bot{$this->token}/getUpdates"; |
|
75
|
|
|
|
|
76
|
|
|
try { |
|
77
|
|
|
$response = $this->client->request('GET', $url); |
|
78
|
|
|
|
|
79
|
|
|
return $response->getBody()->getContents(); |
|
80
|
|
|
} catch (GuzzleException) { |
|
81
|
|
|
throw WebhookException::getUpdates(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|