This service enables outbound voice calls using Twilio and Voiceflow Voice feature, with advanced call handling features including answering machine detection, call status tracking, and seamless integration with Voiceflow's conversational AI.
- Initiate outbound voice calls via a simple API endpoint
- Automatic answering machine detection
- Real-time call status tracking
- Integration with Voiceflow for conversational AI
- Detailed call status reporting and event logging
- Node.js
- Twilio account with Account SID and Auth Token
- Voiceflow account with API key and webhook ID
- Environment variables properly configured
Create a .env
file with the following variables:
TWILIO_ACCOUNT_SID=your_twilio_account_sid
TWILIO_AUTH_TOKEN=your_twilio_auth_token
VOICEFLOW_API_KEY=your_voiceflow_api_key
VOICEFLOW_WEBHOOK_ID=your_voiceflow_webhook_id
SERVER_URL=your_server_url
PORT=4242 # Optional, defaults to 4242
Note: The Voiceflow API key and webhook ID can be found in your Twilio Phone Numbers section. Select your phone number and look for the webhook URL:
https://runtime-api.voiceflow.com/v1/twilio/webhooks/{VOICEFLOW_WEBHOOK_ID}/answer?authorization={VOICEFLOW_API_KEY}
npm install
npm start
To allow Twilio to reach your local service, you'll need to expose it using a secure tunnel. ngrok is recommended for this purpose.
-
Install ngrok from https://ngrok.com/download
-
Start ngrok tunnel to your local server:
ngrok http ${PORT:-4242}
-
Copy the HTTPS URL provided by ngrok (e.g.,
https://your-tunnel.ngrok-free.app
) -
Update your
.env
file with the ngrok URL:
SERVER_URL=https://your-tunnel.ngrok-free.app
Note: The ngrok URL changes each time you restart ngrok unless you have a paid account. Make sure to update your
SERVER_URL
environment variable whenever the URL changes.
You can also run this service using Docker. Make sure you have Docker and Docker Compose installed on your system.
- Make sure your
.env
file is properly configured - Build and start the container:
docker-compose up -d
To stop the service:
docker-compose down
- Build the Docker image:
docker build -t voiceflow-outbound .
- Run the container:
docker run -d \
--env-file .env \
-p ${PORT:-4242}:${PORT:-4242} \
--name voiceflow-outbound \
voiceflow-outbound
To stop the container:
docker stop voiceflow-outbound
docker rm voiceflow-outbound
GET /call?to={phoneNumber}&from={twilioNumber}
Parameters:
to
: Destination phone number (E.164 format)from
: Your Twilio phone number (E.164 format)
Example:
curl "http://localhost:4242/call?to=14155551234&from=14155557890"
Response:
{
"message": "Call initiated successfully",
"callSid": "CA123...",
"to": "+14155551234",
"from": "+14155557890",
"status": "queued",
"statusUrl": "http://localhost:4242/status/CA123..."
}
GET /status/{callId}
Example:
curl "http://localhost:4242/status/CA123..."
Response:
{
"callSid": "CA123...",
"status": "completed",
"lastUpdated": "2024-01-20T12:00:00.000Z",
"events": [
{
"status": "initiated",
"timestamp": "2024-01-20T11:59:30.000Z"
},
{
"status": "completed",
"timestamp": "2024-01-20T12:00:00.000Z",
"duration": "30",
"answeredBy": "human"
}
]
}
The service tracks various call statuses:
initiated
: Call has been initiatedringing
: Phone is ringingin-progress
: Call is connectedcompleted
: Call completed successfullydeclined
: Call was declined or not answeredmachine
: Call was answered by voicemailerror
: An error occurred during the call
The service includes comprehensive error handling for various scenarios:
- Invalid phone numbers
- Network issues
- Twilio API errors
- Voiceflow integration issues
- All sensitive credentials should be stored in environment variables
- The service validates phone numbers before making calls
- Call status information is temporarily stored and automatically cleaned up
MIT