-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGlipBotman.php
359 lines (312 loc) · 11.4 KB
/
GlipBotman.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
namespace GlipDriver;
use Mpociot\BotMan\User;
use Mpociot\BotMan\Answer;
use Mpociot\BotMan\Message;
use Mpociot\BotMan\Question;
use Mpociot\BotMan\Drivers\Driver;
use Illuminate\Support\Collection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ParameterBag;
use Mpociot\BotMan\Messages\Message as IncomingMessage;
use RingCentral\SDK\SDK;
class GlipBotman extends Driver
{
/** @var Collection */
protected $event;
/** @var config */
protected $config;
/** @var GlipClient */
protected $sdk;
protected $platform;
const DRIVER_NAME = 'GlipBotman';
/** @var Collection|ParameterBag */
protected $payload;
protected $endpoint = '/glip/posts';
/**s
* @param Request $request
*/
public function buildPayload(Request $request)
{
$this->payload = new ParameterBag((array) json_decode($request->getContent(), true));
print 'The payload during the glip buildpayload method is ' . PHP_EOL . print_r($this->payload);
$this->event = Collection::make($this->payload->get('event'));
}
/**
* Return the driver name.
*
* @return string
*/
public function getName()
{
return self::DRIVER_NAME;
}
/**
* @param Message $matchingMessage
* @return User
*/
public function getUser(Message $matchingMessage)
{
$parameters = [
'chat_id' => $matchingMessage->getChannel(),
'user_id' => $matchingMessage->getUser(),
];
$response = $this->$this->getPlatform()->get('/glip/persons' + $matchingMessage->getUser());
$responseData = json_decode($response->getContent(), true);
$userData = Collection::make($responseData['result']['user']);
return new User($userData->get('id'), $userData->get('firstName'), $userData->get('lastName'), $userData->get('avatar'));
}
/**
* Determine if the request is for this driver.
*
* @return bool
*/
public function matchesRequest()
{
return (! is_null($this->payload->get('body'))) && ! is_null($this->payload->get('event'));
}
/**
* @param Message $message
* @return Answer
*/
public function getConversationAnswer(Message $message)
{
print 'Inside the get Conv Answeer' . PHP_EOL . print_r($message);
return Answer::create($message->getMessage())->setMessage($message);
}
/**
* Retrieve the chat message.
*
* @return array
*/
public function getMessages()
{
if ($this->payload->get('body') !== null) {
$callback = Collection::make($this->payload->get('body'));
return [new Message($callback->get('text'), $callback->get('creatorId'), $callback->get('groupId'), $this->payload->get('body'))];
}
}
/**
* @return bool
*/
public function isBot()
{
return false;
}
/**
* @param Message $matchingMessage
* @return void
*/
public function types(Message $matchingMessage)
{
$parameters = [
'chat_id' => $matchingMessage->getChannel(),
'action' => 'typing',
];
$this->http->post('/glip/posts', $parameters);
}
/**
* Convert a Question object into a valid
* quick reply response object.
*
* @param Question $question
* @return array
*/
private function convertQuestion(Question $question)
{
$replies = Collection::make($question->getButtons())->map(function ($button) {
return [
[
'text' => (string) $button['text'],
'callback_data' => (string) $button['value'],
],
];
});
return $replies->toArray();
}
/**
* @return \RingCentral\SDK\Platform\Platform
*/
public function getPlatform()
{
$rcsdk = new SDK($this->config->get('GLIP_APPKEY'), $this->config->get('GLIP_APPSECRET'), $this->config->get('GLIP_SERVER'), 'Sample-Bot', '1.0.0');
$platform = $rcsdk->platform();
$cacheDir = __DIR__ . DIRECTORY_SEPARATOR . '_cache';
$file = $cacheDir . DIRECTORY_SEPARATOR . 'platform.json';
if (!file_exists($cacheDir)) {
mkdir($cacheDir);
print 'The config is :' . PHP_EOL . print_r($this->config);
$platform->login($this->config->get('GLIP_USERNAME'), $this->config->get('GLIP_EXTENSION'), $this->config->get('GLIP_PASSWORD'));
file_put_contents($file, json_encode($platform->auth()->data(), JSON_PRETTY_PRINT));
}
$cachedAuth = array();
if (file_exists($file)) {
$cachedAuth = json_decode(file_get_contents($file), true);
$platform->auth()->setData($cachedAuth);
}
try {
if($platform->loggedIn()) {
return $platform;
}
else {
print 'The Username is : ' . $this->config->get('GLIP_USERNAME');
$refresh = $platform->login($this->config->get('GLIP_USERNAME'), $this->config->get('GLIP_EXTENSION'), $this->config->get('GLIP_PASSWORD'));
file_put_contents($file, json_encode($refresh->jsonArray(), JSON_PRETTY_PRINT));
return $platform;
}
}
catch (Exception $e) {
$platform->login($this->config->get('GLIP_USERNAME'), $this->config->get('GLIP_EXTENSION'), $this->config->get('GLIP_PASSWORD'));
file_put_contents($file, json_encode($platform->auth()->data(), JSON_PRETTY_PRINT));
return $platform;
}
}
/**
* Removes the inline keyboard from an interactive
* message.
* @param int $chatId
* @param int $messageId
* @return Response
*/
private function removeInlineKeyboard($chatId, $messageId)
{
$parameters = [
'chat_id' => $chatId,
'message_id' => $messageId,
'inline_keyboard' => [],
];
$this->getPlatform()->post('/glip/posts', $parameters);
}
/**
* @param string|Question|IncomingMessage $message
* @param Message $matchingMessage
* @param array $additionalParameters
* @return Response
*/
public function reply($message, $matchingMessage, $additionalParameters = [])
{
print 'Inside Reply method' . PHP_EOL;
$endpoint = 'sendMessage';
$parameters = array_merge([
'groupId' => $matchingMessage->getChannel(),
], $additionalParameters);
/*
* If we send a Question with buttons, ignore
* the text and append the question.
*/
if ($message instanceof Question) {
$parameters['text'] = $message->getText();
$parameters['reply_markup'] = json_encode([
'inline_keyboard' => $this->convertQuestion($message),
], true);
} elseif ($message instanceof IncomingMessage) {
if (! is_null($message->getImage())) {
if (strtolower(pathinfo($message->getImage(), PATHINFO_EXTENSION)) === 'gif') {
$endpoint = 'sendDocument';
$parameters['document'] = $message->getImage();
} else {
$endpoint = 'sendPhoto';
$parameters['photo'] = $message->getImage();
}
$parameters['caption'] = $message->getMessage();
} elseif (! is_null($message->getVideo())) {
$endpoint = 'sendVideo';
$parameters['video'] = $message->getVideo();
$parameters['caption'] = $message->getMessage();
} else {
$parameters['text'] = $message->getMessage();
}
} else {
$parameters['text'] = $message;
}
$this->getPlatform()->post('/glip/posts', $parameters);
}
/**
* @param string|Question|IncomingMessage $message
* @param Message $matchingMessage
* @param array $additionalParameters
* @return Response
*/
public function buildServicePayload($message, $matchingMessage, $additionalParameters = [])
{
$recipient = $matchingMessage->getRecipient() === '' ? $matchingMessage->getSender() : $matchingMessage->getRecipient();
$parameters = array_merge_recursive([
'groupId' => $recipient,
], $additionalParameters);
print 'The mesaage is : ' . PHP_EOL . print_r($message);
/*
* If we send a Question with buttons, ignore
* the text and append the question.
*/
if ($message instanceof Question) {
$parameters['text'] = $message->getText();
$parameters['reply_markup'] = json_encode([
'inline_keyboard' => $this->convertQuestion($message),
], true);
} elseif ($message instanceof IncomingMessage) {
if (! is_null($message->getAttachment())) {
$attachment = $message->getAttachment();
$parameters['caption'] = $message->getText();
if ($attachment instanceof Image) {
if (strtolower(pathinfo($attachment->getUrl(), PATHINFO_EXTENSION)) === 'gif') {
$this->endpoint = 'sendDocument';
$parameters['document'] = $attachment->getUrl();
} else {
$this->endpoint = 'sendPhoto';
$parameters['photo'] = $attachment->getUrl();
}
} elseif ($attachment instanceof Video) {
$this->endpoint = 'sendVideo';
$parameters['video'] = $attachment->getUrl();
} elseif ($attachment instanceof Audio) {
$this->endpoint = 'sendAudio';
$parameters['audio'] = $attachment->getUrl();
} elseif ($attachment instanceof File) {
$this->endpoint = 'sendDocument';
$parameters['document'] = $attachment->getUrl();
} elseif ($attachment instanceof Location) {
$this->endpoint = 'sendLocation';
$parameters['latitude'] = $attachment->getLatitude();
$parameters['longitude'] = $attachment->getLongitude();
}
} else {
$parameters['text'] = $message->getText();
}
} else {
$parameters['text'] = $message;
}
return $parameters;
}
/**
* @param mixed $payload
* @return Response
*/
public function sendPayload($payload)
{
print 'Inside GlipTest sendpayload' . PHP_EOL . print_r($payload);
return $this->getPlatform()->post($this->endpoint, $payload);
}
/**
* @return bool
*/
public function isConfigured()
{
return ! is_null($this->getPlatform()->loggedIn());
}
/**
* Low-level method to perform driver specific API requests.
*
* @param string $endpoint
* @param array $parameters
* @param Message $matchingMessage
* @return Response
*/
public function sendRequest($endpoint, array $parameters, Message $matchingMessage)
{
$parameters = array_replace_recursive([
'chat_id' => $matchingMessage->getRecipient(),
], $parameters);
return $this->getPlatform()->post($endpoint, $parameters);
}
}