Skip to content

Commit

Permalink
New events and code cleanup. Improved test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
RTippin committed Jul 5, 2021
1 parent 39664d6 commit d5ab76c
Show file tree
Hide file tree
Showing 8 changed files with 485 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@

### [v0.40 (X.X.XXXX)]

#### Added
- `BotActionHandledEvent` and `BotActionFailedEvent`.

#### Changed
- Add index to column `bot_actions.handler`
- Moved logic for executing action handlers to `ProcessMessageTrigger` action class.
- When a bot handler fails / throws exception, the exception will not be reported. The action and exception will be dispatched in the new `BotActionFailedEvent` that can be listened to by the end user.

---

Expand Down
47 changes: 39 additions & 8 deletions src/Actions/Bots/ProcessMessageTriggers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Collection;
use RTippin\Messenger\Actions\BaseMessengerAction;
use RTippin\Messenger\Events\BotActionFailedEvent;
use RTippin\Messenger\Events\BotActionHandledEvent;
use RTippin\Messenger\Exceptions\FeatureDisabledException;
use RTippin\Messenger\Messenger;
use RTippin\Messenger\MessengerBots;
Expand Down Expand Up @@ -72,7 +74,8 @@ public function __construct(Messenger $messenger,
}

/**
* Create a new thread bot!
* Process all matching actions that the message
* body matches through the action triggers.
*
* @param mixed ...$parameters
* @param Thread[0]
Expand Down Expand Up @@ -119,7 +122,8 @@ private function matchActionTriggers(BotAction $action): void
/**
* Check if we should execute the actions handler. When executing,
* set the proper data into the handler, and start the actions
* cooldown, if any.
* cooldown, if any. Fire events when the action is handled
* or failed.
*
* @param BotAction $action
* @param string $trigger
Expand All @@ -134,14 +138,12 @@ private function handleAction(BotAction $action, string $trigger): void
->initializeHandler($action->handler)
->setAction($action)
->setThread($this->getThread())
->setMessage(
$this->getMessage(),
$trigger,
$this->senderIp
)
->setMessage($this->getMessage(), $trigger, $this->senderIp)
->handle();

$this->fireHandledEvent($action, $trigger);
} catch (Throwable $e) {
report($e);
$this->fireFailedEvent($action, $e);
}

$this->botActionEnding($action);
Expand Down Expand Up @@ -226,4 +228,33 @@ private function startTriggeredBotCooldowns(): void
->unique('id')
->each(fn (Bot $bot) => $bot->startCooldown());
}

/**
* @param BotAction $action
* @param string $trigger
*/
private function fireHandledEvent(BotAction $action, string $trigger): void
{
if ($this->shouldFireEvents()) {
$this->dispatcher->dispatch(new BotActionHandledEvent(
$action,
$this->getMessage(true),
$trigger
));
}
}

/**
* @param BotAction $action
* @param Throwable $exception
*/
private function fireFailedEvent(BotAction $action, Throwable $exception): void
{
if ($this->shouldFireEvents()) {
$this->dispatcher->dispatch(new BotActionFailedEvent(
$action,
$exception
));
}
}
}
34 changes: 34 additions & 0 deletions src/Events/BotActionFailedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace RTippin\Messenger\Events;

use Illuminate\Queue\SerializesModels;
use RTippin\Messenger\Models\BotAction;
use Throwable;

class BotActionFailedEvent
{
use SerializesModels;

/**
* @var BotAction
*/
public BotAction $action;

/**
* @var Throwable
*/
public Throwable $exception;

/**
* Create a new event instance.
*
* @param BotAction $action
* @param Throwable $exception
*/
public function __construct(BotAction $action, Throwable $exception)
{
$this->action = $action;
$this->exception = $exception;
}
}
43 changes: 43 additions & 0 deletions src/Events/BotActionHandledEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace RTippin\Messenger\Events;

use Illuminate\Queue\SerializesModels;
use RTippin\Messenger\Models\BotAction;
use RTippin\Messenger\Models\Message;

class BotActionHandledEvent
{
use SerializesModels;

/**
* @var BotAction
*/
public BotAction $action;

/**
* @var Message
*/
public Message $message;

/**
* @var string
*/
public string $trigger;

/**
* Create a new event instance.
*
* @param BotAction $action
* @param Message $message
* @param string $trigger
*/
public function __construct(BotAction $action,
Message $message,
string $trigger)
{
$this->action = $action;
$this->message = $message;
$this->trigger = $trigger;
}
}
Loading

0 comments on commit d5ab76c

Please sign in to comment.