Skip to content

Commit 9a197b2

Browse files
committed
Added urban dictionary command
1 parent 7f87f24 commit 9a197b2

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

Diff for: cli/run.php

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Room11\Jeeves\Chat\Message\Factory as MessageFactory;
1212

1313
use Room11\Jeeves\Chat\Command\Version as VersionCommand;
14+
use Room11\Jeeves\Chat\Command\Urban as UrbanCommand;
1415

1516
use Amp\Websocket\Handshake;
1617
use Room11\Jeeves\WebSocket\Handler;
@@ -33,6 +34,7 @@
3334

3435
$commands = (new CommandCollection())
3536
->register(new VersionCommand($httpClient, $chatKey))
37+
->register(new UrbanCommand($httpClient, $chatKey))
3638
;
3739

3840
\Amp\run(function () use ($webSocketUrl, $httpClient, $chatKey, $roomCollection, $commands, $logger) {

Diff for: src/Chat/Command/Urban.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Room11\Jeeves\Chat\Command;
4+
5+
use Room11\Jeeves\Chat\Message\Message;
6+
use Amp\Artax\Client as HttpClient;
7+
use Amp\Artax\Request;
8+
use Amp\Artax\FormBody;
9+
10+
class Urban implements Command
11+
{
12+
const COMMAND = 'urban';
13+
14+
private $httpClient;
15+
16+
private $chatKey;
17+
18+
public function __construct(HttpClient $httpClient, string $chatKey)
19+
{
20+
$this->httpClient = $httpClient;
21+
$this->chatKey = $chatKey;
22+
}
23+
24+
public function handle(Message $message): \Generator
25+
{
26+
if (!$this->validMessage($message)) {
27+
return;
28+
}
29+
30+
yield from $this->getResult($message);
31+
}
32+
33+
private function validMessage(Message $message): bool
34+
{
35+
return get_class($message) === 'Room11\Jeeves\Chat\Message\NewMessage'
36+
&& strpos($message->getContent(), '!!urban') === 0
37+
&& count(explode(' ', trim($message->getContent())) > 1);
38+
}
39+
40+
private function getResult(Message $message): \Generator
41+
{
42+
$fullCommand = explode(' ', trim($message->getContent()));
43+
44+
array_shift($fullCommand);
45+
46+
$promise = $this->httpClient->request('http://api.urbandictionary.com/v0/define?term=' . implode('%20', $fullCommand));
47+
48+
$response = yield $promise;
49+
50+
$result = json_decode($response->getBody(), true);
51+
52+
yield from $this->postResult($message, $result);
53+
}
54+
55+
private function postResult(Message $message, array $result)
56+
{
57+
$body = (new FormBody)
58+
->addField('text', sprintf('[ [%s](%s) ] %s', $result['list'][0]['word'], $result['list'][0]['permalink'], str_replace('\r\n', "\r\n", $result['list'][0]['definition'])))
59+
->addField('fkey', $this->chatKey);
60+
61+
$request = (new Request)
62+
->setUri('http://chat.stackoverflow.com/chats/' . $message->getRoomid() . '/messages/new')
63+
->setMethod('POST')
64+
->setBody($body);
65+
66+
$promise = $this->httpClient->request($request);
67+
68+
yield $promise;
69+
}
70+
}

0 commit comments

Comments
 (0)