Ruby
Create Routes
We will add three HTTP routes to the server.rb file, two GET routes and one POST route.
The first GET route will start a new phone call after it is accessed. The second GET route will welcome the recipient with a greeting. The POST route will send the streaming audio instructions to the Voice API once the call has been answered:
get '/new' do
response = client.voice.create(
to: [{ type: 'phone', number: ENV['VONAGE_NUMBER'] }],
from: { type: 'phone', number: ENV['TO_NUMBER'] },
answer_url: ["#{BASE_URL}/answer"],
event_url: ["#{BASE_URL}/event"]
)
puts response.inspect
end
get '/answer' do
content_type :json
[
{
:action => 'stream',
:streamUrl => ['https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/assets/welcome_to_nexmo.mp3'],
:loop => 0
}
].to_json
end
post '/event' do
data = JSON.parse(request.body.read)
response = client.voice.stream.start(data['uuid'], stream_url: [AUDIO_URL]) if data['status'] == 'answered'
puts response.inspect
end
Play Audio into a Call with Ruby
A tutorial showing you how to build an app that will play audio into a PSTN call
Steps
1
Introduction to this tutorial2
Prerequisites3
Create the Project Folder and Files4
Install Vonage Ruby5
Require Dependencies6
Initialize Vonage Client7
Add Environment Variables8
Define Constant Variables9
Create Routes10
Run the Ruby App11
What's Next?