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