-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat: convert LID to phoneNumber on GROUP_PARTICIPANTS_UPDATE webhook #2025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
GROUP_PARTICIPANTS_UPDATE
Reviewer's GuideRefactor the GROUP_PARTICIPANTS_UPDATE webhook handler to asynchronously fetch group metadata for LID-to-phoneNumber resolution, enrich participant records (phoneNumber, name, imgUrl), and provide a fallback path on errors. Sequence diagram for GROUP_PARTICIPANTS_UPDATE webhook with LID to phoneNumber conversionsequenceDiagram
participant "Webhook Handler"
participant "Group Metadata Service (findParticipants)"
participant "Webhook Consumer"
participant "Logger"
"Webhook Handler"->>"Group Metadata Service (findParticipants)": Fetch group participants metadata
"Group Metadata Service (findParticipants)"-->>"Webhook Handler": Return enriched participant data
"Webhook Handler"->>"Webhook Consumer": Send GROUP_PARTICIPANTS_UPDATE with resolved phoneNumbers
Note over "Webhook Handler","Logger": On error, fallback to sending raw participant data
"Webhook Handler"->>"Logger": Log error
"Webhook Handler"->>"Webhook Consumer": Send GROUP_PARTICIPANTS_UPDATE with original data
Class diagram for enhanced participant data structure in webhookclassDiagram
class ParticipantUpdate {
+id: string
+participants: Participant[]
+action: ParticipantAction
}
class Participant {
+jid: string
+phoneNumber: string
+name: string
+imgUrl: string
}
ParticipantUpdate "1" *-- "*" Participant: contains
class ParticipantAction {
<<enum>>
}
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- This change alters the payload shape for GROUP_PARTICIPANTS_UPDATE (participants array now contains objects instead of strings), which could break existing webhook consumers—consider versioning the event or clearly documenting this schema change.
- Calling findParticipants on every update may add latency under load; consider caching resolved participant data or batching these lookups to reduce overhead.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- This change alters the payload shape for GROUP_PARTICIPANTS_UPDATE (participants array now contains objects instead of strings), which could break existing webhook consumers—consider versioning the event or clearly documenting this schema change.
- Calling findParticipants on every update may add latency under load; consider caching resolved participant data or batching these lookups to reduce overhead.
## Individual Comments
### Comment 1
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:1594-1599` </location>
<code_context>
},
- 'group-participants.update': (participantsUpdate: {
+ 'group-participants.update': async (participantsUpdate: {
id: string;
participants: string[];
action: ParticipantAction;
}) => {
- this.sendDataWebhook(Events.GROUP_PARTICIPANTS_UPDATE, participantsUpdate);
+ try {
+ // Usa o mesmo método que o endpoint /group/participants
+ const groupParticipants = await this.findParticipants({ groupJid: participantsUpdate.id });
</code_context>
<issue_to_address>
**issue:** Consider handling cases where findParticipants returns undefined or an unexpected structure.
Add validation to ensure groupParticipants and its participants property exist before mapping, to avoid undefined values or runtime errors.
</issue_to_address>
### Comment 2
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:1622` </location>
<code_context>
+
+ this.sendDataWebhook(Events.GROUP_PARTICIPANTS_UPDATE, enhancedParticipantsUpdate);
+ } catch (error) {
+ console.log('Erro ao buscar dados dos participantes para webhook:', error);
+ // Fallback - envia sem conversão
+ this.sendDataWebhook(Events.GROUP_PARTICIPANTS_UPDATE, participantsUpdate);
</code_context>
<issue_to_address>
**suggestion:** Using console.log for error reporting may not be ideal for production environments.
Recommend implementing a structured logging or error tracking solution to improve error visibility and monitoring in production.
Suggested implementation:
```typescript
this.logger.error('Erro ao buscar dados dos participantes para webhook', error);
```
If `this.logger` is not already defined in the class, you will need to:
1. Import a logger (e.g., `import { Logger } from '@nestjs/common';`).
2. Add `private readonly logger = new Logger(WhatsappBaileysService.name);` to the class constructor or as a class property.
If you use a different logging library, adjust the import and instantiation accordingly.
</issue_to_address>
### Comment 3
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:1604-1613` </location>
<code_context>
+
+ return {
+ jid: participantId,
+ phoneNumber: participantData?.phoneNumber || participantId,
+ name: participantData?.name,
+ imgUrl: participantData?.imgUrl,
</code_context>
<issue_to_address>
**suggestion:** Defaulting phoneNumber to participantId may cause confusion if formats differ.
Validate or normalize participantId before using it as a fallback for phoneNumber to prevent format inconsistencies.
```suggestion
// Helper to normalize participantId as phone number
function normalizePhoneNumber(id: string): string {
// Example normalization: remove non-digit characters and leading plus
// Adjust this logic to match your application's phone number format requirements
return id.replace(/[^\d]/g, '');
}
const resolvedParticipants = participantsUpdate.participants.map((participantId) => {
const participantData = groupParticipants.participants.find(p => p.id === participantId);
let phoneNumber: string;
if (participantData?.phoneNumber) {
phoneNumber = participantData.phoneNumber;
} else {
phoneNumber = normalizePhoneNumber(participantId);
}
return {
jid: participantId,
phoneNumber,
name: participantData?.name,
imgUrl: participantData?.imgUrl,
};
});
```
</issue_to_address>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
compatibility - Keep original participants array (string[]) for backward compatibility - Add new participantsData field with resolved phone numbers and metadata - Consumers can migrate gradually from participants to participantsData - No breaking changes to existing webhook integrations Payload structure: - participants: string[] (original JID strings) - participantsData: object[] (enhanced with phoneNumber, name, imgUrl)
I've resolved to NOT break the payload shape, thanks for the feedback. |
Fix the lint please |
that fix will save me, thank you 😭 |
📋 Description
Summary
This PR enhances the
GROUP_PARTICIPANTS_UPDATE
-> add - webhook to properlyconvert LID (Local Identifier) values to real phone numbers, ensuring
consistency with the existing
/group/participants
endpointbehavior.
Problem
Currently, when the
GROUP_PARTICIPANTS_UPDATE
webhook is triggered,participants with LID identifiers return the raw LID value instead
of the actual phone number:
Before:
json
{
"jid": "131159895875721@lid",
"phoneNumber": "131159895875721@lid"
}
Solution
The webhook now uses the same logic as the /group/participants
endpoint to resolve LID values to actual phone numbers:
After:
{
"jid": "131159895875721@lid",
"phoneNumber": "[email protected]"
}
Changes Made
findParticipants() method
metadata
Testing
PS: This solves my biggest problem around here on group entrances, but i don't know if it's the best approach.
🧪 Type of Change
🧪 Testing
📸 Screenshots (if applicable)
✅ Checklist
📝 Additional Notes
PS: This solves my biggest problem around here on group entrances, but i don't know if it's the best approach.
Summary by Sourcery
Enhance the GROUP_PARTICIPANTS_UPDATE webhook handler to fetch full participant metadata, convert LID identifiers to real phone numbers, and include name and image URL fields, while preserving backward compatibility and adding an error fallback.
New Features:
Bug Fixes:
Enhancements: