CognxSafeTrack commited on
Commit Β·
af32fee
1
Parent(s): 1f0e6e9
fix(whatsapp): remove httpsAgent IPv4 constraint causing ENOTFOUND and timeouts on Railway
Browse files
apps/api/src/routes/whatsapp.ts
CHANGED
|
@@ -3,9 +3,6 @@ import { WhatsAppService } from '../services/whatsapp';
|
|
| 3 |
import crypto from 'crypto';
|
| 4 |
import { z } from 'zod';
|
| 5 |
import axios from 'axios';
|
| 6 |
-
import https from 'https';
|
| 7 |
-
|
| 8 |
-
const httpsAgent = new https.Agent({ family: 4 });
|
| 9 |
|
| 10 |
// βββ Zod Schema for WhatsApp Webhook Payload βββββββββββββββββββββββββββββββββ
|
| 11 |
const WhatsAppMessageSchema = z.object({
|
|
@@ -152,8 +149,7 @@ export async function whatsappRoutes(fastify: FastifyInstance) {
|
|
| 152 |
|
| 153 |
// 1. Get media URL
|
| 154 |
const metaRes = await axios.get(`https://graph.facebook.com/v18.0/${audioId}`, {
|
| 155 |
-
headers: { 'Authorization': `Bearer ${accessToken}` }
|
| 156 |
-
httpsAgent
|
| 157 |
});
|
| 158 |
const meta = metaRes.data;
|
| 159 |
|
|
@@ -161,8 +157,7 @@ export async function whatsappRoutes(fastify: FastifyInstance) {
|
|
| 161 |
if (meta.url) {
|
| 162 |
const mediaRes = await axios.get(meta.url, {
|
| 163 |
headers: { 'Authorization': `Bearer ${accessToken}` },
|
| 164 |
-
responseType: 'arraybuffer'
|
| 165 |
-
httpsAgent
|
| 166 |
});
|
| 167 |
const buffer = Buffer.from(mediaRes.data);
|
| 168 |
|
|
|
|
| 3 |
import crypto from 'crypto';
|
| 4 |
import { z } from 'zod';
|
| 5 |
import axios from 'axios';
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
// βββ Zod Schema for WhatsApp Webhook Payload βββββββββββββββββββββββββββββββββ
|
| 8 |
const WhatsAppMessageSchema = z.object({
|
|
|
|
| 149 |
|
| 150 |
// 1. Get media URL
|
| 151 |
const metaRes = await axios.get(`https://graph.facebook.com/v18.0/${audioId}`, {
|
| 152 |
+
headers: { 'Authorization': `Bearer ${accessToken}` }
|
|
|
|
| 153 |
});
|
| 154 |
const meta = metaRes.data;
|
| 155 |
|
|
|
|
| 157 |
if (meta.url) {
|
| 158 |
const mediaRes = await axios.get(meta.url, {
|
| 159 |
headers: { 'Authorization': `Bearer ${accessToken}` },
|
| 160 |
+
responseType: 'arraybuffer'
|
|
|
|
| 161 |
});
|
| 162 |
const buffer = Buffer.from(mediaRes.data);
|
| 163 |
|
apps/whatsapp-worker/src/whatsapp-cloud.ts
CHANGED
|
@@ -8,10 +8,6 @@
|
|
| 8 |
*/
|
| 9 |
|
| 10 |
import axios from 'axios';
|
| 11 |
-
import https from 'https';
|
| 12 |
-
|
| 13 |
-
// Force IPv4 resolution for Meta Graph API which fails with IPv6 in Docker on Hugging Face
|
| 14 |
-
const httpsAgent = new https.Agent({ family: 4 });
|
| 15 |
|
| 16 |
const GRAPH_API_VERSION = 'v18.0';
|
| 17 |
|
|
@@ -45,7 +41,7 @@ export async function sendTextMessage(to: string, text: string): Promise<void> {
|
|
| 45 |
};
|
| 46 |
|
| 47 |
try {
|
| 48 |
-
await axios.post(getBaseUrl(), body, { headers: getHeaders()
|
| 49 |
} catch (err: any) {
|
| 50 |
throw new Error(`[WhatsApp] sendTextMessage failed: ${err.response?.data?.error?.message || err.message}`);
|
| 51 |
}
|
|
@@ -74,7 +70,7 @@ export async function sendDocumentMessage(to: string, fileUrl: string, filename:
|
|
| 74 |
};
|
| 75 |
|
| 76 |
try {
|
| 77 |
-
await axios.post(getBaseUrl(), body, { headers: getHeaders()
|
| 78 |
} catch (err: any) {
|
| 79 |
throw new Error(`[WhatsApp] sendDocumentMessage failed: ${err.response?.data?.error?.message || err.message}`);
|
| 80 |
}
|
|
@@ -97,7 +93,7 @@ export async function sendAudioMessage(to: string, audioUrl: string): Promise<vo
|
|
| 97 |
};
|
| 98 |
|
| 99 |
try {
|
| 100 |
-
await axios.post(getBaseUrl(), body, { headers: getHeaders()
|
| 101 |
} catch (err: any) {
|
| 102 |
throw new Error(`[WhatsApp] sendAudioMessage failed: ${err.response?.data?.error?.message || err.message}`);
|
| 103 |
}
|
|
@@ -137,7 +133,7 @@ export async function sendInteractiveButtonMessage(
|
|
| 137 |
};
|
| 138 |
|
| 139 |
try {
|
| 140 |
-
await axios.post(getBaseUrl(), body, { headers: getHeaders()
|
| 141 |
} catch (err: any) {
|
| 142 |
throw new Error(`[WhatsApp] sendInteractiveButtonMessage failed: ${err.response?.data?.error?.message || err.message}`);
|
| 143 |
}
|
|
|
|
| 8 |
*/
|
| 9 |
|
| 10 |
import axios from 'axios';
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
const GRAPH_API_VERSION = 'v18.0';
|
| 13 |
|
|
|
|
| 41 |
};
|
| 42 |
|
| 43 |
try {
|
| 44 |
+
await axios.post(getBaseUrl(), body, { headers: getHeaders() });
|
| 45 |
} catch (err: any) {
|
| 46 |
throw new Error(`[WhatsApp] sendTextMessage failed: ${err.response?.data?.error?.message || err.message}`);
|
| 47 |
}
|
|
|
|
| 70 |
};
|
| 71 |
|
| 72 |
try {
|
| 73 |
+
await axios.post(getBaseUrl(), body, { headers: getHeaders() });
|
| 74 |
} catch (err: any) {
|
| 75 |
throw new Error(`[WhatsApp] sendDocumentMessage failed: ${err.response?.data?.error?.message || err.message}`);
|
| 76 |
}
|
|
|
|
| 93 |
};
|
| 94 |
|
| 95 |
try {
|
| 96 |
+
await axios.post(getBaseUrl(), body, { headers: getHeaders() });
|
| 97 |
} catch (err: any) {
|
| 98 |
throw new Error(`[WhatsApp] sendAudioMessage failed: ${err.response?.data?.error?.message || err.message}`);
|
| 99 |
}
|
|
|
|
| 133 |
};
|
| 134 |
|
| 135 |
try {
|
| 136 |
+
await axios.post(getBaseUrl(), body, { headers: getHeaders() });
|
| 137 |
} catch (err: any) {
|
| 138 |
throw new Error(`[WhatsApp] sendInteractiveButtonMessage failed: ${err.response?.data?.error?.message || err.message}`);
|
| 139 |
}
|