From 99a673eaf852594ba3dcc24b70e723026ec9ad0c Mon Sep 17 00:00:00 2001 From: Michael Crumm Date: Wed, 16 Dec 2015 01:12:33 -0800 Subject: [PATCH] Add realtime bot example --- RoboFile.php | 12 ++++++++++++ bin/examplebot | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100755 bin/examplebot diff --git a/RoboFile.php b/RoboFile.php index 8b54370..f80a405 100644 --- a/RoboFile.php +++ b/RoboFile.php @@ -38,4 +38,16 @@ public function docsPublish($message = 'Regenerate docs') ->checkout('master') ->run(); } + + /** + * Run the example bot. + * + * @param string $token Slack RealTime Messaging Token + */ + public function exampleBot($token) + { + $this->taskExec('bin/examplebot') + ->arg($token) + ->run(); + } } diff --git a/bin/examplebot b/bin/examplebot new file mode 100755 index 0000000..b9e5f72 --- /dev/null +++ b/bin/examplebot @@ -0,0 +1,38 @@ +#!/usr/bin/env php +' . PHP_EOL); +} + +use Slack\RealTimeClient as Client; + +// Event Loop +$loop = \React\EventLoop\Factory::create(); + +// Console Output +$out = new \React\Stream\Stream(STDOUT, $loop); + +$client = new Client($loop); +$client->setToken($argv[1]); + +// disconnect after first message +$client->on('message', function ($message) use ($client, $out) { + $out->write('Someone typed a message:'.$message['text'].PHP_EOL); + $client->disconnect(); + $out->end('Goodbye.'.PHP_EOL); +}); + +$client->connect()->then(function () use ($out) { + $out->write('Connected! Waiting for a message...'.PHP_EOL); +}, function (\Exception $ex) use ($out) { + $out->end('Error: '.$ex->getMessage().PHP_EOL); +}); + +$loop->run();