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
|
|
|
$options = [ |
37
|
|
|
'verify' => config('telegram-git-notifier.app.request_verify'), |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
try { |
41
|
|
|
$response = $this->client->request('GET', $url, $options); |
42
|
|
|
|
43
|
|
|
return $response->getBody()->getContents(); |
44
|
|
|
} catch (GuzzleException) { |
45
|
|
|
throw WebhookException::set(); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function deleteWebHook(): string |
50
|
|
|
{ |
51
|
|
|
$url = "https://api.telegram.org/bot{$this->token}/deleteWebhook"; |
52
|
|
|
$options = [ |
53
|
|
|
'verify' => config('telegram-git-notifier.app.request_verify'), |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
try { |
57
|
|
|
$response = $this->client->request('GET', $url, $options); |
58
|
|
|
|
59
|
|
|
return $response->getBody()->getContents(); |
60
|
|
|
} catch (GuzzleException) { |
61
|
|
|
throw WebhookException::delete(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getWebHookInfo(): string |
66
|
|
|
{ |
67
|
|
|
$url = "https://api.telegram.org/bot{$this->token}/getWebhookInfo"; |
68
|
|
|
$options = [ |
69
|
|
|
'verify' => config('telegram-git-notifier.app.request_verify'), |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
$response = $this->client->request('GET', $url, $options); |
74
|
|
|
|
75
|
|
|
return $response->getBody()->getContents(); |
76
|
|
|
} catch (GuzzleException) { |
77
|
|
|
throw WebhookException::getWebHookInfo(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getUpdates(): string |
82
|
|
|
{ |
83
|
|
|
$url = "https://api.telegram.org/bot{$this->token}/getUpdates"; |
84
|
|
|
$options = [ |
85
|
|
|
'verify' => config('telegram-git-notifier.app.request_verify'), |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
try { |
89
|
|
|
$response = $this->client->request('GET', $url, $options); |
90
|
|
|
|
91
|
|
|
return $response->getBody()->getContents(); |
92
|
|
|
} catch (GuzzleException) { |
93
|
|
|
throw WebhookException::getUpdates(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|