|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TelegramBot; |
|
4
|
|
|
|
|
5
|
|
|
use TelegramBot\APIMessage; |
|
6
|
|
|
|
|
7
|
|
|
class Bot implements BotInterface |
|
8
|
|
|
{ |
|
9
|
|
|
use \League\Event\EmitterTrait; |
|
10
|
|
|
|
|
11
|
|
|
/** @var string */ |
|
12
|
|
|
private $botToken; |
|
|
|
|
|
|
13
|
|
|
/** @var int */ |
|
14
|
|
|
private $offset; |
|
|
|
|
|
|
15
|
|
|
/** @var APIPollClient */ |
|
16
|
|
|
private $client; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param APIClient $client |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(APIPollClient $client) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->client = $client; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function poll() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->client->poll([$this, '_handlePollResponse']); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param \React\HttpClient\Response $response |
|
33
|
|
|
*/ |
|
34
|
|
|
public function _handlePollResponse(\React\HttpClient\Response $response) |
|
35
|
|
|
{ |
|
36
|
|
|
$response->on('data', [$this, '_handlePollData']); |
|
37
|
|
|
$response->on('error', [$this, '_handlePollError']); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array $data |
|
42
|
|
|
* @param \React\HttpClient\Response $response |
|
43
|
|
|
*/ |
|
44
|
|
|
public function _handlePollData($data, $response) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
$data = json_decode($data, 1); |
|
47
|
|
|
$messageData = $data['result']; |
|
48
|
|
|
|
|
49
|
|
|
foreach ($messageData as $message) { |
|
50
|
|
|
$apiMessage = new APIMessage($message); |
|
51
|
|
|
$this->client->markMessageHandled($apiMessage); |
|
52
|
|
|
|
|
53
|
|
|
if (!$apiMessage->hasText()) { |
|
54
|
|
|
continue; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->getEmitter()->emit( |
|
58
|
|
|
$apiMessage->getText(), |
|
59
|
|
|
['message' => $message, 'responder' => $this->getResponder($apiMessage)] |
|
|
|
|
|
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param \React\HttpClient\Response $response |
|
66
|
|
|
*/ |
|
67
|
|
|
public function _handlePollError(\React\HttpClient\Response $response) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
throw new \Exception(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param APIMessage $message |
|
74
|
|
|
* @return function |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getResponder(APIMessage $message) |
|
77
|
|
|
{ |
|
78
|
|
|
return function ($text) use ($message) { |
|
79
|
|
|
$this->client->send($text, $message); |
|
80
|
|
|
}; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.