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 APIPollClient */ |
12
|
|
|
private $client; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param APIClient $client |
16
|
|
|
*/ |
17
|
7 |
|
public function __construct(APIPollClient $client) |
18
|
|
|
{ |
19
|
7 |
|
$this->client = $client; |
20
|
7 |
|
} |
21
|
|
|
|
22
|
1 |
|
public function poll() |
23
|
|
|
{ |
24
|
1 |
|
$this->client->poll([$this, '_handlePollResponse']); |
|
|
|
|
25
|
1 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \React\HttpClient\Response $response |
29
|
|
|
*/ |
30
|
1 |
|
public function _handlePollResponse(\React\HttpClient\Response $response) |
31
|
|
|
{ |
32
|
1 |
|
$response->on('data', [$this, '_handlePollData']); |
33
|
1 |
|
$response->on('error', [$this, '_handlePollError']); |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $data |
38
|
|
|
* @param \React\HttpClient\Response $response |
39
|
|
|
*/ |
40
|
2 |
|
public function _handlePollData($data, $response) |
|
|
|
|
41
|
|
|
{ |
42
|
2 |
|
$data = json_decode($data, 1); |
43
|
2 |
|
$messageData = $data['result']; |
44
|
|
|
|
45
|
2 |
|
foreach ($messageData as $message) { |
46
|
2 |
|
$apiMessage = new APIMessage($message); |
47
|
2 |
|
$this->client->markMessageHandled($apiMessage); |
48
|
|
|
|
49
|
2 |
|
if (!$apiMessage->hasText()) { |
50
|
1 |
|
continue; |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
$this->getEmitter()->emit( |
54
|
1 |
|
$apiMessage->getText(), |
55
|
1 |
|
['message' => $message, 'responder' => $this->getResponder($apiMessage)] |
|
|
|
|
56
|
|
|
); |
57
|
|
|
} |
58
|
2 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param \React\HttpClient\Response $response |
62
|
|
|
*/ |
63
|
1 |
|
public function _handlePollError(\React\HttpClient\Response $response) |
|
|
|
|
64
|
|
|
{ |
65
|
1 |
|
throw new \Exception(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param APIMessage $message |
70
|
|
|
* @return function |
71
|
|
|
*/ |
72
|
|
|
public function getResponder(APIMessage $message) |
73
|
|
|
{ |
74
|
1 |
|
return function ($text) use ($message) { |
75
|
|
|
$this->client->send($text, $message); |
76
|
1 |
|
}; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: