-
Notifications
You must be signed in to change notification settings - Fork 127
/
Bot.php
203 lines (189 loc) · 5.08 KB
/
Bot.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace Viber;
use Closure;
use Viber\Client;
use Viber\Bot\Manager;
use Viber\Api\Event;
use Viber\Api\Signature;
use Viber\Api\Event\Factory;
use Viber\Api\Entity;
/**
* Build bot with viber client
*
* @author Novikov Bogdan <hcbogdan@gmail.com>
*/
class Bot
{
/**
* Api client
*
* @var \Viber\Client
*/
protected $client;
/**
* Event managers collection
*
* @var array
*/
protected $managers = [];
/**
* Init client
*
* Required options (one of two):
* token string
* client \Viber\Client
*
* @throws \RuntimeException
* @param array $options
*/
public function __construct(array $options)
{
if (isset($options['token'])) {
$this->client = new Client($options);
} elseif (isset($options['client']) && $options['client'] instanceof Client) {
$this->client = $options['client'];
} else {
throw new \RuntimeException('Specify "client" or "token" parameter');
}
}
/**
* Get current bot client
*
* @return |Viber\Client
*/
public function getClient()
{
return $this->client;
}
/**
* Register event handler callback
*
* @param \Closure $checker checker function
* @param \Closure $handler handler function
*
* @return \Viber\Bot
*/
public function on(\Closure $checker, \Closure $handler)
{
$this->managers[] = new Manager($checker, $handler);
return $this;
}
/**
* Register text message handler by PCRE
*
* @param string $regexp valid regular expression
* @param Closure $handler event handler
* @return \Viber\Bot
*/
public function onText($regexp, \Closure $handler)
{
$this->managers[] = new Manager(function (Event $event) use ($regexp) {
return (
$event instanceof \Viber\Api\Event\Message
&& preg_match($regexp, $event->getMessage()->getText())
);
}, $handler);
return $this;
}
/**
* Register subscrive event handler
*
* @param Closure $handler valid function
* @return \Viber\Bot
*/
public function onSubscribe(\Closure $handler)
{
$this->managers[] = new Manager(function (Event $event) {
return ($event instanceof \Viber\Api\Event\Subscribed);
}, $handler);
return $this;
}
/**
* Register conversation event handler
*
* @param Closure $handler valid function
* @return \Viber\Bot
*/
public function onConversation(\Closure $handler)
{
$this->managers[] = new Manager(function (Event $event) {
return ($event instanceof \Viber\Api\Event\Conversation);
}, $handler);
return $this;
}
/**
* Get signature header
*
* @throws \RuntimeException
* @return string
*/
public function getSignHeaderValue()
{
$headerName = 'HTTP_X_VIBER_CONTENT_SIGNATURE';
if (!isset($_SERVER[$headerName])) {
throw new \RuntimeException($headerName.' header not found', 1);
}
return $_SERVER[$headerName];
}
/**
* Get bot input stream
*
* @return string
*/
public function getInputBody()
{
return file_get_contents('php://input');
}
/**
* Response with entity
*
* @param Entity $entity
* @return void
*/
public function outputEntity(Entity $entity)
{
header('Content-Type: application/json');
echo json_encode($entity->toApiArray());
}
/**
* Start bot process
*
* @throws \RuntimeException
* @param \Viber\Api\Event $event start bot with some event
* @return \Viber\Bot
*/
public function run($event = null)
{
if (is_null($event)) {
// check body
$eventBody = $this->getInputBody();
if (!Signature::isValid(
$this->getSignHeaderValue(),
$eventBody,
$this->getClient()->getToken()
)) {
throw new \RuntimeException('Invalid signature header', 2);
}
// check json
$eventBody = json_decode($eventBody, true);
if (json_last_error() || empty($eventBody) || !is_array($eventBody)) {
throw new \RuntimeException('Invalid json request', 3);
}
// make event from json
$event = Factory::makeFromApi($eventBody);
} elseif (!$event instanceof Event) {
throw new \RuntimeException('Event must be instance of \Viber\Api\Event', 4);
}
// main bot loop
foreach ($this->managers as $manager) {
if ($manager->isMatch($event)) {
$returnValue = $manager->runHandler($event);
if ($returnValue && $returnValue instanceof Entity) { // reply with entity
$this->outputEntity($returnValue);
}
break;
}
}
return $this;
}
}