|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Zanzara; |
|
6
|
|
|
|
|
7
|
|
|
use Clue\React\HttpProxy\ProxyConnector; |
|
8
|
|
|
use DI\Container; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use React\Cache\ArrayCache; |
|
11
|
|
|
use React\Cache\CacheInterface; |
|
12
|
|
|
use React\EventLoop\Factory; |
|
13
|
|
|
use React\EventLoop\LoopInterface; |
|
14
|
|
|
use React\Http\Browser; |
|
15
|
|
|
use React\Http\Server; |
|
16
|
|
|
use React\Promise\PromiseInterface; |
|
17
|
|
|
use React\Socket\Connector; |
|
18
|
|
|
use Zanzara\Listener\ListenerResolver; |
|
19
|
|
|
use Zanzara\Telegram\Telegram; |
|
20
|
|
|
use Zanzara\UpdateMode\ReactPHPWebhook; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
class Zanzara extends ListenerResolver |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Config |
|
30
|
|
|
*/ |
|
31
|
|
|
private $config; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Telegram |
|
35
|
|
|
*/ |
|
36
|
|
|
private $telegram; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var LoopInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $loop; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var ZanzaraCache |
|
45
|
|
|
*/ |
|
46
|
|
|
private $cache; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $botToken |
|
50
|
|
|
* @param Config|null $config |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(string $botToken, ?Config $config = null) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->config = $config ?? new Config(); |
|
55
|
|
|
$this->config->setBotToken($botToken); |
|
56
|
|
|
$this->container = $this->config->getContainer() ?? new Container(); |
|
57
|
|
|
$this->loop = $this->config->getLoop() ?? Factory::create(); |
|
58
|
|
|
$this->container->set(LoopInterface::class, $this->loop); // loop cannot be created by container |
|
59
|
|
|
$this->container->set(LoggerInterface::class, $this->config->getLogger()); |
|
60
|
|
|
$connector = $this->config->getConnector(); |
|
61
|
|
|
$connectorOptions = $this->config->getConnectorOptions(); |
|
62
|
|
|
$proxyUrl = $this->config->getProxyUrl(); |
|
63
|
|
|
$proxyHttpHeaders = $this->config->getProxyHttpHeaders(); |
|
64
|
|
|
if (!$connector && (!empty($connectorOptions) || $proxyUrl || !empty($proxyHttpHeaders))) { |
|
65
|
|
|
if ($proxyUrl) { |
|
66
|
|
|
$proxy = new ProxyConnector($proxyUrl, new Connector($this->loop), $proxyHttpHeaders); |
|
67
|
|
|
$connectorOptions['tcp'] = $proxy; |
|
68
|
|
|
} |
|
69
|
|
|
$connector = new Connector($this->loop, $connectorOptions); |
|
70
|
|
|
$this->config->setConnector($connector); |
|
71
|
|
|
} |
|
72
|
|
|
$this->container->set(Browser::class, (new Browser($this->loop, $this->config->getConnector())) // browser cannot be created by container |
|
73
|
|
|
->withBase("{$this->config->getApiTelegramUrl()}/bot{$botToken}/")); |
|
74
|
|
|
$this->telegram = $this->container->get(Telegram::class); |
|
75
|
|
|
$this->container->set(CacheInterface::class, $this->config->getCache() ?? new ArrayCache()); |
|
76
|
|
|
$this->container->set(Config::class, $this->config); |
|
77
|
|
|
if ($this->config->isReactFileSystem()) { |
|
78
|
|
|
$this->container->set(\React\Filesystem\Filesystem::class, \React\Filesystem\Filesystem::create($this->loop)); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
$this->cache = $this->container->get(ZanzaraCache::class); |
|
81
|
|
|
$this->conversationManager = $this->container->get(ConversationManager::class); |
|
82
|
|
|
$this->container->set(Zanzara::class, $this); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function run(): void |
|
86
|
|
|
{ |
|
87
|
|
|
$this->feedMiddlewareStack(); |
|
88
|
|
|
// we set "string|UpdateModeInterface" as return type just to have IDE suggestions, actually it is always a string |
|
89
|
|
|
$this->container->get(/** @scrutinizer ignore-type */ $this->config->getUpdateMode())->run(); |
|
90
|
|
|
$this->loop->run(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return Telegram |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getTelegram(): Telegram |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->telegram; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return LoopInterface |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getLoop(): LoopInterface |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->loop; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return Server |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getServer(): Server |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->container->get(ReactPHPWebhook::class)->getServer(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return Container |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getContainer(): Container |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->container; |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Sets an item of the global data. |
|
127
|
|
|
* This cache is not related to any chat or user. |
|
128
|
|
|
* |
|
129
|
|
|
* Eg: |
|
130
|
|
|
* $ctx->setGlobalData('age', 21)->then(function($result) { |
|
131
|
|
|
* |
|
132
|
|
|
* }); |
|
133
|
|
|
* |
|
134
|
|
|
* @param $key |
|
135
|
|
|
* @param $data |
|
136
|
|
|
* @param $ttl |
|
137
|
|
|
* @return PromiseInterface |
|
138
|
|
|
*/ |
|
139
|
|
|
public function setGlobalData($key, $data, $ttl = false) |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->cache->setGlobalCacheData($key, $data, $ttl); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Append data to an existing global cache item. The item value is always an array. |
|
146
|
|
|
* |
|
147
|
|
|
* Eg: |
|
148
|
|
|
* $ctx->appendGlobalData('users', ['Mike', 'John'])->then(function($result) { |
|
149
|
|
|
* |
|
150
|
|
|
* }); |
|
151
|
|
|
* |
|
152
|
|
|
* @param $key |
|
153
|
|
|
* @param $data |
|
154
|
|
|
* @param $ttl |
|
155
|
|
|
* @return PromiseInterface |
|
156
|
|
|
*/ |
|
157
|
|
|
public function appendGlobalData($key, $data, $ttl = false): PromiseInterface |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->cache->appendGlobalCacheData($key, $data, $ttl); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Returns all the global data. |
|
164
|
|
|
* This cache is not related to any chat or user. |
|
165
|
|
|
* |
|
166
|
|
|
* Eg: |
|
167
|
|
|
* $ctx->getGlobalData()->then(function($data) { |
|
168
|
|
|
* $age = $data['age']; |
|
169
|
|
|
* }); |
|
170
|
|
|
* |
|
171
|
|
|
* @return PromiseInterface |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getGlobalData() |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->cache->getGlobalCacheData(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Gets an item of the global data. |
|
180
|
|
|
* This cache is not related to any chat or user. |
|
181
|
|
|
* |
|
182
|
|
|
* Eg: |
|
183
|
|
|
* $ctx->getGlobalDataItem('age')->then(function($age) { |
|
184
|
|
|
* |
|
185
|
|
|
* }); |
|
186
|
|
|
* |
|
187
|
|
|
* @param $key |
|
188
|
|
|
* @return PromiseInterface |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getGlobalDataItem($key) |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->cache->getCacheGlobalDataItem($key); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Deletes an item from the global data. |
|
197
|
|
|
* This cache is not related to any chat or user. |
|
198
|
|
|
* |
|
199
|
|
|
* Eg: |
|
200
|
|
|
* $ctx->deleteGlobalDataItem('age')->then(function($result) { |
|
201
|
|
|
* |
|
202
|
|
|
* }); |
|
203
|
|
|
* |
|
204
|
|
|
* @param $key |
|
205
|
|
|
* @return PromiseInterface |
|
206
|
|
|
*/ |
|
207
|
|
|
public function deleteGlobalDataItem($key) |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->cache->deleteCacheItemGlobalData($key); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Deletes all global data. |
|
214
|
|
|
* |
|
215
|
|
|
* Eg: |
|
216
|
|
|
* $ctx->deleteGlobalData()->then(function($result) { |
|
217
|
|
|
* |
|
218
|
|
|
* }); |
|
219
|
|
|
* |
|
220
|
|
|
* @return PromiseInterface |
|
221
|
|
|
*/ |
|
222
|
|
|
public function deleteGlobalData() |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->cache->deleteCacheGlobalData(); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Wipe entire cache. |
|
229
|
|
|
* |
|
230
|
|
|
* @return PromiseInterface |
|
231
|
|
|
*/ |
|
232
|
|
|
public function wipeCache() |
|
233
|
|
|
{ |
|
234
|
|
|
return $this->cache->wipeCache(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
} |
|
238
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths