BxDolAiAlertResponse.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup BaseGeneral Base classes for modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxDolAiAlertResponse extends BxDolAlertsResponse
  12. {
  13. public function response($oAlert)
  14. {
  15. if ('bx_messenger' == $oAlert->sUnit && 'got_jot' == $oAlert->sAction) {
  16. $iProfileIdSender = $oAlert->iSender;
  17. $this->processMesssage ($oAlert->iSender, $oAlert->aExtras['recipient_id'], $oAlert->iObject, $oAlert->aExtras['subobject_id'], $oAlert->aExtras['subobject_info']);
  18. }
  19. }
  20. protected function processMesssage ($iSender, $iRecipient, $iLotId, $iJotId, $aJotInfo)
  21. {
  22. if ($iSender == $iRecipient)
  23. return;
  24. // call agents
  25. $oAi = BxDolAi::getInstance();
  26. if($aAgents = $oAi->getAgentsByProfileId($iRecipient)) {
  27. $GLOBALS['glAgentsCallQueue'] = [];
  28. foreach($aAgents as $a) {
  29. if (!$oAi->canInteract($a, $iSender))
  30. continue;
  31. if (!$a['message_profile_id'] || $iSender == $a['message_profile_id']) {
  32. $aParams = [
  33. 'trigger' => 'message',
  34. 'sender_profile_id' => $iSender,
  35. 'recipient_profile_id' => $iRecipient,
  36. 'message_lot_id' => $iLotId,
  37. 'message_id' => $iJotId,
  38. 'message_text' => $aJotInfo['message'],
  39. 'message_info' => $aJotInfo,
  40. ];
  41. if ($a['async']) {
  42. BxDolBackgroundJobs::getInstance()->add(bin2hex(random_bytes(16)), [
  43. 'system', 'call_agent',
  44. ['message', $a, $aParams],
  45. 'TemplServices'
  46. ]);
  47. }
  48. else {
  49. $GLOBALS['glAgentsCallQueue'][] = [
  50. 'type' => 'message',
  51. 'agent' => $a,
  52. 'params' => $aParams,
  53. ];
  54. }
  55. }
  56. }
  57. if (!empty($GLOBALS['glAgentsCallQueue'])) {
  58. ignore_user_abort(true);
  59. set_time_limit(0);
  60. // TODO: need a way to force messenger to show new message from the server side
  61. // register_shutdown_function(function () {
  62. // bx_ai_process_agents_call_queue();
  63. // });
  64. bx_ai_process_agents_call_queue(false, false); // TODO: for a while do synchronically
  65. }
  66. }
  67. }
  68. }
  69. /** @} */