1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot\Conversation; |
6
|
|
|
|
7
|
|
|
use FondBot\Contracts\Conversable; |
8
|
|
|
use FondBot\Events\MessageReceived; |
9
|
|
|
use FondBot\Conversation\Concerns\SendsMessages; |
10
|
|
|
use FondBot\Conversation\Concerns\InteractsWithContext; |
11
|
|
|
|
12
|
|
|
abstract class Interaction implements Conversable |
13
|
|
|
{ |
14
|
|
|
use InteractsWithContext; |
15
|
|
|
use SendsMessages; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Run interaction. |
19
|
|
|
* |
20
|
|
|
* @param MessageReceived $message |
21
|
|
|
*/ |
22
|
|
|
abstract public function run(MessageReceived $message): void; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Process received message. |
26
|
|
|
* |
27
|
|
|
* @param MessageReceived $reply |
28
|
|
|
*/ |
29
|
|
|
abstract public function process(MessageReceived $reply): void; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Jump to interaction. |
33
|
|
|
* |
34
|
|
|
* @throws \InvalidArgumentException |
35
|
|
|
*/ |
36
|
|
|
public static function jump(): void |
37
|
|
|
{ |
38
|
|
|
/** @var ConversationManager $conversation */ |
39
|
|
|
$conversation = resolve(ConversationManager::class); |
40
|
|
|
$conversation->converse(resolve(static::class)); |
|
|
|
|
41
|
|
|
$conversation->markAsTransitioned(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Restart current interaction. |
46
|
|
|
*/ |
47
|
|
|
protected function restart(): void |
48
|
|
|
{ |
49
|
|
|
/** @var ConversationManager $conversation */ |
50
|
|
|
$conversation = resolve(ConversationManager::class); |
51
|
|
|
$conversation->restartInteraction($this); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Handle interaction. |
56
|
|
|
* |
57
|
|
|
* @param MessageReceived $message |
58
|
|
|
*/ |
59
|
|
|
public function handle(MessageReceived $message): void |
60
|
|
|
{ |
61
|
2 |
|
$context = context(); |
62
|
|
|
|
63
|
2 |
|
if ($context->getInteraction() instanceof $this) { |
64
|
|
|
$this->process($message); |
65
|
2 |
|
} else { |
66
|
1 |
|
$context->setInteraction($this); |
67
|
|
|
$this->run($message); |
68
|
1 |
|
} |
69
|
1 |
|
} |
70
|
|
|
} |
71
|
|
|
|