-
Notifications
You must be signed in to change notification settings - Fork 25
/
service.php
302 lines (252 loc) · 8.39 KB
/
service.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
error_reporting(0);
#####################
### CONFIG OF BOT ###
#####################
define('DEBUG_FILE_NAME', ''); // if you need read debug log, you should write unique log name
define('CLIENT_ID', ''); // like 'app.67efrrt2990977.85678329' or 'local.57062d3061fc71.97850406' - This code should take in a partner's site, needed only if you want to write a message from Bot at any time without initialization by the user
define('CLIENT_SECRET', ''); // like '8bb00435c88aaa3028a0d44320d60339' - TThis code should take in a partner's site, needed only if you want to write a message from Bot at any time without initialization by the user
#####################
writeToLog($_REQUEST, 'ImBot Event Query');
$appsConfig = Array();
if (file_exists(__DIR__.'/config.php'))
include(__DIR__.'/config.php');
// receive event "new message"
if ($_REQUEST['event'] == 'ONIMBOTMESSAGEADD')
{
// check the event - authorize this event or not
if (!isset($appsConfig[$_REQUEST['auth']['application_token']]))
return false;
// write debug log
writeToLog($_REQUEST['data'], 'ImBot Event message add');
}
// receive event "update message"
else if ($_REQUEST['event'] == 'ONIMBOTMESSAGEUPDATE')
{
// check the event - authorize this event or not
if (!isset($appsConfig[$_REQUEST['auth']['application_token']]))
return false;
// write debug log
writeToLog($_REQUEST['data'], 'ImBot Event message add');
}
// receive event "delete message"
else if ($_REQUEST['event'] == 'ONIMBOTMESSAGEDELETE')
{
// check the event - authorize this event or not
if (!isset($appsConfig[$_REQUEST['auth']['application_token']]))
return false;
// write debug log
writeToLog($_REQUEST['data'], 'ImBot Event message add');
}
// receive event "open private dialog with bot" or "join bot to group chat"
else if ($_REQUEST['event'] == 'ONIMBOTJOINCHAT')
{
// check the event - authorize this event or not
if (!isset($appsConfig[$_REQUEST['auth']['application_token']]))
return false;
// write debug log
writeToLog($_REQUEST['data'], 'ImBot Event join chat');
}
// receive event "delete chat-bot"
else if ($_REQUEST['event'] == 'ONIMBOTDELETE')
{
// check the event - authorize this event or not
if (!isset($appsConfig[$_REQUEST['auth']['application_token']]))
return false;
// unset application variables
unset($appsConfig[$_REQUEST['auth']['application_token']]);
// save params
saveParams($appsConfig);
// write debug log
writeToLog($_REQUEST['event'], 'ImBot unregister');
}
// receive event "Application install"
else if ($_REQUEST['event'] == 'ONAPPINSTALL')
{
// handler for events
$handlerBackUrl = ($_SERVER['SERVER_PORT']==443||$_SERVER["HTTPS"]=="on"? 'https': 'http')."://".$_SERVER['SERVER_NAME'].(in_array($_SERVER['SERVER_PORT'], Array(80, 443))?'':':'.$_SERVER['SERVER_PORT']).$_SERVER['SCRIPT_NAME'];
// If your application supports different localizations
// use $_REQUEST['data']['LANGUAGE_ID'] to load correct localization
// register new bot
$result = restCommand('imbot.register', Array(
'CODE' => 'servicebot',
'TYPE' => 'S',
'EVENT_MESSAGE_ADD' => $handlerBackUrl,
'EVENT_MESSAGE_UPDATE' => $handlerBackUrl,
'EVENT_MESSAGE_DELETE' => $handlerBackUrl,
'EVENT_WELCOME_MESSAGE' => $handlerBackUrl,
'EVENT_BOT_DELETE' => $handlerBackUrl,
'PROPERTIES' => Array(
'NAME' => 'ServiceBot '.(count($appsConfig)+1),
'COLOR' => 'GREEN',
'EMAIL' => '[email protected]',
'PERSONAL_BIRTHDAY' => '2016-03-11',
'WORK_POSITION' => 'My first service bot',
'PERSONAL_WWW' => 'http://bitrix24.com',
'PERSONAL_GENDER' => 'M',
'PERSONAL_PHOTO' => base64_encode(file_get_contents(__DIR__.'/avatar.png')),
)
), $_REQUEST["auth"]);
$botId = $result['result'];
$result = restCommand('event.bind', Array(
'EVENT' => 'OnAppUpdate',
'HANDLER' => $handlerBackUrl
), $_REQUEST["auth"]);
// save params
$appsConfig[$_REQUEST['auth']['application_token']] = Array(
'BOT_ID' => $botId,
'COMMAND_ECHO' => $commandEcho,
'COMMAND_HELP' => $commandHelp,
'COMMAND_LIST' => $commandList,
'LANGUAGE_ID' => $_REQUEST['data']['LANGUAGE_ID'],
'AUTH' => $_REQUEST['auth'],
);
saveParams($appsConfig);
// write debug log
writeToLog(Array($botId, $commandEcho, $commandHelp, $commandList), 'ImBot register');
}
// receive event "Application install"
else if ($_REQUEST['event'] == 'ONAPPUPDATE')
{
// check the event - authorize this event or not
if (!isset($appsConfig[$_REQUEST['auth']['application_token']]))
return false;
if ($_REQUEST['data']['VERSION'] == 2)
{
// Some logic in update event for VERSION 2
// You can execute any method RestAPI, BotAPI or ChatAPI, for example delete or add a new command to the bot
/*
$result = restCommand('...', Array(
'...' => '...',
), $_REQUEST["auth"]);
*/
/*
For example delete "Echo" command:
$result = restCommand('imbot.command.unregister', Array(
'COMMAND_ID' => $appsConfig[$_REQUEST['auth']['application_token']]['COMMAND_ECHO'],
), $_REQUEST["auth"]);
*/
}
else
{
// send answer message
$result = restCommand('app.info', array(), $_REQUEST["auth"]);
}
// write debug log
writeToLog($result, 'ImBot update event');
}
/**
* Save application configuration.
* WARNING: this method is only created for demonstration, never store config like this
*
* @param $params
* @return bool
*/
function saveParams($params)
{
$config = "<?php\n";
$config .= "\$appsConfig = ".var_export($params, true).";\n";
$config .= "?>";
file_put_contents(__DIR__."/config.php", $config);
return true;
}
/**
* Send rest query to Bitrix24.
*
* @param $method - Rest method, ex: methods
* @param array $params - Method params, ex: Array()
* @param array $auth - Authorize data, received from event
* @param boolean $authRefresh - If authorize is expired, refresh token
* @return mixed
*/
function restCommand($method, array $params = Array(), array $auth = Array(), $authRefresh = true)
{
$queryUrl = $auth["client_endpoint"].$method;
$queryData = http_build_query(array_merge($params, array("auth" => $auth["access_token"])));
writeToLog(Array('URL' => $queryUrl, 'PARAMS' => array_merge($params, array("auth" => $auth["access_token"]))), 'ImBot send data');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 1,
CURLOPT_URL => $queryUrl,
CURLOPT_POSTFIELDS => $queryData,
));
$result = curl_exec($curl);
curl_close($curl);
$result = json_decode($result, 1);
if ($authRefresh && isset($result['error']) && in_array($result['error'], array('expired_token', 'invalid_token')))
{
$auth = restAuth($auth);
if ($auth)
{
$result = restCommand($method, $params, $auth, false);
}
}
return $result;
}
/**
* Get new authorize data if you authorize is expire.
*
* @param array $auth - Authorize data, received from event
* @return bool|mixed
*/
function restAuth($auth)
{
if (!CLIENT_ID || !CLIENT_SECRET)
return false;
if(!isset($auth['refresh_token']))
return false;
$queryUrl = 'https://oauth.bitrix.info/oauth/token/';
$queryData = http_build_query($queryParams = array(
'grant_type' => 'refresh_token',
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SECRET,
'refresh_token' => $auth['refresh_token'],
));
writeToLog(Array('URL' => $queryUrl, 'PARAMS' => $queryParams), 'ImBot request auth data');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $queryUrl.'?'.$queryData,
));
$result = curl_exec($curl);
curl_close($curl);
$result = json_decode($result, 1);
if (!isset($result['error']))
{
$appsConfig = Array();
if (file_exists(__DIR__.'/config.php'))
include(__DIR__.'/config.php');
$result['application_token'] = $auth['application_token'];
$appsConfig[$auth['application_token']]['AUTH'] = $result;
saveParams($appsConfig);
}
else
{
$result = false;
}
return $result;
}
/**
* Write data to log file. (by default disabled)
* WARNING: this method is only created for demonstration, never store log file in public folder
*
* @param mixed $data
* @param string $title
* @return bool
*/
function writeToLog($data, $title = '')
{
if (!DEBUG_FILE_NAME)
return false;
$log = "\n------------------------\n";
$log .= date("Y.m.d G:i:s")."\n";
$log .= (strlen($title) > 0 ? $title : 'DEBUG')."\n";
$log .= print_r($data, 1);
$log .= "\n------------------------\n";
file_put_contents(__DIR__."/".DEBUG_FILE_NAME, $log, FILE_APPEND);
return true;
}