feat: media send/receive (intent-based + notification bridge)
- POST /messages/send-media: multipart upload + adb push + intent + Appium - Webhook mediaType: image detection + adb pull + fanout met download-url - Download endpoint /download/:token (10 min TTL, cleanup every 5 min) - Android NotificationListener: detecteert media-notificaties (lege/emoji text) - Dockerfile: adb toegevoegd voor push/pull - MEDIA_WAIT_MS env var (default 8000ms) voor pull-timing
This commit is contained in:
parent
13ef16ac0f
commit
dc121f71de
1 changed files with 370 additions and 19 deletions
|
|
@ -1,10 +1,27 @@
|
|||
const express = require('express');
|
||||
const { remote } = require('webdriverio');
|
||||
const multer = require('multer');
|
||||
const { execSync, exec } = require('child_process');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
const APPIUM_HOST = process.env.APPIUM_HOST || '127.0.0.1';
|
||||
const APPIUM_PORT = parseInt(process.env.APPIUM_PORT || '4723', 10);
|
||||
const PORT = parseInt(process.env.PORT || '3010', 10);
|
||||
const WEBHOOK_SECRET = process.env.WEBHOOK_SHARED_SECRET || 'dev-secret-change-me';
|
||||
const MEDIA_WAIT_MS = parseInt(process.env.MEDIA_WAIT_MS || '8000', 10);
|
||||
const WHATSAPP_IMAGES_DIR = '/sdcard/Android/media/com.whatsapp/WhatsApp/Media/WhatsApp Images';
|
||||
|
||||
const upload = multer({
|
||||
dest: '/tmp/wa-uploads/',
|
||||
limits: { fileSize: 20 * 1024 * 1024 },
|
||||
fileFilter: (_req, file, cb) => {
|
||||
if (!file.mimetype.startsWith('image/')) {
|
||||
return cb(new Error('Only image files are supported'));
|
||||
}
|
||||
cb(null, true);
|
||||
},
|
||||
});
|
||||
|
||||
const app = express();
|
||||
app.use(express.json({ limit: '10mb' }));
|
||||
|
|
@ -55,6 +72,10 @@ function find(d, selector, timeout = 5000) {
|
|||
return d.$(selector).waitForExist({ timeout }).then(() => d.$(selector), () => null);
|
||||
}
|
||||
|
||||
function adb(args) {
|
||||
return execSync(`adb ${args}`, { timeout: 30000, encoding: 'utf8' }).trim();
|
||||
}
|
||||
|
||||
async function ensureWhatsApp(d) {
|
||||
const pkg = await d.getCurrentPackage();
|
||||
if (pkg !== 'com.whatsapp') {
|
||||
|
|
@ -89,7 +110,6 @@ async function openChat(d, query) {
|
|||
await searchInput.setValue(query);
|
||||
await d.pause(2000);
|
||||
|
||||
// Find and click the matching chat row
|
||||
const chats = await d.$$(uia('resourceId("com.whatsapp:id/contact_row_container")'));
|
||||
for (const row of chats) {
|
||||
const nameEl = await row.$(
|
||||
|
|
@ -102,7 +122,6 @@ async function openChat(d, query) {
|
|||
return;
|
||||
}
|
||||
}
|
||||
// If no match by name, try clicking first result
|
||||
if (chats.length > 0) {
|
||||
await chats[0].click();
|
||||
await d.pause(1500);
|
||||
|
|
@ -111,16 +130,188 @@ async function openChat(d, query) {
|
|||
throw new Error(`No chat found for "${query}"`);
|
||||
}
|
||||
|
||||
// ── Debug ─────────────────────────────────────────────────────────
|
||||
// ── ADB helpers ────────────────────────────────────────────────
|
||||
|
||||
function adbPush(localPath, remotePath) {
|
||||
return adb(`push "${localPath}" "${remotePath}"`);
|
||||
}
|
||||
|
||||
function adbRm(remotePath) {
|
||||
return adb(`shell rm -f "${remotePath}"`);
|
||||
}
|
||||
|
||||
// ── Endpoints ─────────────────────────────────────────────────────
|
||||
function adbShell(cmd) {
|
||||
return adb(`shell ${cmd}`);
|
||||
}
|
||||
|
||||
function adbLatestFile(dir) {
|
||||
const out = adb(`shell ls -t "${dir}/" 2>/dev/null | head -1`);
|
||||
return out ? `${dir}/${out}` : null;
|
||||
}
|
||||
|
||||
function adbPull(remotePath, localPath) {
|
||||
return adb(`pull "${remotePath}" "${localPath}"`);
|
||||
}
|
||||
|
||||
// ── Media send ─────────────────────────────────────────────────
|
||||
|
||||
async function sendMediaViaShareIntent(d, chatName, localPath, mimetype, caption) {
|
||||
const ext = path.extname(localPath) || '.jpg';
|
||||
const remoteName = `wa_${Date.now()}${ext}`;
|
||||
const remotePath = `/sdcard/Pictures/${remoteName}`;
|
||||
|
||||
adbPush(localPath, remotePath);
|
||||
|
||||
try {
|
||||
adbShell(
|
||||
`am start -a android.intent.action.SEND ` +
|
||||
`-t ${mimetype} ` +
|
||||
`--eu android.intent.extra.STREAM file://${remotePath} ` +
|
||||
`-p com.whatsapp`
|
||||
);
|
||||
await d.pause(3000);
|
||||
|
||||
const searchInput = await find(
|
||||
d,
|
||||
uia('resourceId("com.whatsapp:id/contactpicker_search")'),
|
||||
5000
|
||||
);
|
||||
if (!searchInput) {
|
||||
throw new Error('Could not find contact picker search in share screen');
|
||||
}
|
||||
|
||||
await searchInput.clearValue();
|
||||
await d.pause(300);
|
||||
await searchInput.setValue(chatName);
|
||||
await d.pause(2000);
|
||||
|
||||
const contacts = await d.$$(
|
||||
uia('resourceId("com.whatsapp:id/contactpicker_row_name")')
|
||||
);
|
||||
let clicked = false;
|
||||
for (const c of contacts) {
|
||||
const name = await c.getText().catch(() => '');
|
||||
if (name.toLowerCase().includes(chatName.toLowerCase())) {
|
||||
await c.click();
|
||||
clicked = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!clicked && contacts.length > 0) {
|
||||
await contacts[0].click();
|
||||
clicked = true;
|
||||
}
|
||||
if (!clicked) throw new Error(`Contact "${chatName}" not found in share picker`);
|
||||
|
||||
await d.pause(3000);
|
||||
|
||||
if (caption) {
|
||||
const captionInput = await find(
|
||||
d,
|
||||
uia('resourceId("com.whatsapp:id/caption")'),
|
||||
3000
|
||||
);
|
||||
if (captionInput) {
|
||||
await captionInput.clearValue();
|
||||
await d.pause(200);
|
||||
await captionInput.setValue(caption);
|
||||
await d.pause(500);
|
||||
} else {
|
||||
const entry = await find(d, uia('resourceId("com.whatsapp:id/entry")'), 2000);
|
||||
if (entry) {
|
||||
await entry.setValue(caption);
|
||||
await d.pause(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let sendBtn = await find(d, uia('resourceId("com.whatsapp:id/send")'), 5000);
|
||||
if (!sendBtn) {
|
||||
sendBtn = await find(d, uia('descriptionContains("Send")'), 3000);
|
||||
}
|
||||
if (!sendBtn) throw new Error('Could not find send button after selecting contact');
|
||||
|
||||
await sendBtn.click();
|
||||
await d.pause(2000);
|
||||
|
||||
goHome(d).catch(() => {});
|
||||
} finally {
|
||||
adbRm(remotePath);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Media receive ───────────────────────────────────────────────
|
||||
|
||||
function waitForNewImage(beforeTimestamp) {
|
||||
const t0 = beforeTimestamp || Date.now();
|
||||
const maxWait = MEDIA_WAIT_MS;
|
||||
const pollInterval = 1000;
|
||||
let waited = 0;
|
||||
|
||||
while (waited < maxWait) {
|
||||
const latest = adbLatestFile(WHATSAPP_IMAGES_DIR);
|
||||
if (latest) {
|
||||
const tsMatch = adbShell(`stat -c '%Y' "${latest}" 2>/dev/null || echo 0`);
|
||||
const ts = parseInt(tsMatch, 10) * 1000;
|
||||
if (ts >= t0) return latest;
|
||||
}
|
||||
new (require('events').EventEmitter)().setMaxListeners(100);
|
||||
const start = Date.now();
|
||||
while (Date.now() - start < pollInterval) {}
|
||||
waited += pollInterval;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function pullImageFile(remotePath) {
|
||||
const filename = path.basename(remotePath);
|
||||
const localPath = `/tmp/wa-downloads/${filename}`;
|
||||
fs.mkdirSync('/tmp/wa-downloads', { recursive: true });
|
||||
adbPull(remotePath, localPath);
|
||||
return localPath;
|
||||
}
|
||||
|
||||
let mediaTokenCounter = 0;
|
||||
const mediaDownloads = new Map();
|
||||
|
||||
function fanoutMedia(event, localPath) {
|
||||
const token = `media_${Date.now()}_${++mediaTokenCounter}`;
|
||||
const filename = path.basename(localPath);
|
||||
mediaDownloads.set(token, { path: localPath, filename, createdAt: Date.now() });
|
||||
|
||||
const downloadUrl = `http://127.0.0.1:${PORT}/download/${token}`;
|
||||
const eventWithUrl = { ...event, mediaUrl: downloadUrl, mediaFilename: filename };
|
||||
|
||||
for (const sub of subscribers) {
|
||||
fetch(sub.url, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(eventWithUrl),
|
||||
}).then((r) => {
|
||||
console.log(` media fanout to ${sub.url}: ${r.status}`);
|
||||
}).catch((e) => {
|
||||
console.log(` media fanout to ${sub.url} failed: ${e.message}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup old downloads every 5 min
|
||||
setInterval(() => {
|
||||
const cutoff = Date.now() - 10 * 60 * 1000;
|
||||
for (const [token, entry] of mediaDownloads) {
|
||||
if (entry.createdAt < cutoff) {
|
||||
try { fs.unlinkSync(entry.path); } catch {}
|
||||
mediaDownloads.delete(token);
|
||||
}
|
||||
}
|
||||
}, 5 * 60 * 1000);
|
||||
|
||||
// ── Endpoints (Appium) ─────────────────────────────────────────
|
||||
|
||||
app.get('/health', async (_req, res) => {
|
||||
try {
|
||||
const d = await getDriver();
|
||||
const session = await d.getSession();
|
||||
await d.getSession();
|
||||
res.json({ status: 'ok', sessionId: d.sessionId });
|
||||
} catch (e) {
|
||||
res.status(503).json({ status: 'error', message: e.message });
|
||||
|
|
@ -194,9 +385,7 @@ app.post('/chat/fetchMessages/:session', async (req, res) => {
|
|||
for (const layout of layoutEls) {
|
||||
try {
|
||||
const textEl = await layout.$(uia('resourceId("com.whatsapp:id/message_text")'));
|
||||
const dateEl = await layout.$(
|
||||
uia('resourceId("com.whatsapp:id/date")')
|
||||
);
|
||||
const dateEl = await layout.$(uia('resourceId("com.whatsapp:id/date")'));
|
||||
if (await textEl.isExisting()) {
|
||||
messages.push({
|
||||
text: await textEl.getText(),
|
||||
|
|
@ -246,7 +435,32 @@ app.post('/client/sendMessage/:session', async (req, res) => {
|
|||
}
|
||||
});
|
||||
|
||||
// ── Webhook subscriber store (in-memory) ─────────────────────
|
||||
// ── Media send endpoint ────────────────────────────────────────
|
||||
|
||||
app.post('/messages/send-media', upload.single('image'), async (req, res) => {
|
||||
let localPath = null;
|
||||
try {
|
||||
const { chatName, caption } = req.body;
|
||||
const file = req.file;
|
||||
if (!chatName) return res.status(400).json({ error: 'chatName required' });
|
||||
if (!file) return res.status(400).json({ error: 'image file required' });
|
||||
|
||||
localPath = file.path;
|
||||
const d = await getDriver();
|
||||
|
||||
await sendMediaViaShareIntent(d, chatName, localPath, file.mimetype, caption || '');
|
||||
|
||||
res.json({ status: 'sent', chatName, caption: caption || '' });
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: e.message });
|
||||
} finally {
|
||||
if (localPath) {
|
||||
try { fs.unlinkSync(localPath); } catch {}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// ── Webhook subscriber store ─────────────────────────────────────
|
||||
|
||||
const subscribers = [];
|
||||
|
||||
|
|
@ -272,14 +486,43 @@ app.post('/webhooks/whatsapp/incoming', (req, res) => {
|
|||
return res.status(401).json({ error: 'invalid or missing webhook secret' });
|
||||
}
|
||||
|
||||
const { sender, text, chatId, timestamp } = req.body;
|
||||
if (!sender || !text || !chatId) {
|
||||
return res.status(400).json({ error: 'sender, text, and chatId are required' });
|
||||
const { sender, text, chatId, timestamp, mediaType } = req.body;
|
||||
if (!sender || !chatId) {
|
||||
return res.status(400).json({ error: 'sender and chatId are required' });
|
||||
}
|
||||
|
||||
const event = { sender, text, chatId, timestamp: timestamp || Date.now(), receivedAt: new Date().toISOString() };
|
||||
fanout(event);
|
||||
res.json({ status: 'ok' });
|
||||
const event = {
|
||||
sender,
|
||||
text: text || '',
|
||||
chatId,
|
||||
timestamp: timestamp || Date.now(),
|
||||
receivedAt: new Date().toISOString(),
|
||||
mediaType: mediaType || 'text',
|
||||
};
|
||||
|
||||
if (mediaType === 'image') {
|
||||
res.json({ status: 'ok', mediaProcessing: 'pending' });
|
||||
|
||||
setImmediate(async () => {
|
||||
try {
|
||||
const imagePath = waitForNewImage(event.timestamp);
|
||||
if (!imagePath) {
|
||||
console.log(` media: no new image found for ${chatId} from ${sender}`);
|
||||
fanout({ ...event, mediaStatus: 'not-found' });
|
||||
return;
|
||||
}
|
||||
const localPath = pullImageFile(imagePath);
|
||||
console.log(` media: pulled ${imagePath} → ${localPath}`);
|
||||
fanoutMedia({ ...event, mediaStatus: 'downloaded' }, localPath);
|
||||
} catch (e) {
|
||||
console.log(` media processing error: ${e.message}`);
|
||||
fanout({ ...event, mediaStatus: 'error', error: e.message });
|
||||
}
|
||||
});
|
||||
} else {
|
||||
fanout({ ...event, mediaType: 'text' });
|
||||
res.json({ status: 'ok' });
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/webhooks/subscribe', (req, res) => {
|
||||
|
|
@ -296,9 +539,21 @@ app.get('/webhooks/subscribers', (_req, res) => {
|
|||
res.json({ subscribers: subscribers.map((s) => ({ url: s.url, createdAt: s.createdAt })) });
|
||||
});
|
||||
|
||||
// ── Media download endpoint ──────────────────────────────────
|
||||
|
||||
app.get('/download/:token', (req, res) => {
|
||||
const entry = mediaDownloads.get(req.params.token);
|
||||
if (!entry) return res.status(404).json({ error: 'download expired or not found' });
|
||||
res.download(entry.path, entry.filename);
|
||||
});
|
||||
|
||||
// ── Server start ─────────────────────────────────────────────
|
||||
|
||||
fs.mkdirSync('/tmp/wa-uploads', { recursive: true });
|
||||
fs.mkdirSync('/tmp/wa-downloads', { recursive: true });
|
||||
|
||||
app.listen(PORT, '0.0.0.0', () => {
|
||||
const host = require('os').networkInterfaces();
|
||||
console.log(`wa-appium-api listening on http://0.0.0.0:${PORT}`);
|
||||
console.log(` Appium server: ${APPIUM_HOST}:${APPIUM_PORT}`);
|
||||
console.log(` Webhook secret: ${WEBHOOK_SECRET === 'dev-secret-change-me' ? 'WARNING: using dev default' : 'configured'}`);
|
||||
|
|
|
|||
Loading…
Reference in a new issue