-
Notifications
You must be signed in to change notification settings - Fork 47.1k
feat(whatsapp): notify the owner when a non-allowlisted sender messages #53745
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
Open
marcelopaniza
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
marcelopaniza:feat/whatsapp-stranger-knock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,13 +23,13 @@ import express from 'express'; | |
| import { Boom } from '@hapi/boom'; | ||
| import pino from 'pino'; | ||
| import path from 'path'; | ||
| import { mkdirSync, readFileSync, writeFileSync, existsSync, readdirSync, unlinkSync } from 'fs'; | ||
| import { mkdirSync, readFileSync, writeFileSync, existsSync, readdirSync, unlinkSync, appendFileSync } from 'fs'; | ||
| import { fileURLToPath } from 'url'; | ||
| import { randomBytes, createHash } from 'crypto'; | ||
| import { execSync } from 'child_process'; | ||
| import { tmpdir } from 'os'; | ||
| import qrcode from 'qrcode-terminal'; | ||
| import { matchesAllowedUser, parseAllowedUsers } from './allowlist.js'; | ||
| import { matchesAllowedUser, parseAllowedUsers, normalizeWhatsAppIdentifier } from './allowlist.js'; | ||
|
|
||
| // Parse CLI args | ||
| const args = process.argv.slice(2); | ||
|
|
@@ -190,6 +190,52 @@ const logger = pino({ level: 'warn' }); | |
| const messageQueue = []; | ||
| const MAX_QUEUE_SIZE = 100; | ||
|
|
||
| // ── Stranger-knocking support ─────────────────────────────────────────────── | ||
| // Owner-approved runtime allowlist additions, persisted next to the session so | ||
| // they survive a bridge restart, merged into ALLOWED_USERS at startup. | ||
| const RUNTIME_ALLOW_FILE = path.join(path.dirname(SESSION_DIR), 'allowlist-runtime.txt'); | ||
| try { | ||
| if (existsSync(RUNTIME_ALLOW_FILE)) { | ||
| for (const line of readFileSync(RUNTIME_ALLOW_FILE, 'utf8').split('\n')) { | ||
| const id = normalizeWhatsAppIdentifier(line); | ||
| if (id) ALLOWED_USERS.add(id); | ||
| } | ||
| } | ||
| } catch {} | ||
| // De-dup "stranger knocking" notices: at most one per sender per window. | ||
| const KNOCK_DEDUP_MS = 6 * 60 * 60 * 1000; | ||
| const knockSeen = new Map(); | ||
| function maybeEmitKnock(msg, senderId, senderNumber, chatId) { | ||
| const tsNow = Date.now(); | ||
| const last = knockSeen.get(senderId) || 0; | ||
| if (tsNow - last < KNOCK_DEDUP_MS) return; | ||
| knockSeen.set(senderId, tsNow); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| let preview = ''; | ||
| try { | ||
| const mc = getMessageContent(msg) || {}; | ||
| preview = mc.conversation || (mc.extendedTextMessage && mc.extendedTextMessage.text) || ''; | ||
| if (!preview) { | ||
| if (mc.imageMessage) preview = '[photo]'; | ||
| else if (mc.videoMessage) preview = '[video]'; | ||
| else if (mc.audioMessage || mc.pttMessage) preview = '[voice note]'; | ||
| else if (mc.documentMessage) preview = '[document]'; | ||
| else preview = '[message]'; | ||
| } | ||
| } catch { preview = '[message]'; } | ||
| preview = String(preview).replace(/\s+/g, ' ').trim().slice(0, 140); | ||
| messageQueue.push({ | ||
| knock: true, | ||
| messageId: msg.key.id, | ||
| chatId, | ||
| senderId, | ||
| senderNumber, | ||
| senderName: msg.pushName || senderNumber, | ||
| preview, | ||
| timestamp: msg.messageTimestamp, | ||
| }); | ||
| if (messageQueue.length > MAX_QUEUE_SIZE) messageQueue.shift(); | ||
| } | ||
|
|
||
| // Track recently sent message IDs to prevent echo-back loops with media | ||
| const recentlySentIds = new Set(); | ||
| const MAX_RECENT_IDS = 50; | ||
|
|
@@ -322,6 +368,9 @@ async function startSocket() { | |
| continue; | ||
| } | ||
| if (!matchesAllowedUser(senderId, ALLOWED_USERS, SESSION_DIR)) { | ||
| // Stranger (not on allowlist): emit a deduped "knock" so the gateway | ||
| // can alert the owner, then drop. The body never reaches the agent. | ||
| try { maybeEmitKnock(msg, senderId, senderNumber, chatId); } catch {} | ||
| try { | ||
| console.log(JSON.stringify({ | ||
| event: 'ignored', | ||
|
|
@@ -510,6 +559,17 @@ app.get('/messages', (req, res) => { | |
| }); | ||
|
|
||
| // Send a message | ||
| // Runtime allowlist add — owner-approved "stranger knocking" sender. | ||
| app.post('/allow', (req, res) => { | ||
| const raw = (req.body && (req.body.number || req.body.id)) || ''; | ||
| const id = normalizeWhatsAppIdentifier(raw); | ||
| if (!id) return res.status(400).json({ error: 'number/id required' }); | ||
| ALLOWED_USERS.add(id); | ||
| try { appendFileSync(RUNTIME_ALLOW_FILE, id + '\n'); } catch {} | ||
| try { console.log(JSON.stringify({ event: 'allowlist_add', id })); } catch {} | ||
| return res.json({ success: true, id }); | ||
| }); | ||
|
|
||
| app.post('/send', async (req, res) => { | ||
| if (!sock || connectionState !== 'connected') { | ||
| return res.status(503).json({ error: 'Not connected to WhatsApp' }); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
WHATSAPP_HOME_CHANNELis a generic notification/cron chat and may be a group. MatchingchatIdhere grants every allowed member of that group authority to add users. Require a verified owner sender identity, or reject group home channels for approval.