| @@ -84,14 +84,23 @@ | ||
| 84 | 84 | (new VBOChatSearch) |
| 85 | 85 | ->withContext($context) |
| 86 | 86 | ->message($threshold, '>') |
| 87 | 87 | ); |
| 88 | + | |
| 89 | + // maintain the user logged in | |
| 90 | + $chat->getUser()->keepAlive($context); | |
| 91 | + | |
| 92 | + // obtain the public context metadata | |
| 93 | + $metadata = $context->getMetadata($public = true); | |
| 88 | 94 | } catch (Exception $error) { |
| 89 | 95 | VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage()); |
| 90 | 96 | } |
| 91 | 97 | |
| 92 | 98 | // output the result content |
| 93 | - VBOHttpDocument::getInstance($app)->json($messages); | |
| 99 | + VBOHttpDocument::getInstance($app)->json([ | |
| 100 | + 'messages' => $messages, | |
| 101 | + 'metadata' => $metadata, | |
| 102 | + ]); | |
| 94 | 103 | } |
| 95 | 104 | |
| 96 | 105 | /** |
| 97 | 106 | * Task used to scan the pagination of a specified chat. |
| @@ -318,8 +327,9 @@ | ||
| 318 | 327 | |
| 319 | 328 | // fetch request data |
| 320 | 329 | $start = $app->input->getUint('start', 0); |
| 321 | 330 | $limit = $app->input->getUint('limit', 20); |
| 331 | + $categories = $app->input->getString('categories', []); | |
| 322 | 332 | $options = $app->input->get('options', [], 'array'); |
| 323 | 333 | |
| 324 | 334 | try { |
| 325 | 335 | /** @var VBOChatMediator */ |
| @@ -327,8 +337,9 @@ | ||
| 327 | 337 | |
| 328 | 338 | // obtain all the chats |
| 329 | 339 | $messages = $chat->getMessages( |
| 330 | 340 | (new VBOChatSearch) |
| 341 | + ->forCategories($categories) | |
| 331 | 342 | ->start($start) |
| 332 | 343 | ->limit($limit) |
| 333 | 344 | ->aggregate() |
| 334 | 345 | ); |
| @@ -353,6 +364,146 @@ | ||
| 353 | 364 | } |
| 354 | 365 | |
| 355 | 366 | // output the result content |
| 356 | 367 | VBOHttpDocument::getInstance($app)->json($chats); |
| 368 | + } | |
| 369 | + | |
| 370 | + /** | |
| 371 | + * Updates the metadata for the specified context. | |
| 372 | + * | |
| 373 | + * @return void | |
| 374 | + * | |
| 375 | + * @since 1.8.8 | |
| 376 | + */ | |
| 377 | + public function update_metadata() | |
| 378 | + { | |
| 379 | + $app = JFactory::getApplication(); | |
| 380 | + | |
| 381 | + // fetch request data | |
| 382 | + $contextId = $app->input->getUint('id_context', 0); | |
| 383 | + $context = $app->input->get('context', ''); | |
| 384 | + $key = $app->input->getString('key'); | |
| 385 | + $val = $app->input->getString('val', null); | |
| 386 | + | |
| 387 | + try { | |
| 388 | + if (!JSession::checkToken()) { | |
| 389 | + throw new RuntimeException(JText::translate('JINVALID_TOKEN'), 403); | |
| 390 | + } | |
| 391 | + | |
| 392 | + /** @var VBOChatMediator */ | |
| 393 | + $chat = VBOFactory::getChatMediator(); | |
| 394 | + | |
| 395 | + /** @var VBOChatContext */ | |
| 396 | + $context = $chat->createContext($context, $contextId); | |
| 397 | + | |
| 398 | + // update the metadata | |
| 399 | + $context->setMetadata($key, $val); | |
| 400 | + } catch (Exception $error) { | |
| 401 | + VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage()); | |
| 402 | + } | |
| 403 | + | |
| 404 | + VBOHttpDocument::getInstance($app)->json((new VBOChatSessionModel)->getItem($sessionId)); | |
| 405 | + | |
| 406 | + $app->close(); | |
| 407 | + } | |
| 408 | + | |
| 409 | + /** | |
| 410 | + * AJAX endpoint to send a message template to a chat session. | |
| 411 | + * | |
| 412 | + * NOTE: VikChannelManager and WhatsApp channel are required. | |
| 413 | + * | |
| 414 | + * @return void | |
| 415 | + */ | |
| 416 | + public function send_template() | |
| 417 | + { | |
| 418 | + $app = JFactory::getApplication(); | |
| 419 | + | |
| 420 | + try { | |
| 421 | + if (!JSession::checkToken()) { | |
| 422 | + // missing CSRF-proof token | |
| 423 | + throw new Exception(JText::translate('JINVALID_TOKEN'), 403); | |
| 424 | + } | |
| 425 | + | |
| 426 | + // gather request values | |
| 427 | + $sessionId = $app->input->getUint('session_id', ''); | |
| 428 | + $configId = $app->input->getUInt('config_id', 0); | |
| 429 | + | |
| 430 | + // recover session details from ID | |
| 431 | + $session = (new VBOChatSessionModel)->getItem($sessionId); | |
| 432 | + | |
| 433 | + if (!$session) { | |
| 434 | + throw new DomainException("Session [$sessionId] not found.", 404); | |
| 435 | + } | |
| 436 | + | |
| 437 | + // get WhatsApp business account ID from session metadata | |
| 438 | + $accountId = $session->metadata['waba_id']; | |
| 439 | + | |
| 440 | + // account validation | |
| 441 | + if (empty($accountId)) { | |
| 442 | + throw new RuntimeException('Missing account or phone ID.', 400); | |
| 443 | + } | |
| 444 | + | |
| 445 | + // find account record | |
| 446 | + $accountRecord = VCMMessagingAccountsModel::getInstance()->getItem([ | |
| 447 | + 'idchannel' => VikChannelManagerConfig::WHATSAPP, | |
| 448 | + 'account_id' => $accountId, | |
| 449 | + ]); | |
| 450 | + | |
| 451 | + if (!$accountRecord) { | |
| 452 | + throw new RuntimeException('Could not find WhatsApp Business Account data.', 404); | |
| 453 | + } | |
| 454 | + | |
| 455 | + if (empty($accountRecord->settings['configurations'][$configId])) { | |
| 456 | + throw new RuntimeException('Could not find account configuration data.', 404); | |
| 457 | + } | |
| 458 | + | |
| 459 | + if (empty($session->phone)) { | |
| 460 | + throw new RuntimeException('Missing recipient phone number.', 400); | |
| 461 | + } | |
| 462 | + | |
| 463 | + // build template decorator | |
| 464 | + if (!empty($session->metadata['quote_id'])) { | |
| 465 | + // use the quote decorator | |
| 466 | + $tplDecorator = new VCMMessagingTemplateDecoratorQuote((int) $session->metadata['quote_id']); | |
| 467 | + } else { | |
| 468 | + // default to chat decorator | |
| 469 | + $tplDecorator = new VCMMessagingTemplateDecoratorChat($session); | |
| 470 | + } | |
| 471 | + | |
| 472 | + // send message template | |
| 473 | + $sendResult = (new VCMWhatsappModelService($accountRecord->account_id, $accountRecord->phone_id)) | |
| 474 | + ->sendTemplate( | |
| 475 | + $session->phone, | |
| 476 | + $accountRecord, | |
| 477 | + $configId, | |
| 478 | + [ | |
| 479 | + 'decorator' => $tplDecorator, | |
| 480 | + ] | |
| 481 | + ); | |
| 482 | + | |
| 483 | + /** @var VBOChatMediator */ | |
| 484 | + $chat = VBOFactory::getChatMediator(); | |
| 485 | + | |
| 486 | + /** @var VBOChatUser */ | |
| 487 | + $user = $chat->getUser(); | |
| 488 | + | |
| 489 | + /** @var VBOChatMessage */ | |
| 490 | + $message = $chat->createMessage([ | |
| 491 | + 'context' => 'session', | |
| 492 | + 'id_context' => $sessionId, | |
| 493 | + 'sender_name' => $user->getName(), | |
| 494 | + 'id_sender' => $user->getID(), | |
| 495 | + 'message' => $sendResult->text, | |
| 496 | + 'ref_id' => $sendResult->id, | |
| 497 | + ]); | |
| 498 | + | |
| 499 | + // collect template message without sending any notification | |
| 500 | + $chat->saveMessage($message); | |
| 501 | + | |
| 502 | + } catch (Exception $error) { | |
| 503 | + VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage() ?: 'Error'); | |
| 504 | + } | |
| 505 | + | |
| 506 | + // output requested message template details | |
| 507 | + VBOHttpDocument::getInstance($app)->json($sendResult); | |
| 357 | 508 | } |
| 358 | 509 | } |