Skip to content
This repository has been archived by the owner on Jun 4, 2018. It is now read-only.

Commit

Permalink
Add realtime bot example
Browse files Browse the repository at this point in the history
  • Loading branch information
mcrumm committed Dec 16, 2015
1 parent e0bfef1 commit 99a673e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
12 changes: 12 additions & 0 deletions RoboFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
38 changes: 38 additions & 0 deletions bin/examplebot
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env php
<?php
/**
* Bot Example
*/

require __DIR__ . '/../vendor/autoload.php';

// Ensure API Key has been provided
if (! isset ($argv[1])) {
die('Usage: ' . $argv[0] . ' <token>' . 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();

0 comments on commit 99a673e

Please sign in to comment.