-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleDriver.php
145 lines (117 loc) · 3.5 KB
/
ConsoleDriver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace BotMan\Bundle;
use BotMan\BotMan\Interfaces\DriverInterface;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Incoming\IncomingMessage;
use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;
use BotMan\BotMan\Messages\Outgoing\Question;
use BotMan\BotMan\Users\User;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Simple driver for Symfony Console, to test a bot locally
*/
class ConsoleDriver implements DriverInterface
{
const DRIVER_NAME = 'Symfony Console';
private $botName = 'Bot';
private $message;
/** @var OutputInterface */
private $output;
/** @var bool */
private $hasQuestion = false;
/** @var array */
private $lastQuestions;
public function setBotName($name)
{
$this->botName = $name;
return $this;
}
public function initialize($message, OutputInterface $output)
{
$this->message = $message;
$this->output = $output;
}
public function getName()
{
return self::DRIVER_NAME;
}
public function matchesRequest()
{
return false;
}
public function getConversationAnswer(IncomingMessage $message)
{
$index = (int) $message->getText() - 1;
if ($this->hasQuestion && isset($this->lastQuestions[$index])) {
$question = $this->lastQuestions[$index];
return Answer::create($question['name'])
->setInteractiveReply(true)
->setValue($question['value'])
->setMessage($message);
}
return Answer::create($this->message)->setMessage($message);
}
public function getMessages()
{
return [new IncomingMessage($this->message, 999, '#channel', $this->message)];
}
public function isBot()
{
return strpos($this->message, $this->botName . ': ') === 0;
}
public function types(IncomingMessage $matchingMessage)
{
$this->output->writeln($this->botName . ': ...');
}
public function getUser(IncomingMessage $matchingMessage)
{
return new User($matchingMessage->getSender());
}
public function isConfigured()
{
return false;
}
public function buildServicePayload($message, $matchingMessage, $additionalParameters = [])
{
$questionData = null;
if ($message instanceof OutgoingMessage) {
$text = $message->getText();
} elseif ($message instanceof Question) {
$text = $message->getText();
$questionData = $message->toArray();
} else {
$text = $message;
}
return compact('text', 'questionData');
}
public function sendPayload($payload)
{
$questionData = $payload['questionData'];
$this->output->writeln($this->botName . ': ' . $payload['text']);
if (!is_null($questionData)) {
foreach ($questionData['actions'] as $key => $action) {
$this->output->writeln(($key + 1) . ') ' . $action['text']);
}
$this->hasQuestion = true;
$this->lastQuestions = $questionData['actions'];
}
}
/**
* Does the driver match to an incoming messaging service event.
*
* @return bool|mixed
*/
public function hasMatchingEvent()
{
return false;
}
/**
* Tells if the stored conversation callbacks are serialized.
*
* @return bool
*/
public function serializesCallbacks()
{
return false;
}
}