mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-11 10:33:54 +00:00
feat: 调整机器人webhook事件
- 可取消接收消息事件 - 打开机器人会话窗口时推送webhook消息,相同机器人消息缓存1分钟
This commit is contained in:
parent
ab4640382d
commit
4933930afd
@ -445,28 +445,6 @@ class DialogController extends AbstractController
|
||||
}
|
||||
$data = WebSocketDialog::synthesizeData($dialog->id, $user->userid);
|
||||
|
||||
if ($userid > 0) {
|
||||
$botTarget = User::whereUserid($userid)->whereBot(1)->first();
|
||||
if ($botTarget) {
|
||||
$userBot = UserBot::whereBotId($botTarget->userid)->first();
|
||||
if ($userBot) {
|
||||
$userBot->dispatchWebhook(UserBot::WEBHOOK_EVENT_DIALOG_OPEN, [
|
||||
'dialog_id' => $dialog->id,
|
||||
'dialog_type' => $dialog->type,
|
||||
'session_id' => $dialog->session_id,
|
||||
'dialog_name' => $dialog->getGroupName(),
|
||||
'user' => [
|
||||
'userid' => $user->userid,
|
||||
'email' => $user->email,
|
||||
'nickname' => $user->nickname,
|
||||
],
|
||||
], 10, [
|
||||
'dialog' => $dialog->id,
|
||||
'operator' => $user->userid,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Base::retSuccess('success', $data);
|
||||
}
|
||||
|
||||
@ -3710,4 +3688,61 @@ class DialogController extends AbstractController
|
||||
//
|
||||
return Base::retSuccess('重命名成功', $session);
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {get} api/dialog/open/webhook 打开机器人会话推送 webhook
|
||||
*
|
||||
* @apiDescription 需要token身份
|
||||
* @apiVersion 1.0.0
|
||||
* @apiGroup dialog
|
||||
* @apiName open__webhook
|
||||
*
|
||||
* @apiParam {Number} dialog_id 对话ID
|
||||
*
|
||||
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
|
||||
* @apiSuccess {String} msg 返回信息(错误描述)
|
||||
* @apiSuccess {Object} data 返回数据
|
||||
*/
|
||||
public function open__webhook()
|
||||
{
|
||||
$user = User::auth();
|
||||
//
|
||||
$dialog_id = intval(Request::input('dialog_id'));
|
||||
if (empty($dialog_id)) {
|
||||
return Base::retError('错误的会话');
|
||||
}
|
||||
//
|
||||
$dialog = WebSocketDialog::checkDialog($dialog_id);
|
||||
if (empty($dialog)) {
|
||||
return Base::retError('打开会话失败');
|
||||
}
|
||||
$data = WebSocketDialog::synthesizeData($dialog->id, $user->userid);
|
||||
if ($data['bot'] == 1) {
|
||||
$botTarget = User::whereUserid($data['dialog_user']['userid'])->whereBot(1)->first();
|
||||
if ($botTarget) {
|
||||
$userBot = UserBot::whereBotId($botTarget->userid)->first();
|
||||
if ($userBot) {
|
||||
// 每个机器人1分钟只触发一次 webhook
|
||||
Cache::remember('webhook_dialog_open_' . $botTarget->userid, 60, function () use ($userBot, $dialog, $user) {
|
||||
$userBot->dispatchWebhook(UserBot::WEBHOOK_EVENT_DIALOG_OPEN, [
|
||||
'dialog_id' => $dialog->id,
|
||||
'dialog_type' => $dialog->type,
|
||||
'session_id' => $dialog->session_id,
|
||||
'dialog_name' => $dialog->getGroupName(),
|
||||
'user' => [
|
||||
'userid' => $user->userid,
|
||||
'email' => $user->email,
|
||||
'nickname' => $user->nickname,
|
||||
],
|
||||
], 10, [
|
||||
'dialog' => $dialog->id,
|
||||
'operator' => $user->userid,
|
||||
]);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return Base::retSuccess('success');
|
||||
}
|
||||
}
|
||||
|
||||
@ -496,13 +496,13 @@ export default {
|
||||
if (result.length) {
|
||||
return Array.from(new Set(result));
|
||||
}
|
||||
return useFallback ? ['message'] : [];
|
||||
return [];
|
||||
},
|
||||
enhanceMybotItem(item = {}) {
|
||||
const data = $A.cloneJSON(item || {});
|
||||
let events = data.webhook_events;
|
||||
if (typeof events === 'undefined' || events === null) {
|
||||
events = ['message'];
|
||||
events = [];
|
||||
}
|
||||
events = this.normalizeWebhookEvents(events, false);
|
||||
if (!events.length) {
|
||||
|
||||
@ -1241,6 +1241,8 @@ export default {
|
||||
this.getDialogBase(dialog_id)
|
||||
this.generateUnreadData(old_id)
|
||||
//
|
||||
this.$store.dispatch('openDialogWebhook', dialog_id)
|
||||
//
|
||||
this.$store.dispatch('closeDialog', {id: old_id})
|
||||
//
|
||||
window.localStorage.removeItem('__cache:vote__')
|
||||
@ -1472,16 +1474,16 @@ export default {
|
||||
if (result.length) {
|
||||
return Array.from(new Set(result));
|
||||
}
|
||||
return useFallback ? ['message'] : [];
|
||||
return [];
|
||||
},
|
||||
prepareWebhookEvents(events, useFallback = false) {
|
||||
let value = events;
|
||||
if (typeof value === 'undefined' || value === null) {
|
||||
value = useFallback ? ['message'] : [];
|
||||
value = [];
|
||||
}
|
||||
value = this.normalizeWebhookEvents(value, false);
|
||||
if (!value.length && useFallback) {
|
||||
return ['message'];
|
||||
return [];
|
||||
}
|
||||
return value;
|
||||
},
|
||||
|
||||
31
resources/assets/js/store/actions.js
vendored
31
resources/assets/js/store/actions.js
vendored
@ -3514,6 +3514,35 @@ export default {
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* 打开会话(打开机器人会话推送 webhook)
|
||||
* @param state
|
||||
* @param dispatch
|
||||
* @param dialogId
|
||||
* @returns {Promise<unknown>}
|
||||
*/
|
||||
openDialogWebhook({state, dispatch}, dialogId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const dialog = state.cacheDialogs.find(item => {
|
||||
if (item.type !== 'user') {
|
||||
return false
|
||||
}
|
||||
return item.id === dialogId
|
||||
});
|
||||
if (dialog && dialog.bot === 1) {
|
||||
dispatch("call", {
|
||||
url: 'dialog/open/webhook',
|
||||
data: {
|
||||
dialog_id: dialogId,
|
||||
},
|
||||
}).catch(e => {
|
||||
console.warn(e);
|
||||
reject(e);
|
||||
})
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 打开会话(通过会员ID打开个人会话)
|
||||
* @param state
|
||||
@ -3528,7 +3557,7 @@ export default {
|
||||
}
|
||||
return item.dialog_user.userid === userid
|
||||
});
|
||||
if (dialog && dialog.bot !== 1) {
|
||||
if (dialog) {
|
||||
return dispatch("openDialog", dialog.id).then(resolve).catch(reject)
|
||||
}
|
||||
dispatch("call", {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user