|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nopolabs\Yabot\Plugins\Giphy; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
|
7
|
|
|
use function GuzzleHttp\Promise\settle; |
|
8
|
|
|
use Nopolabs\Yabot\Message\Message; |
|
9
|
|
|
use Nopolabs\Yabot\Plugin\PluginInterface; |
|
10
|
|
|
use Nopolabs\Yabot\Plugin\PluginTrait; |
|
11
|
|
|
use Nopolabs\Yabot\Helpers\GuzzleTrait; |
|
12
|
|
|
use Psr\Log\LoggerInterface; |
|
13
|
|
|
|
|
14
|
|
|
class GiphyPlugin implements PluginInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use PluginTrait; |
|
17
|
|
|
use GuzzleTrait; |
|
18
|
|
|
|
|
19
|
|
|
private $giphyService; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct( |
|
22
|
|
|
LoggerInterface $logger, |
|
23
|
|
|
GiphyService $giphyService, |
|
24
|
|
|
array $config = []) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->setLog($logger); |
|
27
|
|
|
$this->giphyService = $giphyService; |
|
28
|
|
|
$this->setConfig(array_merge( |
|
29
|
|
|
[ |
|
30
|
|
|
'help' => '<prefix> [search terms] [optional format] (giphy formats to list available)', |
|
31
|
|
|
'prefix' => 'giphy', |
|
32
|
|
|
'matchers' => [ |
|
33
|
|
|
'each' => '/^\*\s+(.*)/', |
|
34
|
|
|
'search' => '/^(.*)/', |
|
35
|
|
|
], |
|
36
|
|
|
'format' => 'fixed_width', |
|
37
|
|
|
'formats' => [ |
|
38
|
|
|
'fixed_height', |
|
39
|
|
|
'fixed_height_still', |
|
40
|
|
|
'fixed_height_downsampled', |
|
41
|
|
|
'fixed_width', |
|
42
|
|
|
'fixed_width_still', |
|
43
|
|
|
'fixed_width_downsampled', |
|
44
|
|
|
'fixed_height_small', |
|
45
|
|
|
'fixed_height_small_still', |
|
46
|
|
|
'fixed_width_small', |
|
47
|
|
|
'fixed_width_small_still', |
|
48
|
|
|
'preview', |
|
49
|
|
|
'downsized_small', |
|
50
|
|
|
'downsized', |
|
51
|
|
|
'downsized_medium', |
|
52
|
|
|
'downsized_large', |
|
53
|
|
|
'downsized_still', |
|
54
|
|
|
'original', |
|
55
|
|
|
'original_still', |
|
56
|
|
|
'looping', |
|
57
|
|
|
], |
|
58
|
|
|
], |
|
59
|
|
|
$config |
|
60
|
|
|
)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function each(Message $msg, array $matches) |
|
64
|
|
|
{ |
|
65
|
|
|
$requests = []; |
|
66
|
|
|
|
|
67
|
|
|
$format = $this->getDefaultFormat(); |
|
68
|
|
|
|
|
69
|
|
|
$terms = preg_split('/[\s,]+/', $matches[1]); |
|
70
|
|
|
|
|
71
|
|
|
foreach ($terms as $term) { |
|
72
|
|
|
$requests[$term] = $this->giphyService->search($term, $format); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$results = settle($requests)->wait(); |
|
76
|
|
|
foreach ($results as $term => $result) { |
|
77
|
|
|
if ($result['state'] === PromiseInterface::FULFILLED) { |
|
78
|
|
|
$gifUrl = $result['value']; |
|
79
|
|
|
$msg->reply($term.': '.$gifUrl); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$msg->setHandled(true); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function search(Message $msg, array $matches) |
|
87
|
|
|
{ |
|
88
|
|
|
if (trim($matches[1]) === 'formats') { |
|
89
|
|
|
$formats = $this->getFormats(); |
|
90
|
|
|
$msg->reply('Available formats: ' . implode(' ', $formats)); |
|
91
|
|
|
$msg->setHandled(true); |
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$terms = preg_split('/\s+/', $matches[1]); |
|
96
|
|
|
$query = $this->getQuery($terms); |
|
97
|
|
|
$format = $this->getFormat($terms); |
|
98
|
|
|
|
|
99
|
|
|
$this->giphyService->search($query, $format)->then( |
|
100
|
|
|
function (string $gifUrl) use ($msg) { |
|
101
|
|
|
$msg->reply($gifUrl); |
|
102
|
|
|
}, |
|
103
|
|
|
function (Exception $e) { |
|
104
|
|
|
$this->getLog()->warning($e->getMessage()); |
|
105
|
|
|
} |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
$msg->setHandled(true); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
protected function getQuery(array $terms) : string |
|
112
|
|
|
{ |
|
113
|
|
|
$search = []; |
|
114
|
|
|
$formats = $this->getFormats(); |
|
115
|
|
|
foreach ($terms as $term) { |
|
116
|
|
|
if (!in_array($term, $formats)) { |
|
117
|
|
|
$search[] = $term; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
$query = implode(' ', $search); |
|
121
|
|
|
|
|
122
|
|
|
return $query; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected function getFormat(array $terms) : string |
|
126
|
|
|
{ |
|
127
|
|
|
$format = $this->getDefaultFormat(); |
|
128
|
|
|
$formats = $this->getFormats(); |
|
129
|
|
|
foreach ($terms as $term) { |
|
130
|
|
|
if (in_array($term, $formats)) { |
|
131
|
|
|
$format = $term; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $format; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
protected function getDefaultFormat() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->get('format', 'fixed_width'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
protected function getFormats() : array |
|
144
|
|
|
{ |
|
145
|
|
|
$default = $this->getDefaultFormat(); |
|
146
|
|
|
|
|
147
|
|
|
return $this->get('formats', [$default]);; |
|
148
|
|
|
} |
|
149
|
|
|
} |