This repository has been archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from DZunke/dev
Release Merge for v1.2.0
- Loading branch information
Showing
18 changed files
with
617 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
.idea/ | ||
vendor/ | ||
composer.lock | ||
log/ | ||
composer.phar | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace DZunke\SlackBundle\Command; | ||
|
||
use DZunke\SlackBundle\Event; | ||
use DZunke\SlackBundle\Events; | ||
use DZunke\SlackBundle\Slack\Client; | ||
use DZunke\SlackBundle\Slack\Entity\Message; | ||
use Monolog\Handler\StreamHandler; | ||
use Monolog\Logger; | ||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class BotMessagingCommand extends ContainerAwareCommand | ||
{ | ||
|
||
const PROCESS_ITERATION_SLEEP = 1; | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->setName('dzunke:slack:run-bot') | ||
->setDescription('Running the Bot-User to a Channel') | ||
->addArgument( | ||
'channel', | ||
InputArgument::REQUIRED, | ||
'Channel wo Watch over' | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$logger = new Logger(new StreamHandler('php://output')); | ||
|
||
$channel = $this->getContainer()->get('dz.slack.channels')->getId($input->getArgument('channel')); | ||
if (empty($channel)) { | ||
$logger->error('channel "' . $channel . '" does not exists'); | ||
return; | ||
} | ||
|
||
$lastTimestamp = time(); | ||
while (true) { | ||
|
||
try { | ||
$latestMessages = $this->getContainer()->get('dz.slack.channels')->history($channel, $lastTimestamp); | ||
|
||
foreach ($latestMessages as $message) { | ||
if ($message->isBot() === true) { | ||
continue; | ||
} | ||
|
||
$logger->debug('Handling Message of Type : "' . $message->getType() . '"'); | ||
|
||
$event = Event::MESSAGE; | ||
switch ($message->getType()) { | ||
case Message::TYPE_MESSAGE : | ||
$event = Event::MESSAGE; | ||
break; | ||
case Message::TYPE_CHANNEL_JOIN: | ||
$event = Event::JOIN; | ||
break; | ||
case Message::TYPE_CHANNEL_LEAVE: | ||
$event = Event::LEAVE; | ||
break; | ||
} | ||
|
||
$logger->debug('Dispatching "' . $event . '"'); | ||
|
||
$this->getContainer()->get('event_dispatcher')->dispatch( | ||
$event, | ||
new Events\MessageEvent($channel, $message) | ||
); | ||
|
||
$lastTimestamp = $message->getId(); | ||
} | ||
|
||
$logger->debug('Handled ' . count($latestMessages) . ' Messages'); | ||
|
||
} catch (\Exception $e) { | ||
$logger->error($e->getMessage()); | ||
$logger->error($e->getTraceAsString()); | ||
} | ||
|
||
sleep(self::PROCESS_ITERATION_SLEEP); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace DZunke\SlackBundle; | ||
|
||
/** | ||
* there are some events that will be triggerd in Bot-Mode to | ||
* react on specific actions in the watched channel | ||
*/ | ||
class Event | ||
{ | ||
|
||
/** | ||
* event for standard messages | ||
*/ | ||
const MESSAGE = 'slack.message'; | ||
|
||
/** | ||
* event for messages that contain a joined user | ||
*/ | ||
const JOIN = 'slack.channel.join'; | ||
|
||
/** | ||
* event for messages that contain a leave | ||
*/ | ||
const LEAVE = 'slack.channel.leave'; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace DZunke\SlackBundle\Events; | ||
|
||
use DZunke\SlackBundle\Slack\Entity\Message; | ||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
class MessageEvent extends Event | ||
{ | ||
|
||
/** | ||
* @var Message | ||
*/ | ||
private $message; | ||
|
||
/** | ||
* @param object $message | ||
*/ | ||
public function __construct($message) | ||
{ | ||
$this->message = $message; | ||
} | ||
|
||
/** | ||
* @return Message | ||
*/ | ||
public function getMessage() | ||
{ | ||
return $this->message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.