|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TelegramBot; |
|
4
|
|
|
|
|
5
|
|
|
use React\HttpClient\Client; |
|
6
|
|
|
use React\HttpClient\Request; |
|
7
|
|
|
use React\HttpClient\Response; |
|
8
|
|
|
|
|
9
|
|
|
class Bot implements BotInterface { |
|
10
|
|
|
|
|
11
|
|
|
use \League\Event\EmitterTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** @var string */ |
|
14
|
|
|
private $botToken; |
|
15
|
|
|
/** @var int */ |
|
16
|
|
|
private $offset; |
|
17
|
|
|
/** @var \React\HttpClient\Client */ |
|
18
|
|
|
private $client; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param string $botToken |
|
22
|
|
|
*/ |
|
23
|
5 |
|
public function __construct(string $botToken) |
|
24
|
|
|
{ |
|
25
|
5 |
|
$this->botToken = $botToken; |
|
26
|
5 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param \React\HttpClient\Client |
|
30
|
|
|
*/ |
|
31
|
5 |
|
public function setClient(Client $client) { |
|
32
|
5 |
|
$this->client = $client; |
|
33
|
5 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function poll() { |
|
36
|
|
|
$request = $this->getMassagesRequest(); |
|
37
|
|
|
$request->on('response', [$this, '_handlePollResponse']); |
|
38
|
|
|
$request->end(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param \React\HttpClient\Response $response |
|
43
|
|
|
*/ |
|
44
|
|
|
public function _handlePollResponse(Response $response) { |
|
45
|
|
|
$response->on('data', [$this, '_handlePollData']); |
|
46
|
|
|
$response->on('error', [$this, '_handlePollError']); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array $data |
|
51
|
|
|
* @param \React\HttpClient\Response $response |
|
52
|
|
|
*/ |
|
53
|
|
|
public function _handlePollData($data, $response) { |
|
|
|
|
|
|
54
|
|
|
$data = json_decode($data, 1); |
|
55
|
|
|
$messageData = $data['result']; |
|
56
|
|
|
|
|
57
|
|
|
foreach ($messageData as $message) { |
|
58
|
|
|
|
|
59
|
|
|
if (!isset($message['message']['text'])) { |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->getEmitter()->emit( |
|
64
|
|
|
$message['message']['text'], |
|
65
|
|
|
['message' => $message, 'responder' => $this->getResponder($message)] |
|
|
|
|
|
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$this->setUpdateId($message['update_id']); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param \React\HttpClient\Response $response |
|
74
|
|
|
*/ |
|
75
|
|
|
public function _handlePollError(Response $response) { |
|
76
|
|
|
print('Error: '); |
|
77
|
|
|
print_r($response); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $message |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getResponder($message) { |
|
84
|
1 |
|
return function ($text) use($message){ |
|
85
|
|
|
$this->sendResponse($text, $message); |
|
|
|
|
|
|
86
|
1 |
|
}; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
public function getMassagesRequest(): Request { |
|
90
|
1 |
|
return $this->client->request( |
|
91
|
1 |
|
'GET', |
|
92
|
1 |
|
$this->botCommand('getUpdates', ['offset' => $this->offset]) |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
public function sendResponse(string $responseText, array $incomingMessage) { |
|
97
|
1 |
|
$responseData = $this->postDataEncoder([ |
|
98
|
1 |
|
'chat_id' => $incomingMessage['message']['chat']['id'], |
|
99
|
1 |
|
'reply_to_message_id' => $incomingMessage['message']['message_id'], |
|
100
|
1 |
|
'text' => $responseText |
|
101
|
|
|
]); |
|
102
|
|
|
|
|
103
|
1 |
|
$responseCall = $this->client->request( |
|
104
|
1 |
|
'POST', |
|
105
|
1 |
|
$this->botCommand('sendMessage'), |
|
106
|
1 |
|
$this->getResponseHeaders($responseData) |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
1 |
|
$responseCall->end($responseData); |
|
110
|
1 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function setUpdateId($id) { |
|
113
|
|
|
$this->offset = $id+1; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
2 |
|
private function botCommand(string $command, array $params = []) :string { |
|
118
|
2 |
|
$params = (count($params)) ? '?' . $this->postDataEncoder($params) : ''; |
|
119
|
|
|
|
|
120
|
2 |
|
return $this->assembleUri($command, $params); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
2 |
|
private function assembleUri($command, $params) :string { |
|
125
|
2 |
|
return sprintf( |
|
126
|
2 |
|
'https://api.telegram.org/bot%s/%s%s', |
|
127
|
2 |
|
$this->botToken, |
|
128
|
|
|
$command, |
|
129
|
|
|
$params |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
public function getResponseHeaders(string $responseString) :array { |
|
134
|
|
|
return [ |
|
135
|
2 |
|
'Content-Type' => 'application/x-www-form-urlencoded', |
|
136
|
2 |
|
'Content-Length' => strlen($responseString) |
|
137
|
|
|
]; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
2 |
|
private function postDataEncoder(array $data) :string { |
|
141
|
2 |
|
$string = ''; |
|
142
|
|
|
|
|
143
|
2 |
|
foreach ($data as $k => $v) { |
|
144
|
2 |
|
$string .= $k . '=' . urlencode($v) . '&'; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
2 |
|
return $string; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.