|
| 1 | +# Sending Targeted Messages with the Broadcast API |
| 2 | + |
| 3 | +The Broadcast API can send a message to one user, not only to every user in an integration. |
| 4 | + |
| 5 | +`integration_id` selects the bot integration. `filters` narrows the recipient list inside that integration. |
| 6 | + |
| 7 | +caution If you omit `filters`, the message is sent to all conversations/users for that integration. Always include a phone-number filter for one-user sends. |
| 8 | + |
| 9 | +If the phone number does not match an existing conversation in that integration, the API returns a `404` and does not send the message. |
| 10 | + |
| 11 | +### Example Request |
| 12 | + |
| 13 | +```http |
| 14 | +POST https://api.gooey.ai/v2/agent/broadcast/send/ |
| 15 | +Authorization: Bearer <GOOEY_API_KEY> |
| 16 | +Content-Type: application/json |
| 17 | +``` |
| 18 | + |
| 19 | +```json |
| 20 | +{ |
| 21 | + "text": "Welcome! I'm your assistant.", |
| 22 | + "integration_id": "your-integration-id", |
| 23 | + "filters": { |
| 24 | + "wa_phone_number__in": ["+91XXXXXXXXXX"] |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +Response: |
| 30 | + |
| 31 | +```json |
| 32 | +{ |
| 33 | + "status": "success", |
| 34 | + "count": 1 |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Use E.164 phone format, for example `+919876543210`. |
| 39 | + |
| 40 | +### Sending Files |
| 41 | + |
| 42 | +The same endpoint accepts file URLs through `audio`, `video`, and `documents`. |
| 43 | + |
| 44 | +```json |
| 45 | +{ |
| 46 | + "text": "Your report is ready.", |
| 47 | + "integration_id": "your-integration-id", |
| 48 | + "documents": ["https://example.com/report.pdf"], |
| 49 | + "filters": { |
| 50 | + "wa_phone_number__in": ["+91XXXXXXXXXX"] |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +This is useful when a backend job or Gooey Function generates a PDF, audio file, video, or other downloadable asset. Once the file URL is available, call the Broadcast API and scope the send to the user with `filters`. |
| 56 | + |
| 57 | +### Using a Gooey Function |
| 58 | + |
| 59 | +If an agent needs to trigger the message, create a Gooey Function that calls `/agent/broadcast/send/`. The agent can pass the `integration_id`, target phone number, message text, and optional file URL to the function. |
| 60 | + |
| 61 | +```js |
| 62 | +async ({ integration_id, phone_number, text, file_url }) => { |
| 63 | + const phoneNumber = phone_number.replace(/\s/g, ""); |
| 64 | + |
| 65 | + const payload = { |
| 66 | + text, |
| 67 | + integration_id, |
| 68 | + filters: { |
| 69 | + // For Twilio-backed SMS/MMS, use twilio_phone_number__in instead. |
| 70 | + wa_phone_number__in: [phoneNumber] |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + if (file_url) { |
| 75 | + payload.documents = [file_url]; |
| 76 | + } |
| 77 | + |
| 78 | + const res = await fetch("https://api.gooey.ai/v2/agent/broadcast/send/", { |
| 79 | + method: "POST", |
| 80 | + headers: { |
| 81 | + "Content-Type": "application/json", |
| 82 | + "Authorization": `Bearer ${process.env.GOOEY_API_KEY}` |
| 83 | + }, |
| 84 | + body: JSON.stringify(payload) |
| 85 | + }); |
| 86 | + |
| 87 | + if (!res.ok) { |
| 88 | + throw new Error(await res.text()); |
| 89 | + } |
| 90 | + |
| 91 | + return await res.json(); |
| 92 | +}; |
| 93 | +``` |
| 94 | + |
| 95 | +For audio or video files, use `audio: file_url` or `video: file_url` instead of `documents`. |
| 96 | + |
| 97 | +Use the filter key that matches your integration type. |
| 98 | + |
| 99 | +### WhatsApp 24-Hour Window |
| 100 | + |
| 101 | +WhatsApp only allows outbound messages within 24 hours of the user's last message to your integration. If a broadcast does not arrive, check whether the recipient has messaged the integration in the last 24 hours. If not, ask them to send a message first, then retry. |
0 commit comments