1
0

BxDolAiTriggerMessage.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolAiTriggerMessage extends BxDolAiTrigger
  10. {
  11. protected $_sType = 'message';
  12. protected function usesSenderChatHistory()
  13. {
  14. return true;
  15. }
  16. protected function appliesChatLimits()
  17. {
  18. return true;
  19. }
  20. public function response($oAlert)
  21. {
  22. BxDolAiTrigger::getInstance($this->getType())->handle($oAlert);
  23. }
  24. public function handle($mixed = null)
  25. {
  26. $oAlert = $mixed;
  27. if (!$oAlert || 'bx_messenger' != $oAlert->sUnit || 'got_jot' != $oAlert->sAction)
  28. return false;
  29. return $this->processMessage(
  30. $oAlert->iSender,
  31. $oAlert->aExtras['recipient_id'],
  32. $oAlert->iObject,
  33. $oAlert->aExtras['subobject_id'],
  34. $oAlert->aExtras['subobject_info']
  35. );
  36. }
  37. public function getAgentsByProfileId($iProfileId)
  38. {
  39. $oDb = new BxDolAiQuery();
  40. return $oDb->getAgentsByProfileId($iProfileId);
  41. }
  42. public function processCallQueue($bFinishRequest = true, $bExit = true)
  43. {
  44. if ($bFinishRequest) {
  45. if (function_exists('fastcgi_finish_request')) {
  46. fastcgi_finish_request();
  47. } else {
  48. @ob_end_flush();
  49. @flush();
  50. }
  51. }
  52. if (!empty($GLOBALS['glAgentsCallQueue'])) {
  53. foreach ($GLOBALS['glAgentsCallQueue'] as $r) {
  54. $sType = $r['type'] ?? $this->getType();
  55. $sMessage = BxDolAiTrigger::getInstance($sType)->call($r['agent'], $r['params']);
  56. if (null == $sMessage) {
  57. // TODO: maybe reply with some empty message
  58. continue;
  59. }
  60. $oParsedown = new Parsedown();
  61. $oParsedown->setSafeMode(true);
  62. $sMessageHtml = $oParsedown->text($sMessage);
  63. $this->sendMessengerMessage($r['agent']['profile_id'], $r['params']['sender_profile_id'], str_replace('\n', '', $sMessageHtml));
  64. }
  65. }
  66. if ($bExit)
  67. exit(0);
  68. }
  69. public function sendMessengerMessage($iSender, $iRecipient, $sMsg)
  70. {
  71. $oMessengerModule = BxDolModule::getInstance('bx_messenger');
  72. $aAutoReplyData = [
  73. 'message' => $sMsg,
  74. 'participants' => [$iSender, $iRecipient],
  75. ];
  76. $iSaveProfileId = $oMessengerModule->setProfileId($iSender);
  77. $a = $oMessengerModule->sendMessage($aAutoReplyData, $iRecipient, $iSender);
  78. $oMessengerModule->setProfileId($iSaveProfileId);
  79. return $a;
  80. }
  81. protected function processMessage($iSender, $iRecipient, $iLotId, $iJotId, $aJotInfo)
  82. {
  83. if ($iSender == $iRecipient)
  84. return false;
  85. $oAi = $this->getAi();
  86. if (!$oAi)
  87. return false;
  88. $aAgents = $this->getAgentsByProfileId($iRecipient);
  89. if (!$aAgents)
  90. return false;
  91. $GLOBALS['glAgentsCallQueue'] = [];
  92. foreach ($aAgents as $a) {
  93. if (!$oAi->canInteract($a, $iSender))
  94. continue;
  95. if (!$a['message_profile_id'] || $iSender == $a['message_profile_id']) {
  96. $aParams = [
  97. 'trigger' => $this->getType(),
  98. 'sender_profile_id' => $iSender,
  99. 'recipient_profile_id' => $iRecipient,
  100. 'message_lot_id' => $iLotId,
  101. 'message_id' => $iJotId,
  102. 'message_text' => $aJotInfo['message'],
  103. 'message_info' => $aJotInfo,
  104. ];
  105. if ($a['async']) {
  106. $this->enqueue($a, $aParams);
  107. } else {
  108. $GLOBALS['glAgentsCallQueue'][] = [
  109. 'type' => $this->getType(),
  110. 'agent' => $a,
  111. 'params' => $aParams,
  112. ];
  113. }
  114. }
  115. }
  116. if (!empty($GLOBALS['glAgentsCallQueue'])) {
  117. ignore_user_abort(true);
  118. set_time_limit(0);
  119. // TODO: need a way to force messenger to show new message from the server side
  120. // register_shutdown_function(function () {
  121. // bx_ai_process_agents_call_queue();
  122. // });
  123. $this->processCallQueue(false, false); // TODO: for a while do synchronically
  124. }
  125. return true;
  126. }
  127. }
  128. /** @} */