|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TelegramBot; |
|
4
|
|
|
|
|
5
|
|
|
use React\HttpClient\Client; |
|
6
|
|
|
|
|
7
|
|
|
class APIPollClient |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* |
|
11
|
|
|
* |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $botToken; |
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
* |
|
18
|
|
|
* @var int |
|
19
|
|
|
*/ |
|
20
|
|
|
private $offset; |
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
* |
|
24
|
|
|
* @var \React\HttpClient\Client |
|
25
|
|
|
*/ |
|
26
|
|
|
private $client; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $botToken |
|
30
|
|
|
* @param Client $client |
|
31
|
|
|
*/ |
|
32
|
6 |
|
public function __construct(string $botToken, Client $client) |
|
33
|
|
|
{ |
|
34
|
6 |
|
$this->botToken = $botToken; |
|
35
|
6 |
|
$this->client = $client; |
|
36
|
6 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param CallableInterface $responseHandler |
|
40
|
|
|
* @throws \Exception |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function poll($responseHandler) |
|
43
|
|
|
{ |
|
44
|
1 |
|
$request = $this->client->request( |
|
45
|
1 |
|
'GET', |
|
46
|
1 |
|
$this->botCommand('getUpdates', ['offset' => $this->offset]) |
|
47
|
|
|
); |
|
48
|
1 |
|
$request->on('response', $responseHandler); |
|
49
|
1 |
|
$request->on( |
|
50
|
1 |
|
'error', function ($data) { |
|
51
|
|
|
throw new \RuntimeException($data); |
|
52
|
1 |
|
} |
|
53
|
|
|
); |
|
54
|
1 |
|
$request->end(); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $answer |
|
59
|
|
|
* @param APIMessage $message |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function send($answer, APIMessage $message) |
|
62
|
|
|
{ |
|
63
|
1 |
|
$responseData = $this->postDataEncoder($message->getResponseData($answer)); |
|
64
|
|
|
|
|
65
|
1 |
|
$responseCall = $this->client->request( |
|
66
|
1 |
|
'POST', |
|
67
|
1 |
|
$this->botCommand('sendMessage'), |
|
68
|
1 |
|
$this->getResponseHeaders($responseData) |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
1 |
|
$responseCall->end($responseData); |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
public function markMessageHandled(APIMessage $message) |
|
75
|
|
|
{ |
|
76
|
1 |
|
$this->offset = $message->getUpdateId() + 1; |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
public function getResponseHeaders(string $responseString): array |
|
80
|
|
|
{ |
|
81
|
|
|
return [ |
|
82
|
2 |
|
'Content-Type' => 'application/x-www-form-urlencoded', |
|
83
|
2 |
|
'Content-Length' => strlen($responseString) |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $command |
|
89
|
|
|
* @param array $params |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
2 |
|
private function botCommand(string $command, array $params = []): string |
|
93
|
|
|
{ |
|
94
|
2 |
|
$args = count($params) ? '?' . $this->postDataEncoder($params) : ''; |
|
95
|
|
|
|
|
96
|
2 |
|
return $this->assembleUri($command, $args); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
private function assembleUri($command, $params): string |
|
100
|
|
|
{ |
|
101
|
2 |
|
return sprintf( |
|
102
|
2 |
|
'https://api.telegram.org/bot%s/%s%s', |
|
103
|
2 |
|
$this->botToken, |
|
104
|
2 |
|
$command, |
|
105
|
2 |
|
$params |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
2 |
|
private function postDataEncoder(array $data): string |
|
110
|
|
|
{ |
|
111
|
2 |
|
$string = ''; |
|
112
|
|
|
|
|
113
|
2 |
|
foreach ($data as $k => $v) { |
|
114
|
2 |
|
$string .= $k . '=' . urlencode($v) . '&'; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
return $string; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|