@@ -4,6 +4,13 @@ import type { loadConfig } from "openclaw/plugin-sdk/config-runtime";
44import { recordPendingHistoryEntryIfEnabled } from "openclaw/plugin-sdk/reply-history" ;
55import { parseActivationCommand } from "openclaw/plugin-sdk/reply-runtime" ;
66import { normalizeE164 } from "openclaw/plugin-sdk/text-runtime" ;
7+ import {
8+ getPrimaryIdentityId ,
9+ getReplyContext ,
10+ getSelfIdentity ,
11+ getSenderIdentity ,
12+ identitiesOverlap ,
13+ } from "../../identity.js" ;
714import type { MentionConfig } from "../mentions.js" ;
815import { buildMentionConfig , debugMention , resolveOwnerList } from "../mentions.js" ;
916import type { WebInboundMsg } from "../types.js" ;
@@ -36,11 +43,11 @@ type ApplyGroupGatingParams = {
3643} ;
3744
3845function isOwnerSender ( baseMentionConfig : MentionConfig , msg : WebInboundMsg ) {
39- const sender = normalizeE164 ( msg . senderE164 ?? "" ) ;
46+ const sender = normalizeE164 ( getSenderIdentity ( msg ) . e164 ?? "" ) ;
4047 if ( ! sender ) {
4148 return false ;
4249 }
43- const owners = resolveOwnerList ( baseMentionConfig , msg . selfE164 ?? undefined ) ;
50+ const owners = resolveOwnerList ( baseMentionConfig , getSelfIdentity ( msg ) . e164 ?? undefined ) ;
4451 return owners . includes ( sender ) ;
4552}
4653
@@ -50,10 +57,14 @@ function recordPendingGroupHistoryEntry(params: {
5057 groupHistoryKey : string ;
5158 groupHistoryLimit : number ;
5259} ) {
60+ const senderIdentity = getSenderIdentity ( params . msg ) ;
5361 const sender =
54- params . msg . senderName && params . msg . senderE164
55- ? `${ params . msg . senderName } (${ params . msg . senderE164 } )`
56- : ( params . msg . senderName ?? params . msg . senderE164 ?? "Unknown" ) ;
62+ senderIdentity . name && senderIdentity . e164
63+ ? `${ senderIdentity . name } (${ senderIdentity . e164 } )`
64+ : ( senderIdentity . name ??
65+ senderIdentity . e164 ??
66+ getPrimaryIdentityId ( senderIdentity ) ??
67+ "Unknown" ) ;
5768 recordPendingHistoryEntryIfEnabled ( {
5869 historyMap : params . groupHistories ,
5970 historyKey : params . groupHistoryKey ,
@@ -63,7 +74,7 @@ function recordPendingGroupHistoryEntry(params: {
6374 body : params . msg . body ,
6475 timestamp : params . msg . timestamp ,
6576 id : params . msg . id ,
66- senderJid : params . msg . senderJid ,
77+ senderJid : senderIdentity . jid ?? params . msg . senderJid ,
6778 } ,
6879 } ) ;
6980}
@@ -80,6 +91,8 @@ function skipGroupMessageAndStoreHistory(params: ApplyGroupGatingParams, verbose
8091}
8192
8293export function applyGroupGating ( params : ApplyGroupGatingParams ) {
94+ const sender = getSenderIdentity ( params . msg ) ;
95+ const self = getSelfIdentity ( params . msg , params . authDir ) ;
8396 const groupPolicy = resolveGroupPolicyFor ( params . cfg , params . conversationId ) ;
8497 if ( groupPolicy . allowlistEnabled && ! groupPolicy . allowed ) {
8598 params . logVerbose ( `Skipping group message ${ params . conversationId } (not in allowlist)` ) ;
@@ -89,15 +102,15 @@ export function applyGroupGating(params: ApplyGroupGatingParams) {
89102 noteGroupMember (
90103 params . groupMemberNames ,
91104 params . groupHistoryKey ,
92- params . msg . senderE164 ,
93- params . msg . senderName ,
105+ sender . e164 ?? undefined ,
106+ sender . name ?? undefined ,
94107 ) ;
95108
96109 const mentionConfig = buildMentionConfig ( params . cfg , params . agentId ) ;
97110 const commandBody = stripMentionsForCommand (
98111 params . msg . body ,
99112 mentionConfig . mentionRegexes ,
100- params . msg . selfE164 ,
113+ self . e164 ,
101114 ) ;
102115 const activationCommand = parseActivationCommand ( commandBody ) ;
103116 const owner = isOwnerSender ( params . baseMentionConfig , params . msg ) ;
@@ -127,21 +140,11 @@ export function applyGroupGating(params: ApplyGroupGatingParams) {
127140 conversationId : params . conversationId ,
128141 } ) ;
129142 const requireMention = activation !== "always" ;
130- const selfJid = params . msg . selfJid ?. replace ( / : \d + / , "" ) ;
131- const selfLid = params . msg . selfLid ?. replace ( / : \d + / , "" ) ;
132- const replySenderJid = params . msg . replyToSenderJid ?. replace ( / : \d + / , "" ) ;
133- const selfE164 = params . msg . selfE164 ? normalizeE164 ( params . msg . selfE164 ) : null ;
134- const replySenderE164 = params . msg . replyToSenderE164
135- ? normalizeE164 ( params . msg . replyToSenderE164 )
136- : null ;
143+ const replyContext = getReplyContext ( params . msg , params . authDir ) ;
137144 // Detect reply-to-bot: compare JIDs, LIDs, and E.164 numbers.
138145 // WhatsApp may report the quoted message sender as either a phone JID
139146 // (xxxxx@s.whatsapp.net) or a LID (xxxxx@lid), so we compare both.
140- const implicitMention = Boolean (
141- ( selfJid && replySenderJid && selfJid === replySenderJid ) ||
142- ( selfLid && replySenderJid && selfLid === replySenderJid ) ||
143- ( selfE164 && replySenderE164 && selfE164 === replySenderE164 ) ,
144- ) ;
147+ const implicitMention = identitiesOverlap ( self , replyContext ?. sender ) ;
145148 const mentionGate = resolveMentionGating ( {
146149 requireMention,
147150 canDetectMention : true ,
0 commit comments