This application demonstrates how to use Node.js, Twilio Voice and ConversationRelay, and the Open AI API to create a voice assistant that can engage in two-way conversations over a phone call. Other branches in this repository demonstrate how to add more advanced features such as streaming, interruption handling, and tool/function calling.
To use the app, you will need:
- Node.js 23.9.0: Download from here. Other versions may work, but I tested with this one.
- A Twilio Account: Sign up for a free trial here.
- A Twilio Number with Voice Capabilities: Instructions to purchase a number.
- An Open AI Account and API Key: Visit Open AI's platform here for more information.
You'll need to expose your local server to the internet for Twilio to access it. Use ngrok for tunneling:
ngrok http 8080
Copy the Forwarding URL and put it aside; it looks like https://[your-ngrok-subdomain].ngrok.app. You'll need it in a couple places.
Run the following command to install necessary packages:
npm install
Update Your Twilio Phone Number: In the Twilio Console under Phone Numbers, set the Webhook for A call comes in to your ngrok URL followed by /twiml.
Example: https://[your-ngrok-subdomain].ngrok.app/twiml
.
Copy the example environment file to .env
:
cp .env.example .env
Edit the .env file and input your Open AI API key in OPENAI_API_KEY
. Add your ngrok URL in NGROK_URL
(do not include the scheme, "http://" or "https://")
Start the development server:
node server.js
Call your Twilio phone number. After connection, you should be able to converse with the Open AI-powered AI Assistant, integrated over ConversationRelay with Twilio Voice!
Note
Customize the initial greeting and response behavior by modifying the aiResponse function and constants like SYSTEM_PROMPT in index.js. Ensure that you update ngrok URLs each time you restart ngrok, as they change with each session.