Ruby

ルート作成

に3つのHTTPルートを追加する。 server.rb ファイル、2つの GET ルートと POST のルートだ。

最初の GET ルートにアクセスすると、新しい電話が始まる。2番目の GET ルートは挨拶で受信者を歓迎する。その POST ルートは、通話に応答すると、ストリーミングオーディオの指示を Voice API に送信します:

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