fix(whatsapp): handle group mentions and caller allowlists

This commit is contained in:
xmanrui 2026-08-26 17:07:29 +08:00
parent 54468bbe1e
commit 9664e85599
13 changed files with 563 additions and 214 deletions

View file

@ -97,6 +97,11 @@ function normalizeBot(value) {
typeof entry === 'string' && /^[1-9]\d{4,14}$/.test(entry)
)))]
: [],
groupAllowedNumbers: Array.isArray(value.accessPolicy?.groupAllowedNumbers)
? [...new Set(value.accessPolicy.groupAllowedNumbers.filter((entry) => (
typeof entry === 'string' && /^[1-9]\d{4,14}$/.test(entry)
)))]
: [],
},
bot: {
name: text(value.bot?.name, 'WhatsApp机器人', 100),

View file

@ -37,6 +37,8 @@ function accessPolicyFor(account) {
accessMode,
allowedNumbers: Array.isArray(account?.accessPolicy?.allowedNumbers)
? account.accessPolicy.allowedNumbers : [],
groupAllowedNumbers: Array.isArray(account?.accessPolicy?.groupAllowedNumbers)
? account.accessPolicy.groupAllowedNumbers : [],
};
}
@ -52,30 +54,39 @@ function allowedNumbersFromText(value) {
export function WhatsappAccessSettings({ account, busy = false, onSave }) {
const policy = accessPolicyFor(account);
const sourceNumbers = policy.allowedNumbers.join('\n');
const sourceGroupNumbers = policy.groupAllowedNumbers.join('\n');
const helpId = React.useId();
const [accessMode, setAccessMode] = React.useState(policy.accessMode);
const [allowedNumbers, setAllowedNumbers] = React.useState(sourceNumbers);
const [groupAllowedNumbers, setGroupAllowedNumbers] = React.useState(sourceGroupNumbers);
const [error, setError] = React.useState(null);
React.useEffect(() => {
setAccessMode(policy.accessMode);
setAllowedNumbers(sourceNumbers);
setGroupAllowedNumbers(sourceGroupNumbers);
setError(null);
}, [policy.accessMode, sourceNumbers]);
}, [policy.accessMode, sourceNumbers, sourceGroupNumbers]);
const save = async (event) => {
event.preventDefault();
setError(null);
try {
const normalized = allowedNumbersFromText(allowedNumbers);
const normalizedGroup = allowedNumbersFromText(groupAllowedNumbers);
if (typeof onSave !== 'function') throw new Error('WhatsApp 访问设置暂不可用。');
await onSave({ accessMode, allowedNumbers: normalized });
await onSave({
accessMode,
allowedNumbers: normalized,
groupAllowedNumbers: normalizedGroup,
});
} catch (caught) {
setError(caught?.message ?? 'WhatsApp 访问设置保存失败。');
}
};
const allowlistEnabled = accessMode === 'private-allowlist';
const groupAllowlistEnabled = accessMode === 'open';
const labels = {
'self-only': '仅自己模式',
'private-allowlist': '指定联系人模式',
@ -103,7 +114,7 @@ export function WhatsappAccessSettings({ account, busy = false, onSave }) {
h('span', null, '响应自聊和白名单联系人的私聊,忽略群聊。')),
h('span', { className: 'dwa-accessTooltipItem' },
h('strong', null, '开放响应模式'),
h('span', null, '响应所有私聊、已绑定账号自己发出的群聊消息,以及其他群成员的提及或回复。')))))),
h('span', null, '响应所有私聊、已绑定账号自己发出的群聊消息,以及允许成员的提及或回复;群聊号码列表留空时允许所有群成员。')))))),
h('label', { className: 'dwa-accessField' },
h('span', null, '模式'),
h('select', {
@ -115,18 +126,28 @@ export function WhatsappAccessSettings({ account, busy = false, onSave }) {
h('option', { value: 'self-only' }, '仅自己模式(默认)'),
h('option', { value: 'private-allowlist' }, '指定联系人模式'),
h('option', { value: 'open' }, '开放响应模式'))),
allowlistEnabled
(allowlistEnabled || groupAllowlistEnabled)
? h('label', { className: 'dwa-accessField' },
h('span', null, '允许私聊的 WhatsApp 电话号码'),
h('span', null, allowlistEnabled
? '允许私聊的 WhatsApp 电话号码'
: '允许在群聊中呼叫机器人的 WhatsApp 电话号码'),
h('textarea', {
value: allowedNumbers,
value: allowlistEnabled ? allowedNumbers : groupAllowedNumbers,
disabled: busy,
rows: 3,
placeholder: '每行一个含国家或地区代码的号码',
'aria-label': '允许私聊的 WhatsApp 电话号码',
onChange: (event) => { setAllowedNumbers(event.target.value); setError(null); },
'aria-label': allowlistEnabled
? '允许私聊的 WhatsApp 电话号码'
: '允许在群聊中呼叫机器人的 WhatsApp 电话号码',
onChange: (event) => {
if (allowlistEnabled) setAllowedNumbers(event.target.value);
else setGroupAllowedNumbers(event.target.value);
setError(null);
},
}),
h('small', null, '可以包含开头的 +,保存时会自动移除。'))
h('small', null, allowlistEnabled
? '可以包含开头的 +,保存时会自动移除。'
: '留空表示所有群成员都可以通过提及或回复呼叫机器人。'))
: null,
allowlistEnabled && allowedNumbers.trim() === ''
? h('p', { className: 'dwa-accessWarning', role: 'status' },