diff --git a/app/Tasks/BotReceiveMsgTask.php b/app/Tasks/BotReceiveMsgTask.php index 6feb2f6ac..fb0fa81c6 100644 --- a/app/Tasks/BotReceiveMsgTask.php +++ b/app/Tasks/BotReceiveMsgTask.php @@ -23,12 +23,14 @@ class BotReceiveMsgTask extends AbstractTask { protected $userid; protected $msgId; + protected $mention; - public function __construct($userid, $msgId) + public function __construct($userid, $msgId, $mention) { parent::__construct(...func_get_args()); $this->userid = $userid; $this->msgId = $msgId; + $this->mention = $mention; } public function start() @@ -42,14 +44,6 @@ class BotReceiveMsgTask extends AbstractTask return; } $msg->readSuccess($botUser->userid); - // - $dialog = WebSocketDialog::find($msg->dialog_id); - if (empty($dialog)) { - return; - } - if ($dialog->type !== 'user') { - return; - } $this->botManagerReceive($msg, $botUser); } @@ -70,11 +64,43 @@ class BotReceiveMsgTask extends AbstractTask return; } $original = $msg->msg['text']; + if ($this->mention) { + $original = preg_replace("/(.*?)<\/span>/", "", $original); + } if (preg_match("/]*?data-quick-key=([\"'])(.*?)\\1[^>]*?>(.*?)<\/span>/is", $original, $match)) { $command = $match[2]; } else { $command = trim(strip_tags($original)); } + // + $dialog = WebSocketDialog::find($msg->dialog_id); + if (empty($dialog)) { + return; + } + // 推送Webhook + if ($command + && !str_starts_with($command, '/') + && ($dialog->type === 'user' || $this->mention)) { + $userBot = UserBot::whereBotId($botUser->userid)->first(); + if ($userBot && preg_match("/^https*:\/\//", $userBot->webhook_url)) { + Ihttp::ihttp_post($userBot->webhook_url, [ + 'text' => $command, + 'token' => User::generateToken($botUser), + 'dialog_id' => $dialog->id, + 'dialog_type' => $dialog->type, + 'msg_id' => $msg->id, + 'msg_uid' => $msg->userid, + 'mention' => $this->mention ? 1 : 0, + 'bot_uid' => $botUser->userid, + 'version' => Base::getVersion(), + ], 10); + $userBot->webhook_num++; + $userBot->save(); + } + } + if ($dialog->type !== 'user') { + return; + } // 签到机器人 if ($botUser->email === 'check-in@bot.system') { $text = UserBot::checkinBotQuickMsg($command, $msg->userid); @@ -337,24 +363,6 @@ class BotReceiveMsgTask extends AbstractTask $text = preg_replace("/^\x20+/", "", $text); $text = preg_replace("/\n\x20+/", "\n", $text); WebSocketDialogMsg::sendMsg(null, $msg->dialog_id, 'text', ['text' => $text], $botUser->userid, false, false, true); // todo 未能在任务end事件来发送任务 - return; - } - // 推送Webhook - if ($command) { - $userBot = UserBot::whereBotId($botUser->userid)->first(); - if ($userBot && preg_match("/^https*:\/\//", $userBot->webhook_url)) { - Ihttp::ihttp_post($userBot->webhook_url, [ - 'text' => $command, - 'token' => User::generateToken($botUser), - 'dialog_id' => $msg->dialog_id, - 'msg_id' => $msg->id, - 'msg_uid' => $msg->userid, - 'bot_uid' => $botUser->userid, - 'version' => Base::getVersion(), - ], 10); - $userBot->webhook_num++; - $userBot->save(); - } } } diff --git a/app/Tasks/WebSocketDialogMsgTask.php b/app/Tasks/WebSocketDialogMsgTask.php index 4433496cd..88b7a6b85 100644 --- a/app/Tasks/WebSocketDialogMsgTask.php +++ b/app/Tasks/WebSocketDialogMsgTask.php @@ -144,7 +144,7 @@ class WebSocketDialogMsgTask extends AbstractTask // 机器人收到消处理 $botUser = User::whereUserid($userid)->whereBot(1)->first(); if ($botUser) { - $this->endArray[] = new BotReceiveMsgTask($botUser->userid, $msg->id); + $this->endArray[] = new BotReceiveMsgTask($botUser->userid, $msg->id, $mention); } } } diff --git a/resources/views/push/bot.blade.php b/resources/views/push/bot.blade.php index edfa3fd1a..72db8fa6c 100755 --- a/resources/views/push/bot.blade.php +++ b/resources/views/push/bot.blade.php @@ -94,16 +94,19 @@ --header 'token: {机器人Token}' \ --form 'dialog_id="{对话ID}"' \ --form 'text="{消息内容}"' - --form 'text_type="[md|html]"' + --form 'text_type="[html|md]"' --form 'silence="[yes|no]"' + --form 'reply_id="[回复指定消息ID]"' Webhook说明: - 机器人收到个人对话消息后会将消息POST推送到Webhook地址,请求超时为10秒,请求参数如下: + 机器人收到消息后会将消息POST推送到Webhook地址,请求超时为10秒,请求参数如下: text: 消息文本 token: 机器人Token dialog_id: 对话ID + dialog_type: 对话类型 msg_id: 消息ID msg_uid: 消息发送人ID + mention: 是否被@到 bot_uid: 机器人ID version: 系统版本 @elseif ($type === 'notice')