Spaces:
Sleeping
Sleeping
| /** | |
| * Google Apps Script - WhatsApp API Proxy | |
| * | |
| * This script proxies WhatsApp API requests to graph.facebook.com | |
| * from Google's infrastructure (script.google.com / script.googleusercontent.com). | |
| * | |
| * HuggingFace Spaces can connect to Google domains but blocks | |
| * graph.facebook.com and workers.dev, so this acts as a bridge. | |
| * | |
| * DEPLOYMENT INSTRUCTIONS: | |
| * 1. Go to https://script.google.com | |
| * 2. Create a new project ("Untitled project") | |
| * 3. Replace all code with this file's contents | |
| * 4. Click "Deploy" → "New deployment" | |
| * 5. Type: "Web app" | |
| * 6. Execute as: "Me" | |
| * 7. Who has access: "Anyone" | |
| * 8. Click "Deploy" | |
| * 9. Copy the Web App URL (looks like: https://script.google.com/macros/s/XXXXX/exec) | |
| * 10. Set META_API_PROXY_URL env var on HuggingFace to that URL | |
| */ | |
| function doPost(e) { | |
| try { | |
| var payload = JSON.parse(e.postData.contents); | |
| var phoneNumberId = payload.phone_number_id; | |
| var accessToken = payload.access_token; | |
| var messageData = payload.message_data; | |
| if (!phoneNumberId || !accessToken || !messageData) { | |
| return ContentService.createTextOutput(JSON.stringify({ | |
| error: "Missing required fields: phone_number_id, access_token, message_data" | |
| })).setMimeType(ContentService.MimeType.JSON); | |
| } | |
| // Forward to Meta Graph API | |
| var url = "https://graph.facebook.com/v21.0/" + phoneNumberId + "/messages"; | |
| var options = { | |
| method: "post", | |
| contentType: "application/json", | |
| headers: { | |
| "Authorization": "Bearer " + accessToken | |
| }, | |
| payload: JSON.stringify(messageData), | |
| muteHttpExceptions: true | |
| }; | |
| var response = UrlFetchApp.fetch(url, options); | |
| var responseCode = response.getResponseCode(); | |
| var responseBody = response.getContentText(); | |
| return ContentService.createTextOutput(JSON.stringify({ | |
| status: responseCode, | |
| body: JSON.parse(responseBody) | |
| })).setMimeType(ContentService.MimeType.JSON); | |
| } catch (err) { | |
| return ContentService.createTextOutput(JSON.stringify({ | |
| error: err.toString() | |
| })).setMimeType(ContentService.MimeType.JSON); | |
| } | |
| } | |
| function doGet(e) { | |
| return ContentService.createTextOutput(JSON.stringify({ | |
| status: "ok", | |
| service: "WhatsApp API Proxy", | |
| description: "POST to this URL with {phone_number_id, access_token, message_data}" | |
| })).setMimeType(ContentService.MimeType.JSON); | |
| } | |