mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-13 20:12:48 +00:00
perf: 机器人群聊消息被@到时发送到webhook
This commit is contained in:
parent
29c9ba56ff
commit
efdc4c5229
@ -23,12 +23,14 @@ class BotReceiveMsgTask extends AbstractTask
|
|||||||
{
|
{
|
||||||
protected $userid;
|
protected $userid;
|
||||||
protected $msgId;
|
protected $msgId;
|
||||||
|
protected $mention;
|
||||||
|
|
||||||
public function __construct($userid, $msgId)
|
public function __construct($userid, $msgId, $mention)
|
||||||
{
|
{
|
||||||
parent::__construct(...func_get_args());
|
parent::__construct(...func_get_args());
|
||||||
$this->userid = $userid;
|
$this->userid = $userid;
|
||||||
$this->msgId = $msgId;
|
$this->msgId = $msgId;
|
||||||
|
$this->mention = $mention;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function start()
|
public function start()
|
||||||
@ -42,14 +44,6 @@ class BotReceiveMsgTask extends AbstractTask
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$msg->readSuccess($botUser->userid);
|
$msg->readSuccess($botUser->userid);
|
||||||
//
|
|
||||||
$dialog = WebSocketDialog::find($msg->dialog_id);
|
|
||||||
if (empty($dialog)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if ($dialog->type !== 'user') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$this->botManagerReceive($msg, $botUser);
|
$this->botManagerReceive($msg, $botUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,11 +64,43 @@ class BotReceiveMsgTask extends AbstractTask
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$original = $msg->msg['text'];
|
$original = $msg->msg['text'];
|
||||||
|
if ($this->mention) {
|
||||||
|
$original = preg_replace("/<span class=\"mention user\" data-id=\"(\d+)\">(.*?)<\/span>/", "", $original);
|
||||||
|
}
|
||||||
if (preg_match("/<span[^>]*?data-quick-key=([\"'])(.*?)\\1[^>]*?>(.*?)<\/span>/is", $original, $match)) {
|
if (preg_match("/<span[^>]*?data-quick-key=([\"'])(.*?)\\1[^>]*?>(.*?)<\/span>/is", $original, $match)) {
|
||||||
$command = $match[2];
|
$command = $match[2];
|
||||||
} else {
|
} else {
|
||||||
$command = trim(strip_tags($original));
|
$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') {
|
if ($botUser->email === 'check-in@bot.system') {
|
||||||
$text = UserBot::checkinBotQuickMsg($command, $msg->userid);
|
$text = UserBot::checkinBotQuickMsg($command, $msg->userid);
|
||||||
@ -337,24 +363,6 @@ class BotReceiveMsgTask extends AbstractTask
|
|||||||
$text = preg_replace("/^\x20+/", "", $text);
|
$text = preg_replace("/^\x20+/", "", $text);
|
||||||
$text = preg_replace("/\n\x20+/", "\n", $text);
|
$text = preg_replace("/\n\x20+/", "\n", $text);
|
||||||
WebSocketDialogMsg::sendMsg(null, $msg->dialog_id, 'text', ['text' => $text], $botUser->userid, false, false, true); // todo 未能在任务end事件来发送任务
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -144,7 +144,7 @@ class WebSocketDialogMsgTask extends AbstractTask
|
|||||||
// 机器人收到消处理
|
// 机器人收到消处理
|
||||||
$botUser = User::whereUserid($userid)->whereBot(1)->first();
|
$botUser = User::whereUserid($userid)->whereBot(1)->first();
|
||||||
if ($botUser) {
|
if ($botUser) {
|
||||||
$this->endArray[] = new BotReceiveMsgTask($botUser->userid, $msg->id);
|
$this->endArray[] = new BotReceiveMsgTask($botUser->userid, $msg->id, $mention);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -94,16 +94,19 @@
|
|||||||
--header 'token: <span style="color:#84c56a">{机器人Token}</span>' \
|
--header 'token: <span style="color:#84c56a">{机器人Token}</span>' \
|
||||||
--form 'dialog_id="<span style="color:#84c56a">{对话ID}</span>"' \
|
--form 'dialog_id="<span style="color:#84c56a">{对话ID}</span>"' \
|
||||||
--form 'text="<span style="color:#84c56a">{消息内容}</span>"'
|
--form 'text="<span style="color:#84c56a">{消息内容}</span>"'
|
||||||
--form 'text_type="<span style="color:#84c56a">[md|html]</span>"'
|
--form 'text_type="<span style="color:#84c56a">[html|md]</span>"'
|
||||||
--form 'silence="<span style="color:#84c56a">[yes|no]</span>"'
|
--form 'silence="<span style="color:#84c56a">[yes|no]</span>"'
|
||||||
|
--form 'reply_id="<span style="color:#84c56a">[回复指定消息ID]</span>"'
|
||||||
|
|
||||||
<b>Webhook说明:</b>
|
<b>Webhook说明:</b>
|
||||||
机器人收到个人对话消息后会将消息POST推送到Webhook地址,请求超时为10秒,请求参数如下:
|
机器人收到消息后会将消息POST推送到Webhook地址,请求超时为10秒,请求参数如下:
|
||||||
<span style="color:#84c56a">text</span>: 消息文本
|
<span style="color:#84c56a">text</span>: 消息文本
|
||||||
<span style="color:#84c56a">token</span>: 机器人Token
|
<span style="color:#84c56a">token</span>: 机器人Token
|
||||||
<span style="color:#84c56a">dialog_id</span>: 对话ID
|
<span style="color:#84c56a">dialog_id</span>: 对话ID
|
||||||
|
<span style="color:#84c56a">dialog_type</span>: 对话类型
|
||||||
<span style="color:#84c56a">msg_id</span>: 消息ID
|
<span style="color:#84c56a">msg_id</span>: 消息ID
|
||||||
<span style="color:#84c56a">msg_uid</span>: 消息发送人ID
|
<span style="color:#84c56a">msg_uid</span>: 消息发送人ID
|
||||||
|
<span style="color:#84c56a">mention</span>: 是否被@到
|
||||||
<span style="color:#84c56a">bot_uid</span>: 机器人ID
|
<span style="color:#84c56a">bot_uid</span>: 机器人ID
|
||||||
<span style="color:#84c56a">version</span>: 系统版本
|
<span style="color:#84c56a">version</span>: 系统版本
|
||||||
@elseif ($type === 'notice')
|
@elseif ($type === 'notice')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user