1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nopolabs\Yabot\Plugins\Examples; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Nopolabs\Yabot\Guzzle\Guzzle; |
7
|
|
|
use Nopolabs\Yabot\Helpers\GuzzleTrait; |
8
|
|
|
use Nopolabs\Yabot\Message\Message; |
9
|
|
|
use Nopolabs\Yabot\Plugin\PluginInterface; |
10
|
|
|
use Nopolabs\Yabot\Plugin\PluginTrait; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
|
14
|
|
|
class YesOrNoPlugin implements PluginInterface |
15
|
|
|
{ |
16
|
|
|
use PluginTrait; |
17
|
|
|
use GuzzleTrait; |
18
|
|
|
|
19
|
|
|
public function __construct( |
20
|
|
|
Guzzle $guzzle, |
21
|
|
|
LoggerInterface $logger) |
22
|
|
|
{ |
23
|
|
|
$this->setGuzzle($guzzle); |
24
|
|
|
$this->setLog($logger); |
25
|
|
|
|
26
|
|
|
$this->setConfig([ |
27
|
|
|
'matchers' => [ |
28
|
|
|
'yes' => "/\\byes\\b/i", |
29
|
|
|
'no' => "/\\bno\\b/i", |
30
|
|
|
'maybe' => "/\\bmaybe\\b/i", |
31
|
|
|
'questionMark' => "/\?/i", |
32
|
|
|
], |
33
|
|
|
]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function yes(Message $msg, array $matches) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$this->respond($msg, 'yes'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function no(Message $msg, array $matches) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->respond($msg, 'no'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function maybe(Message $msg, array $matches) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$this->respond($msg, 'maybe'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function questionMark(Message $msg, array $matches) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this->respond($msg); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function respond(Message $msg, $force = null) |
57
|
|
|
{ |
58
|
|
|
$url = $this->url($force); |
59
|
|
|
$this->guzzle->getAsync($url) |
60
|
|
|
->then( |
61
|
|
|
function (ResponseInterface $response) use ($msg) { |
62
|
|
|
$json = $response->getBody(); |
63
|
|
|
$rsp = json_decode($json); |
64
|
|
|
|
65
|
|
|
$msg->reply($rsp->image); |
66
|
|
|
} |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function url($force = null) |
71
|
|
|
{ |
72
|
|
|
$query = $force ? "?force=$force" : ''; |
73
|
|
|
|
74
|
|
|
return "https://yesno.wtf/api/$query"; |
75
|
|
|
} |
76
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.