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
|
|
|
use Teebot\Traits\Property; |
16
|
|
|
|
17
|
|
|
class Config |
18
|
|
|
{ |
19
|
|
|
use Property; |
20
|
|
|
|
21
|
|
|
const DEFAULT_LIMIT = 1; |
22
|
|
|
|
23
|
|
|
const DEFAULT_OFFSET = -1; |
24
|
|
|
|
25
|
|
|
const REQUEST_TIMEOUT = 6; |
26
|
|
|
|
27
|
|
|
const BOT_PREFIX = 'bot'; |
28
|
|
|
|
29
|
|
|
const COMMAND_NAMESPACE_PATTERN = 'Teebot\\Bot\\%s\\Command'; |
30
|
|
|
|
31
|
|
|
const EVENT_NAMESPACE_PATTERN = 'Teebot\\Bot\\%s\\EntityEvent'; |
32
|
|
|
|
33
|
|
|
const CONFIG_FILENAME = 'config.php'; |
34
|
|
|
|
35
|
|
|
const BOT_DIR_PATTERN = '%s/../Bot/%s'; |
36
|
|
|
|
37
|
|
|
protected $name = null; |
38
|
|
|
|
39
|
|
|
protected $token; |
40
|
|
|
|
41
|
|
|
protected $url = 'https://api.telegram.org'; |
42
|
|
|
|
43
|
|
|
protected $timeout = 1; |
44
|
|
|
|
45
|
|
|
protected $method; |
46
|
|
|
|
47
|
|
|
protected $file_url = 'https://api.telegram.org/file/bot'; |
48
|
|
|
|
49
|
|
|
protected $log_file = null; |
50
|
|
|
|
51
|
|
|
protected $events; |
52
|
|
|
|
53
|
|
|
protected $command_on_first = true; |
54
|
|
|
|
55
|
|
|
protected $commandNamespace = null; |
56
|
|
|
|
57
|
|
|
protected $entityEventNamespace = null; |
58
|
|
|
|
59
|
|
|
protected $botDir = null; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructs configuration object with either bot name or bot config file passed. |
63
|
|
|
* |
64
|
|
|
* @param string $botConfig Path to bot's configuration file |
65
|
|
|
*/ |
66
|
|
|
public function __construct($botConfig = '') |
67
|
|
|
{ |
68
|
|
|
$this->initBotConfiguration($botConfig); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Initialises bot configuration via bot name or configuration file. |
73
|
|
|
* |
74
|
|
|
* @param string $botConfig Path to bot's configuration file |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function initBotConfiguration($botConfig = '') |
79
|
|
|
{ |
80
|
|
|
try { |
81
|
|
|
|
82
|
|
|
if (empty($botConfig)) { |
83
|
|
|
Output::log(new Fatal("Path to configuration file was not set!")); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->loadConfigFile($botConfig); |
87
|
|
|
} catch (Fatal $e) { |
88
|
|
|
Output::log($e); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Loads configuration file in JSON format. |
96
|
|
|
* |
97
|
|
|
* @param string $configFile Path to configuration file |
98
|
|
|
*/ |
99
|
|
|
protected function loadConfigFile($configFile) |
100
|
|
|
{ |
101
|
|
|
if (!is_file($configFile) || !is_readable($configFile)) { |
102
|
|
|
Output::log(new Fatal('File "' . $configFile . '" does not exists or not readable!')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$config = require_once($configFile); |
106
|
|
|
|
107
|
|
|
$this->setProperties($config); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns bot token string, if the value was not set in config - default value will be used |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getToken() |
116
|
|
|
{ |
117
|
|
|
return $this->token; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns Bot-API request url |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getUrl() |
126
|
|
|
{ |
127
|
|
|
return $this->url; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns request timeout in seconds, if the value was not set in config - default value will be used |
132
|
|
|
* |
133
|
|
|
* @return int |
134
|
|
|
*/ |
135
|
|
|
public function getTimeout() |
136
|
|
|
{ |
137
|
|
|
return $this->timeout; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns name of the bot if it was set |
142
|
|
|
* |
143
|
|
|
* @return null|string |
144
|
|
|
*/ |
145
|
|
|
public function getName() |
146
|
|
|
{ |
147
|
|
|
return $this->name; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns request method name |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public function getMethod() |
156
|
|
|
{ |
157
|
|
|
return $this->method; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns whether command should be searched on first position in text. |
162
|
|
|
* |
163
|
|
|
* @return boolean |
164
|
|
|
*/ |
165
|
|
|
public function getCommandOnFirst() |
166
|
|
|
{ |
167
|
|
|
return $this->command_on_first; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns command's name space if bots are placed in default Bot directory |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
public function getCommandNamespace() |
176
|
|
|
{ |
177
|
|
|
return $this->commandNamespace; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns entity's name space if bots are placed in default Bot directory |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public function getEntityEventNamespace() |
186
|
|
|
{ |
187
|
|
|
return $this->entityEventNamespace; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Returns base url from the files to download from Telegram's storage servers, if the value |
192
|
|
|
* was not set in config - default value will be used |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function getFileUrl() |
197
|
|
|
{ |
198
|
|
|
return $this->file_url; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Returns path to log file for Errors, if not set - all errors will be echoed. |
203
|
|
|
* |
204
|
|
|
* @return null|string |
205
|
|
|
*/ |
206
|
|
|
public function getLogFile() |
207
|
|
|
{ |
208
|
|
|
return $this->log_file; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns an array with defined events map, if not set default namespaces and mapping will be used. |
213
|
|
|
* |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
|
|
public function getEvents() |
217
|
|
|
{ |
218
|
|
|
return $this->events; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Returns base url for the files to download from Telegram's storage servers, token will be also added. |
223
|
|
|
* If the value of file url was not set in config - default value will be used |
224
|
|
|
* |
225
|
|
|
* @return null|string |
226
|
|
|
*/ |
227
|
|
|
public function getFileBasePath() |
228
|
|
|
{ |
229
|
|
|
if (empty($this->file_url) || empty($this->token)) { |
230
|
|
|
return null; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $this->file_url . $this->token . '/'; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|