Skip to content

Commit

Permalink
Implement remainder of voice calls
Browse files Browse the repository at this point in the history
  • Loading branch information
smithrobs committed Oct 7, 2016
1 parent 3485e73 commit 287747b
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 20 deletions.
91 changes: 83 additions & 8 deletions Nexmo.Api.Test.Integration/CallTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Configuration;
using System.Net;
using Nexmo.Api.Voice;
using NUnit.Framework;

Expand All @@ -11,13 +10,12 @@ public class CallTest
[Test]
public void should_call()
{
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

var results = Call.Do(new Call.CallCommand
{
to = new[]
{
new Call.Endpoint {
new Call.Endpoint
{
type = "phone",
number = ConfigurationManager.AppSettings["test_number"]
}
Expand All @@ -38,8 +36,6 @@ public void should_call()
[Test]
public void should_get_calls()
{
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

var results = Call.List();
Assert.AreEqual(3, results._embedded.calls.Count);
}
Expand All @@ -55,14 +51,93 @@ public void should_get_specified_call()
[Test]
public void should_edit_specified_call()
{
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

var id = "ffffffff-ffff-ffff-ffff-ffffffffffff";
var results = Call.Edit(id, new Call.CallEditCommand
{
action = "hangup"
});
Assert.AreEqual(id, results.uuid);
}

[Test]
public void should_call_then_stream_then_end_stream()
{
var call = Call.Do(new Call.CallCommand
{
to = new[]
{
new Call.Endpoint
{
type = "phone",
number = ConfigurationManager.AppSettings["test_number"]
}
},
from = new Call.Endpoint
{
type = "phone",
number = ConfigurationManager.AppSettings["nexmo_number"]
},
answer_url = new[]
{
"https://nexmo-community.github.io/ncco-examples/first_call_talk.json"
}
});
Assert.AreEqual("started", call.status);

// need to look up the call id based on the conversation id
var calls = Call.List(new Call.SearchFilter
{
conversation_uuid = call.conversation_uuid,
page_size = 10
});
var callId = calls._embedded.calls[0].uuid;

// test streaming
var streamCmdResponse = Call.BeginStream(callId, new Call.StreamCommand
{
stream_url = new[]
{
"https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3"
},
loop = 0
});
Assert.AreEqual("Stream started", streamCmdResponse.message);

System.Threading.Thread.Sleep(1000);

streamCmdResponse = Call.EndStream(callId);
Assert.AreEqual("Stream stopped", streamCmdResponse.message);
}

[Test]
public void should_call_then_send_dtmf()
{
var callId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
// TODO: need to look up the call id based on the conversation id
var dtmfResponse = Call.SendDtmf(callId, new Call.DtmfCommand
{
digits = "24681357"
});
Assert.AreEqual("DTMF sent", dtmfResponse.message);
}

[Test]
public void should_call_then_send_and_end_talk()
{
var callId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
// TODO: need to look up the call id based on the conversation id
var talkResponse = Call.BeginTalk(callId, new Call.TalkCommand
{
text = "Four score and seven years ago our fathers brought forth, on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle - field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate — we can not consecrate — we can not hallow — this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us — that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion — that we here highly resolve that these dead shall not have died in vain — that this nation, under God, shall have a new birth of freedom — and that government of the people, by the people, for the people, shall not perish from the earth.",
voice_name = "Salli",
loop = 1
});
Assert.AreEqual("Talk started", talkResponse.message);

System.Threading.Thread.Sleep(1000);

talkResponse = Call.EndTalk(callId);
Assert.AreEqual("Talk stopped", talkResponse.message);
}
}
}
27 changes: 22 additions & 5 deletions Nexmo.Api/Voice/Call.Stream.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
using System;
using Newtonsoft.Json;
using Nexmo.Api.Request;

namespace Nexmo.Api.Voice
{
public static partial class Call
{
public class StreamCommand
{
/// <summary>
/// An array of URLs pointing to the .mp3 or .wav audio file to stream.
/// </summary>
public string[] stream_url { get; set; }
/// <summary>
/// Set to 0 to replay the audio file at stream_url when the stream ends.
/// </summary>
public int loop { get; set; }
}

/// <summary>
/// PUT /v1/calls/{uuid}/stream - stream an audio file to an active Call
/// </summary>
public static CallResponse BeginStream()
public static CallCommandResponse BeginStream(string id, StreamCommand cmd)
{
throw new NotImplementedException();
var response = VersionedApiRequest.DoRequest("PUT", ApiRequest.GetBaseUriFor(typeof(Call), $"/v1/calls/{id}/stream"), cmd);

return JsonConvert.DeserializeObject<CallCommandResponse>(response.JsonResponse);
}

/// <summary>
/// DELETE /v1/calls/{uuid}/stream - stop streaming an audio file to an active Call
/// </summary>
public static CallResponse EndStream()
public static CallCommandResponse EndStream(string id)
{
throw new NotImplementedException();
var response = VersionedApiRequest.DoRequest("DELETE", ApiRequest.GetBaseUriFor(typeof(Call), $"/v1/calls/{id}/stream"), new {});

return JsonConvert.DeserializeObject<CallCommandResponse>(response.JsonResponse);
}
}
}
51 changes: 44 additions & 7 deletions Nexmo.Api/Voice/Call.Talk.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,68 @@
using System;
using Newtonsoft.Json;
using Nexmo.Api.Request;

namespace Nexmo.Api.Voice
{
public static partial class Call
{
public class TalkCommand
{
/// <summary>
/// A UTF-8 and URL encoded string of up to 1500 characters containing
/// the message to be synthesized in the Call or Conversation. Each
/// comma in text adds a short pause to the synthesized speech.
/// </summary>
public string text { get; set; }
/// <summary>
/// The name of the voice used to deliver text. You use the voice_name
/// that has the correct language, gender and accent for the message
/// you are sending. For example, the default voice kimberley is a
/// female who speaks English with an American accent (en-US).
/// Possible values for voice_name are listed at https://docs.nexmo.com/voice/voice-api/api-reference#talk_put
/// </summary>
public string voice_name { get; set; }
/// <summary>
/// Set to 0 to replay the audio file at stream_url when the stream ends. The default value is 1.
/// </summary>
public int loop { get; set; }
}

public class DtmfCommand
{
/// <summary>
/// The array of digits to send to the Call
/// </summary>
public string digits { get; set; }
}

/// <summary>
/// PUT /v1/calls/{uuid}/talk - send a synthesized speech message to an active Call
/// </summary>
public static CallResponse BeginTalk()
public static CallCommandResponse BeginTalk(string id, TalkCommand cmd)
{
throw new NotImplementedException();
var response = VersionedApiRequest.DoRequest("PUT", ApiRequest.GetBaseUriFor(typeof(Call), $"/v1/calls/{id}/talk"), cmd);

return JsonConvert.DeserializeObject<CallCommandResponse>(response.JsonResponse);
}

/// <summary>
/// DELETE /v1/calls/{uuid}/talk - stop sending a synthesized speech message to an active Call
/// </summary>
public static CallResponse EndTalk()
public static CallCommandResponse EndTalk(string id)
{
throw new NotImplementedException();
var response = VersionedApiRequest.DoRequest("DELETE", ApiRequest.GetBaseUriFor(typeof(Call), $"/v1/calls/{id}/talk"), new {});

return JsonConvert.DeserializeObject<CallCommandResponse>(response.JsonResponse);
}

/// <summary>
/// PUT /v1/calls/{uuid}/dtmf - send Dual-tone multi-frequency(DTMF) tones to an active Call
/// </summary>
public static CallResponse SendDtmf()
public static CallCommandResponse SendDtmf(string id, DtmfCommand cmd)
{
throw new NotImplementedException();
var response = VersionedApiRequest.DoRequest("PUT", ApiRequest.GetBaseUriFor(typeof(Call), $"/v1/calls/{id}/dtmf"), cmd);

return JsonConvert.DeserializeObject<CallCommandResponse>(response.JsonResponse);
}
}
}
13 changes: 13 additions & 0 deletions Nexmo.Api/Voice/Call.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ public class CallCommand
public Endpoint from { get; set; }
public string[] answer_url { get; set; }
}

public class CallCommandResponse
{
/// <summary>
/// A string explaining the state of this request.
/// </summary>
public string message { get; set; }
/// <summary>
/// The unique id for this request.
/// </summary>
public string uuid { get; set; }
}

public class CallEditCommand
{
/// <summary>
Expand Down

0 comments on commit 287747b

Please sign in to comment.