-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path02-chatbot.php
62 lines (45 loc) · 1.76 KB
/
02-chatbot.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
<?php
use Clue\React\Quassel\Factory;
use Clue\React\Quassel\Client;
use Clue\React\Quassel\Io\Protocol;
use Clue\React\Quassel\Models\Message;
require __DIR__ . '/../vendor/autoload.php';
$host = '127.0.0.1';
if (isset($argv[1])) { $host = $argv[1]; }
echo 'Server: ' . $host . PHP_EOL;
echo 'User name: ';
$user = trim(fgets(STDIN));
echo 'Password: ';
$pass = trim(fgets(STDIN));
echo 'Keyword: ';
$keyword = trim(fgets(STDIN));
if (strlen($keyword) < 3) {
die('Keyword MUST contain at least 3 characters to avoid excessive spam');
}
$factory = new Factory();
$uri = rawurlencode($user) . ':' . rawurlencode($pass) . '@' . $host;
$factory->createClient($uri)->then(function (Client $client) use ($keyword) {
var_dump('CONNECTED');
$client->on('data', function ($message) use ($client, $keyword) {
// session initialized
if (isset($message->MsgType) && $message->MsgType === 'SessionInit') {
var_dump('session initialized, now waiting for incoming messages');
return;
}
// chat message received
if (isset($message[0]) && $message[0] === Protocol::REQUEST_RPCCALL && $message[1] === '2displayMsg(Message)') {
$in = $message[2];
assert($in instanceof Message);
if (strpos($in->contents, $keyword) !== false) {
$client->writeBufferInput($in->bufferInfo, 'Hello from clue/quassel-react :-)');
echo date('Y-m-d H:i:s') . ' Replied to ' . $in->bufferInfo->name . '/' . explode('!', $in->sender)[0] . ': "' . $in->contents . '"' . PHP_EOL;
}
}
});
$client->on('error', 'printf');
$client->on('close', function () {
echo 'Connection closed' . PHP_EOL;
});
})->then(null, function ($e) {
echo $e;
});