|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Configuration class that stores all configuration values required to run bots. |
|
5
|
|
|
* |
|
6
|
|
|
* @package Teebot (Telegram bot framework) |
|
7
|
|
|
* |
|
8
|
|
|
* @author Stanislav Drozdov <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Teebot; |
|
12
|
|
|
|
|
13
|
|
|
use Teebot\Exception\Fatal; |
|
14
|
|
|
use Teebot\Exception\Output; |
|
15
|
|
|
|
|
16
|
|
|
class Config |
|
17
|
|
|
{ |
|
18
|
|
|
const DEFAULT_LIMIT = 1; |
|
19
|
|
|
|
|
20
|
|
|
const DEFAULT_OFFSET = -1; |
|
21
|
|
|
|
|
22
|
|
|
const REQUEST_TIMEOUT = 6; |
|
23
|
|
|
|
|
24
|
|
|
const BOT_PREFIX = 'bot'; |
|
25
|
|
|
|
|
26
|
|
|
const COMMAND_NAMESPACE_PATTERN = 'Teebot\\Bot\\%s\\Command'; |
|
27
|
|
|
|
|
28
|
|
|
const EVENT_NAMESPACE_PATTERN = 'Teebot\\Bot\\%s\\EntityEvent'; |
|
29
|
|
|
|
|
30
|
|
|
const CONFIG_FILENAME = 'config.json'; |
|
31
|
|
|
|
|
32
|
|
|
const BOT_DIR_PATTERN = '%s/../Bot/%s'; |
|
33
|
|
|
|
|
34
|
|
|
protected $botName = null; |
|
35
|
|
|
|
|
36
|
|
|
protected $token; |
|
37
|
|
|
|
|
38
|
|
|
protected $url = 'https://api.telegram.org'; |
|
39
|
|
|
|
|
40
|
|
|
protected $timeout = 1; |
|
41
|
|
|
|
|
42
|
|
|
protected $method; |
|
43
|
|
|
|
|
44
|
|
|
protected $file_url = 'https://api.telegram.org/file/bot'; |
|
45
|
|
|
|
|
46
|
|
|
protected $log_file = null; |
|
47
|
|
|
|
|
48
|
|
|
protected $events; |
|
49
|
|
|
|
|
50
|
|
|
protected $commandNamespace = null; |
|
51
|
|
|
|
|
52
|
|
|
protected $entityEventNamespace = null; |
|
53
|
|
|
|
|
54
|
|
|
protected $botDir = null; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Constructs configuration object with either bot name or bot config file passed. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $botName The name of the bot to execute |
|
60
|
|
|
* @param string $botConfig Path to bot's configuration file |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct(string $botName = '', $botConfig = '') |
|
63
|
|
|
{ |
|
64
|
|
|
$this->initBotConfiguration($botName, $botConfig); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Initialises bot configuration via bot name or configuration file. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $botName The name of the bot to execute |
|
71
|
|
|
* @param string $botConfig Path to bot's configuration file |
|
72
|
|
|
* |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
public function initBotConfiguration(string $botName = '', $botConfig = '') |
|
76
|
|
|
{ |
|
77
|
|
|
$this->botName = $botName; |
|
78
|
|
|
|
|
79
|
|
|
try { |
|
80
|
|
|
if (!empty($botName)) { |
|
81
|
|
|
$this->botDir = $this->getBotDir($botName); |
|
82
|
|
|
$botConfig = $this->botDir . static::CONFIG_FILENAME; |
|
83
|
|
|
|
|
84
|
|
|
$this->setNamespaces($botName); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (empty($botConfig)) { |
|
88
|
|
|
Output::log(new Fatal("Path to configuration file was not sent!")); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->loadConfig($botConfig); |
|
92
|
|
|
} catch (Fatal $e) { |
|
93
|
|
|
Output::log($e); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns bot directory built with default path pattern. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $botName The name of the bot to execute |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function getBotDir(string $botName) : string |
|
107
|
|
|
{ |
|
108
|
|
|
$dir = sprintf( |
|
109
|
|
|
static::BOT_DIR_PATTERN, |
|
110
|
|
|
__DIR__, |
|
111
|
|
|
$botName |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
if (!file_exists($dir)) { |
|
115
|
|
|
Output::log(new Fatal('Bot does not exist!')); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return realpath($dir) . "/"; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Sets namespace for command and event entities classes to be able to load them via autoloader in the future. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $botName The name of the bot to execute |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function setNamespaces(string $botName) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->commandNamespace = sprintf(static::COMMAND_NAMESPACE_PATTERN, $botName); |
|
129
|
|
|
$this->entityEventNamespace = sprintf(static::EVENT_NAMESPACE_PATTERN, $botName); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Loads configuration file in JSON format. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $configFile Path to configuration file |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function loadConfig($configFile) |
|
138
|
|
|
{ |
|
139
|
|
|
if (!is_file($configFile) || !is_readable($configFile)) { |
|
140
|
|
|
Output::log(new Fatal('File "' . $configFile . '" does not exists or not readable!')); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$config = file_get_contents($configFile); |
|
144
|
|
|
$configArray = json_decode($config, true); |
|
145
|
|
|
|
|
146
|
|
|
foreach ($configArray as $name => $value) { |
|
147
|
|
|
if (property_exists($this, $name)) { |
|
148
|
|
|
$this->{$name} = $value; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Returns bot token string, if the value was not set in config - default value will be used |
|
155
|
|
|
* |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getToken() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->token; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Returns Bot-API request url |
|
165
|
|
|
* |
|
166
|
|
|
* @return string |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getUrl() |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->url; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Returns request timeout in seconds, if the value was not set in config - default value will be used |
|
175
|
|
|
* |
|
176
|
|
|
* @return int |
|
177
|
|
|
*/ |
|
178
|
|
|
public function getTimeout() |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->timeout; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns name of the bot if it was set |
|
185
|
|
|
* |
|
186
|
|
|
* @return null|string |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getBotName() |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->botName; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Returns request method name |
|
195
|
|
|
* |
|
196
|
|
|
* @return string |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getMethod() |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->method; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Returns command's name space if bots are placed in default Bot directory |
|
205
|
|
|
* |
|
206
|
|
|
* @return string |
|
207
|
|
|
*/ |
|
208
|
|
|
public function getCommandNamespace() |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->commandNamespace; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Returns entity's name space if bots are placed in default Bot directory |
|
215
|
|
|
* |
|
216
|
|
|
* @return string |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getEntityEventNamespace() |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->entityEventNamespace; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Returns base url from the files to download from Telegram's storage servers, if the value |
|
225
|
|
|
* was not set in config - default value will be used |
|
226
|
|
|
* |
|
227
|
|
|
* @return string |
|
228
|
|
|
*/ |
|
229
|
|
|
public function getFileUrl() |
|
230
|
|
|
{ |
|
231
|
|
|
return $this->file_url; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Returns path to log file for Errors, if not set - all errors will be echoed. |
|
236
|
|
|
* |
|
237
|
|
|
* @return null|string |
|
238
|
|
|
*/ |
|
239
|
|
|
public function getLogFile() |
|
240
|
|
|
{ |
|
241
|
|
|
return $this->log_file; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Returns an array with defined events map, if not set default namespaces and mapping will be used. |
|
246
|
|
|
* |
|
247
|
|
|
* @return array |
|
248
|
|
|
*/ |
|
249
|
|
|
public function getEvents() |
|
250
|
|
|
{ |
|
251
|
|
|
return $this->events; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Returns base url for the files to download from Telegram's storage servers, token will be also added. |
|
256
|
|
|
* If the value of file url was not set in config - default value will be used |
|
257
|
|
|
* |
|
258
|
|
|
* @return null|string |
|
259
|
|
|
*/ |
|
260
|
|
|
public function getFileBasePath() |
|
261
|
|
|
{ |
|
262
|
|
|
if (empty($this->file_url) || empty($this->token)) { |
|
263
|
|
|
return null; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
return $this->file_url . $this->token . '/'; |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|