mirror of
https://github.com/hansjone/dsh-im-ops.git
synced 2026-10-09 03:03:24 +08:00
fix(whatsapp): handle group mentions and caller allowlists
This commit is contained in:
parent
54468bbe1e
commit
9664e85599
13 changed files with 563 additions and 214 deletions
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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' },
|
||||
|
|
|
|||
|
|
@ -380,9 +380,12 @@ const EN = Object.freeze({
|
|||
'只响应已绑定 WhatsApp 账号的自聊消息。': 'Only respond to self-chat messages from the linked WhatsApp account.',
|
||||
'响应自聊和白名单联系人的私聊,忽略群聊。': 'Respond to self-chat and allowlisted direct messages; ignore group messages.',
|
||||
'响应所有私聊、已绑定账号自己发出的群聊消息,以及其他群成员的提及或回复。': 'Respond to all direct messages, group messages sent by the linked account, and mentions or replies from other group members.',
|
||||
'响应所有私聊、已绑定账号自己发出的群聊消息,以及允许成员的提及或回复;群聊号码列表留空时允许所有群成员。': 'Respond to all direct messages, group messages sent by the linked account, and mentions or replies from allowed members; leaving the group number list empty allows every group member.',
|
||||
'允许私聊的 WhatsApp 电话号码': 'WhatsApp phone numbers allowed to send direct messages',
|
||||
'允许在群聊中呼叫机器人的 WhatsApp 电话号码': 'WhatsApp phone numbers allowed to call the bot in group chats',
|
||||
'每行一个含国家或地区代码的号码': 'One number with country or region code per line',
|
||||
'可以包含开头的 +,保存时会自动移除。': 'A leading + is allowed and removed when saved.',
|
||||
'留空表示所有群成员都可以通过提及或回复呼叫机器人。': 'Leave empty to let every group member call the bot by mentioning it or replying to it.',
|
||||
'仅指定联系人模式使用白名单,切换模式时会保留。': 'Only Selected contacts uses the allowlist; it is retained when modes change.',
|
||||
'白名单为空;保存后将只接受自聊消息。': 'The allowlist is empty; only self-chat messages will be accepted after saving.',
|
||||
'电话号码必须包含国家或地区代码,每行一个。': 'Each phone number must include a country or region code on its own line.',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue