|
1
|
|
|
<?php |
|
2
|
|
|
namespace Domains\Bot\Middlewares; |
|
3
|
|
|
|
|
4
|
|
|
use AlgoliaSearch\Client; |
|
5
|
|
|
use Domains\Message; |
|
6
|
|
|
use Domains\Middleware\MiddlewareInterface; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class LaravelDocumentationSearcherMiddleware |
|
11
|
|
|
*/ |
|
12
|
|
|
class LaravelDocumentationSearcherMiddleware implements MiddlewareInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param Message $message |
|
16
|
|
|
* @return mixed |
|
17
|
|
|
*/ |
|
18
|
|
|
public function handle(Message $message) |
|
19
|
|
|
{ |
|
20
|
|
|
if (preg_match('/^(@.*?\s)?(информация|доки|документация|larvel doc)\s+(?:про|по)?\s*(.*?)$/isu', $message->text, $matches)) { |
|
21
|
|
|
if (!trim($matches[3])) { |
|
22
|
|
|
return $message; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$client = new Client( |
|
26
|
|
|
'8BB87I11DE', |
|
27
|
|
|
'8e1d446d61fce359f69cd7c8b86a50de' |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
$result = $client->initIndex('docs')->search($matches[3]); |
|
31
|
|
|
|
|
32
|
|
|
if (! isset($result['hits'])) { |
|
33
|
|
|
$message->italic('По вашему запросу ничего не найдено'); |
|
34
|
|
|
return null; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$response = ''; |
|
38
|
|
|
|
|
39
|
|
|
$hits = new Collection($result['hits']); |
|
40
|
|
|
|
|
41
|
|
|
$hits->unique(function($row) { |
|
42
|
|
|
return $row['h1']; |
|
43
|
|
|
})->map(function($row) { |
|
44
|
|
|
$row['link'] = 'https://laravel.com/docs/5.2/'.$row['link']; |
|
45
|
|
|
return $row; |
|
46
|
|
|
})->take(3)->each(function($row) use(&$response) { |
|
47
|
|
|
$title = ''; |
|
48
|
|
|
foreach (['h1', 'h2', 'h3', 'h4', 'h5'] as $tag) { |
|
49
|
|
|
if (isset($row[$tag])) { |
|
50
|
|
|
$title .= ' : '.$row[$tag]; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$response .= "[*] [i][url={$row['link']}]{$title}[/url][/i]".PHP_EOL; |
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
if (! empty($response)) { |
|
58
|
|
|
$message->answer(trans('search.results', [ |
|
59
|
|
|
'results' => '[i]Вот что нашел в документации:[/i] [list]'.PHP_EOL.$response.PHP_EOL.'[/list]' |
|
60
|
|
|
])); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $message; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getGroup() |
|
75
|
|
|
{ |
|
76
|
|
|
return ['laravel', 'global']; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|