feat: 优化用户机器人 webhook 逻辑

This commit is contained in:
nightcp 2025-10-21 13:53:16 +08:00
parent 9b2731607b
commit 98e4668969
5 changed files with 21 additions and 12 deletions

View File

@ -1280,6 +1280,9 @@ class DialogController extends AbstractController
if ($model_name) {
$msgData['model_name'] = $model_name;
}
if (User::isBot($user->userid)) {
$msgData['force_webhook'] = true; // 强制使用webhook发送
}
$result = WebSocketDialogMsg::sendMsg($action, $dialog_id, 'text', $msgData, $user->userid, false, false, $silence, $key);
}
}

View File

@ -66,7 +66,7 @@ class BotReceiveMsgTask extends AbstractTask
}
// 判断消息是否存在
$msg = WebSocketDialogMsg::with(['user'])->find($this->msgId);
$msg = WebSocketDialogMsg::with(['user', 'webSocketDialog'])->find($this->msgId);
if (empty($msg)) {
return;
}
@ -75,7 +75,11 @@ class BotReceiveMsgTask extends AbstractTask
$msg->readSuccess($botUser->userid);
// 判断消息是否是机器人发送的则不处理,避免循环
if (!$msg->user || $msg->user->bot) {
if ((!$msg->user || $msg->user->bot)) {
$msgData = Base::json2array($msg->msg);
if (Base::val($msgData, 'force_webhook') && $msg->webSocketDialog) {
$this->handleWebhookRequest($msgData['text'], null, $msg, $msg->webSocketDialog, $botUser);
}
return;
}

View File

@ -138,11 +138,11 @@ class WebSocketDialogMsgTask extends AbstractTask
'dot' => $dot,
'updated' => $updated,
];
// 机器人收到消处理
$botUser = User::whereUserid($userid)->whereBot(1)->first();
if ($botUser) {
$this->endArray[] = new BotReceiveMsgTask($botUser->userid, $msg->id, $mentions, $this->client);
}
}
// 机器人收到消处理
$botUser = User::whereUserid($userid)->whereBot(1)->first();
if ($botUser) { // 避免机器人处理自己发送的消息
$this->endArray[] = new BotReceiveMsgTask($botUser->userid, $msg->id, $mentions, $this->client);
}
}
// 更新已发送数量

View File

@ -463,7 +463,7 @@
<Input v-model="modifyData.webhook_url" :maxlength="255" />
</FormItem>
<FormItem v-if="typeof modifyData.webhook_events !== 'undefined'" prop="webhook_events" :label="$L('Webhook事件')">
<CheckboxGroup v-model="modifyData.webhook_events">
<CheckboxGroup v-model="webhookEvents">
<Checkbox v-for="option in webhookEventOptions" :key="option.value" :label="option.value">
{{$L(option.label)}}
</Checkbox>
@ -781,6 +781,7 @@ export default {
{value: 'member_join', label: '成员加入'},
{value: 'member_leave', label: '成员退出'},
],
webhookEvents: [],
openId: 0,
errorId: 0,
@ -2762,8 +2763,9 @@ export default {
clear_day: 0,
webhook_url: '',
system_name: '',
webhook_events: this.prepareWebhookEvents([], true),
webhook_events: [],
})
this.webhookEvents = this.prepareWebhookEvents([], true)
this.modifyLoad++;
this.$store.dispatch("call", {
url: 'users/bot/info',
@ -2774,7 +2776,7 @@ export default {
this.modifyData.clear_day = data.clear_day
this.modifyData.webhook_url = data.webhook_url
this.modifyData.system_name = data.system_name
this.modifyData.webhook_events = this.prepareWebhookEvents(data.webhook_events, true)
this.webhookEvents = this.prepareWebhookEvents(data.webhook_events, true)
}).finally(() => {
this.modifyLoad--;
})
@ -2929,7 +2931,7 @@ export default {
name: this.modifyData.name,
clear_day: this.modifyData.clear_day,
webhook_url: this.modifyData.webhook_url,
webhook_events: this.normalizeWebhookEvents(this.modifyData.webhook_events, false),
webhook_events: this.normalizeWebhookEvents(this.webhookEvents),
dialog_id: this.modifyData.dialog_id
}).then(({msg}) => {
$A.messageSuccess(msg);

View File

@ -3399,7 +3399,7 @@ export default {
}
return item.dialog_user.userid === userid
});
if (dialog) {
if (dialog && dialog.bot !== 1) {
return dispatch("openDialog", dialog.id).then(resolve).catch(reject)
}
dispatch("call", {