-
Notifications
You must be signed in to change notification settings - Fork 9
/
BotApp.php
191 lines (166 loc) · 4.28 KB
/
BotApp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace pimax\bitrix24;
/**
* Class BotApp
*
* @package pimax\bitrix24
*/
class BotApp
{
/**
* Current language
*
* @var string
*/
protected $language = "en";
/**
* Default language
*
* @var string
*/
protected $default_language = "en";
/**
* I18n messages
*
* @var array
*/
protected $messages = [];
/**
* Auth info
*
* @var array
*/
protected $auth = [];
/**
* Portal params
*
* @var array
*/
protected $params = [];
/**
* BotApp constructor.
*
* @param $auth Auth token
*/
public function __construct($auth)
{
$this->auth = $auth;
$this->params = $this->loadParams();
$this->language = $this->params[$_REQUEST['auth']['application_token']]['LANGUAGE_ID'];
$this->messages = $this->loadMessages();
}
/**
* Bot install action
*
* @param Bot $bot
*/
public function install(Bot $bot)
{
$result = $this->call('imbot.register', $bot->getData(), $this->auth);
$appsConfig = [];
$appsConfig[$_REQUEST['auth']['application_token']] = array(
'BOT_ID' => $result['result'],
'LANGUAGE_ID' => $_REQUEST['data']['LANGUAGE_ID'],
);
$this->saveParams($appsConfig);
}
/**
* Bot uninstall action
*
* @return bool
*/
public function uninstall()
{
$configFileName = '/config_' . trim(str_replace('.', '_', $_REQUEST['auth']['domain'])) . '.php';
if (file_exists(__DIR__.'/../../../'.$configFileName)) {
unlink($configFileName);
}
return true;
}
/**
* Send message action
*
* @param Message $message
* @return mixed
*/
public function send(Message $message)
{
return $this->call('imbot.message.add', $message->getData(), $this->auth);
}
/**
* Save portal params
*
* @param $params
* @return bool
*/
public function saveParams($params)
{
$config = "<?php\n";
$config .= "return " . var_export($params, true) . ";\n";
$configFileName = '/config_' . trim(str_replace('.', '_', $_REQUEST['auth']['domain'])) . '.php';
file_put_contents(__DIR__ . '/../../../' . $configFileName, $config);
return true;
}
/**
* Get i18n message for string
*
* @param $text string to translate
* @return string
*/
public function t($text)
{
if ($this->language != $this->default_language && !empty($this->messages[$text])) {
return $this->messages[$text];
}
return $text;
}
/**
* API request
*
* @param string $method Method
* @param array $params POST data
* @return mixed
*/
protected function call($method, array $params = array())
{
$queryUrl = 'https://' . $this->auth['domain'] . '/rest/' . $method;
$queryData = http_build_query(array_merge($params, array('auth' => $this->auth['access_token'])));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $queryUrl,
CURLOPT_POSTFIELDS => $queryData,
));
$result = curl_exec($curl);
curl_close($curl);
$result = json_decode($result, 1);
return $result;
}
/**
* Load portal params
*
* @return array
*/
protected function loadParams()
{
$configFileName = '/config_' . trim(str_replace('.', '_', $_REQUEST['auth']['domain'])) . '.php';
if (file_exists(realpath(__DIR__ . '/../../../' . $configFileName))) {
return include __DIR__ . '/../../../' . $configFileName;
}
return false;
}
/**
* Load i18n messages
*
* @return array|mixed
*/
protected function loadMessages()
{
if ($this->language != $this->default_language && file_exists(realpath(__DIR__.'/../../../messages/'.$this->language.'.php'))) {
return include __DIR__.'/../../../messages/'.$this->language.'.php';
}
return [];
}
}