|
| 1 | +# Forward Call |
| 2 | + |
| 3 | +Demonstrates how to forward an incoming call to a specified phone number using Twilio Serverless Functions and TwiML. |
| 4 | + |
| 5 | +## Environment Variables |
| 6 | + |
| 7 | +`.env` is included with placeholder values. Update it before running. Never commit `.env`. |
| 8 | + |
| 9 | +| Variable | Where to find | Format | |
| 10 | +| -------- | ------------- | ------ | |
| 11 | +| `MY_PHONE_NUMBER` | Phone number you control — the destination for forwarded calls | E.164 format: `+15551234567` | |
| 12 | +| `TWILIO_VOICE_WEBHOOK_URL` | Pre-set to `/forward-call` — do not change | `/forward-call` | |
| 13 | + |
| 14 | +## Commands |
| 15 | + |
| 16 | +```bash |
| 17 | +# Install Twilio CLI serverless plugin (once) |
| 18 | +twilio plugins:install @twilio-labs/plugin-serverless |
| 19 | + |
| 20 | +# Initialize a new project from this template |
| 21 | +# --skip-credentials skips the credential prompt (Serverless injects them at runtime) |
| 22 | +twilio serverless:init example --template=forward-call --skip-credentials && cd example |
| 23 | +# Edit .env — replace placeholder MY_PHONE_NUMBER with your real E.164 number |
| 24 | + |
| 25 | +# Start the server |
| 26 | +twilio serverless:start |
| 27 | + |
| 28 | +# Expose locally via ngrok (run separately — install and authenticate at https://ngrok.com) |
| 29 | +ngrok http 3000 |
| 30 | + |
| 31 | +# Configure the voice webhook on your Twilio number |
| 32 | +# Find your phone number SID: |
| 33 | +twilio api:core:incoming-phone-numbers:list |
| 34 | +# Set the webhook (replace URL with your ngrok URL or deployed URL): |
| 35 | +twilio api:core:incoming-phone-numbers:update \ |
| 36 | + --sid <your-phone-number-sid> \ |
| 37 | + --voice-url https://<your-ngrok-id>.ngrok.app/forward-call \ |
| 38 | + --voice-method POST |
| 39 | + |
| 40 | +# Deploy |
| 41 | +twilio serverless:deploy --service-name forward-call |
| 42 | +``` |
| 43 | + |
| 44 | +## Project Structure |
| 45 | + |
| 46 | +- `functions/forward-call.protected.js` — main handler; dials `MY_PHONE_NUMBER` via TwiML |
| 47 | +- `tests/forward-call.test.js` — Jest unit tests (run from the monorepo root, not this directory) |
| 48 | +- `.env` — environment variables (never commit) |
| 49 | + |
| 50 | +## Agent Boundaries |
| 51 | + |
| 52 | +**Always:** |
| 53 | + |
| 54 | +- Confirm `.env` is configured before running any command |
| 55 | +- Use the Environment Variables section to guide the user to each credential — don't ask them to find values without direction |
| 56 | +- Confirm the app is running before asking the user to test it |
| 57 | +- Always use `--template=forward-call` when initializing a new project |
| 58 | + |
| 59 | +**Never:** |
| 60 | + |
| 61 | +- Run the app with missing or placeholder credentials |
| 62 | +- Hardcode credentials or phone numbers in source files |
| 63 | +- Run with placeholder values still in `.env` |
| 64 | + |
| 65 | +## Verify It's Working |
| 66 | + |
| 67 | +1. Call your Twilio phone number |
| 68 | +2. The call should ring through to the number set in `MY_PHONE_NUMBER` |
| 69 | + |
| 70 | +## Twilio Resources |
| 71 | + |
| 72 | +- [Twilio Console](https://console.twilio.com) — credentials, phone numbers, webhook configuration |
| 73 | +- [Twilio Serverless Toolkit](https://www.twilio.com/docs/labs/serverless-toolkit) — deploy and manage Serverless Functions |
| 74 | +- [TwiML for Voice](https://www.twilio.com/docs/voice/twiml) — reference for the VoiceResponse used in this template |
| 75 | +- [Programmable Voice](https://www.twilio.com/docs/voice) — Voice API reference |
0 commit comments