Skip to content

Commit

Permalink
Implement voice call and TTS
Browse files Browse the repository at this point in the history
  • Loading branch information
smithrobs committed Mar 19, 2016
1 parent aaa3878 commit e5fbb05
Show file tree
Hide file tree
Showing 16 changed files with 726 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<a name="1.0.0"></a>
# 1.0.0 (2016-03-19)

* Initial release with nuget package
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<ItemGroup>
<Compile Include="ShortCodeTest.cs" />
<Compile Include="SearchTest.cs" />
<Compile Include="VoiceTest.cs" />
<Compile Include="SmsTest.cs" />
<Compile Include="NumberTest.cs" />
<Compile Include="AccountTest.cs" />
Expand Down
75 changes: 75 additions & 0 deletions Nexmo.Api.Test.Integration/VoiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using NUnit.Framework;

namespace Nexmo.Api.Test.Integration
{
[TestFixture]
public class VoiceTest
{
[Test]
public void should_call_voice()
{
var result = Voice.Call(new Voice.CallCommand
{
to = "17775551212",
answer_url = "https://abcdefgh.ngrok.io/content/voiceDemo.xml",
status_url = "https://abcdefgh.ngrok.io/api/voice",
from = "15555551212",
});
Assert.IsNotEmpty(result.CallId);
Assert.AreEqual("0", result.status);
Assert.AreEqual("17775551212", result.to);
}

[Test]
public void should_text_to_speech_call()
{
var result = Voice.TextToSpeech(new Voice.TextToSpeechCallCommand
{
to = "17775551212",
from = "15555551212",
text = "1, 2, 3 wait <break time=\"1s\"/>5 minutes <prosody rate=\"-25%\">humanoid</prosody>",
callback = "https://abcdefgh.ngrok.io/api/tts"
});

Assert.IsNotEmpty(result.call_id);
Assert.AreEqual("0", result.status);
Assert.AreEqual("17775551212", result.to);
}

[Test]
public void should_text_to_speech_capture()
{
var result = Voice.TextToSpeechPrompt(new Voice.TextToSpeechPromptCaptureCommand
{
to = "17775551212",
from = "15555551212",
text = "Enter some digits please",
bye_text = "Thanks",
callback = "https://abcdefgh.ngrok.io/api/tts"
});

Assert.IsNotEmpty(result.call_id);
Assert.AreEqual("0", result.status);
Assert.AreEqual("17775551212", result.to);
}

[Test]
public void should_text_to_speech_confirm()
{
var result = Voice.TextToSpeechPrompt(new Voice.TextToSpeechPromptConfirmCommand
{
to = "17775551212",
text = "Please enter your 6 digit pin",
max_digits = "6",
pin_code = "123457",
bye_text = "Correct!",
failed_text = "Failed, ",
callback = "https://abcdefgh.ngrok.io/api/tts"
});

Assert.IsNotEmpty(result.call_id);
Assert.AreEqual("0", result.status);
Assert.AreEqual("17775551212", result.to);
}
}
}
1 change: 1 addition & 0 deletions Nexmo.Api.Test.Unit/MockedWebTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal class MockedWebTest
protected Mock<IHttpWebRequestFactory> _mock;
protected Mock<IHttpWebRequest> _request;

protected string ApiUrl = ConfigurationManager.AppSettings["Nexmo.Url.Api"];
protected string RestUrl = ConfigurationManager.AppSettings["Nexmo.Url.Rest"];
protected string ApiKey = ConfigurationManager.AppSettings["Nexmo.api_key"];
protected string ApiSecret = ConfigurationManager.AppSettings["Nexmo.api_secret"];
Expand Down
1 change: 1 addition & 0 deletions Nexmo.Api.Test.Unit/Nexmo.Api.Test.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Compile Include="ShortCodeTest.cs" />
<Compile Include="MockedWebTest.cs" />
<Compile Include="SearchTest.cs" />
<Compile Include="VoiceTest.cs" />
<Compile Include="SmsTest.cs" />
<Compile Include="NumberTest.cs" />
<Compile Include="AccountTest.cs" />
Expand Down
135 changes: 135 additions & 0 deletions Nexmo.Api.Test.Unit/VoiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using System;
using System.IO;
using System.Text;
using Moq;
using Nexmo.Api.Request;
using NUnit.Framework;

namespace Nexmo.Api.Test.Unit
{
[TestFixture]
internal class VoiceTest : MockedWebTest
{
[Test]
public void should_call_voice()
{
var resp = new Mock<IWebResponse>();
resp.Setup(e => e.GetResponseStream()).Returns(new MemoryStream(Encoding.UTF8.GetBytes("{\"call-id\":\"ffffffff36c53a6c37ba7bcfada6ffff-1\",\"to\":\"17775551212\",\"status\":\"0\"}")));
var postDataStream = new MemoryStream();
_request.Setup(e => e.GetRequestStream()).Returns(postDataStream);
_request.Setup(e => e.GetResponse()).Returns(resp.Object);
var result = Voice.Call(new Voice.CallCommand
{
to = "17775551212",
answer_url = "https://test.test.com/content/voiceDemo.xml",
from = "15555551212",
});

_mock.Verify(h => h.CreateHttp(new Uri(
string.Format("{0}/call/json", RestUrl))),
Times.Once);
postDataStream.Position = 0;
var sr = new StreamReader(postDataStream);
var postData = sr.ReadToEnd();
Assert.AreEqual(string.Format("to=17775551212&answer_url=https%3a%2f%2ftest.test.com%2fcontent%2fvoiceDemo.xml&from=15555551212&api_key={0}&api_secret={1}&", ApiKey, ApiSecret), postData);

Assert.AreEqual("ffffffff36c53a6c37ba7bcfada6ffff-1", result.CallId);
Assert.AreEqual("17775551212", result.to);
Assert.AreEqual("0", result.status);
}

[Test]
public void should_text_to_speech()
{
var resp = new Mock<IWebResponse>();
resp.Setup(e => e.GetResponseStream()).Returns(new MemoryStream(Encoding.UTF8.GetBytes("{\"call_id\":\"ffffffffc839189f36354f94b878ffff\",\"to\":\"17775551212\",\"status\":\"0\",\"error_text\":\"Success\"}")));
var postDataStream = new MemoryStream();
_request.Setup(e => e.GetRequestStream()).Returns(postDataStream);
_request.Setup(e => e.GetResponse()).Returns(resp.Object);
var result = Voice.TextToSpeech(new Voice.TextToSpeechCallCommand
{
to = "17775551212",
from = "15555551212",
text = "1, 2, 3 wait <break time=\"1s\"/>5 minutes <prosody rate=\"-25%\">humanoid</prosody>",
callback = "https://test.test.com/api/tts"
});

_mock.Verify(h => h.CreateHttp(new Uri(
string.Format("{0}/tts/json", ApiUrl))),
Times.Once);
postDataStream.Position = 0;
var sr = new StreamReader(postDataStream);
var postData = sr.ReadToEnd();
Assert.AreEqual(string.Format("to=17775551212&from=15555551212&text=1%2c+2%2c+3+wait+%3cbreak+time%3d%221s%22%2f%3e5+minutes+%3cprosody+rate%3d%22-25%25%22%3ehumanoid%3c%2fprosody%3e&callback=https%3a%2f%2ftest.test.com%2fapi%2ftts&api_key={0}&api_secret={1}&", ApiKey, ApiSecret), postData);

Assert.AreEqual("ffffffffc839189f36354f94b878ffff", result.call_id);
Assert.AreEqual("17775551212", result.to);
Assert.AreEqual("0", result.status);
Assert.AreEqual("Success", result.error_text);
}

[Test]
public void should_text_to_speech_capture()
{
var resp = new Mock<IWebResponse>();
resp.Setup(e => e.GetResponseStream()).Returns(new MemoryStream(Encoding.UTF8.GetBytes("{\"call_id\":\"ffffffffc839189f36354f94b878ffff\",\"to\":\"17775551212\",\"status\":\"0\",\"error_text\":\"Success\"}")));
var postDataStream = new MemoryStream();
_request.Setup(e => e.GetRequestStream()).Returns(postDataStream);
_request.Setup(e => e.GetResponse()).Returns(resp.Object);
var result = Voice.TextToSpeechPrompt(new Voice.TextToSpeechPromptCaptureCommand
{
to = "17775551212",
from = "15555551212",
text = "Enter some digits please",
bye_text = "Thanks",
callback = "https://test.test.com/api/tts"
});

_mock.Verify(h => h.CreateHttp(new Uri(
string.Format("{0}/tts-prompt/json", ApiUrl))),
Times.Once);
postDataStream.Position = 0;
var sr = new StreamReader(postDataStream);
var postData = sr.ReadToEnd();
Assert.AreEqual(string.Format("from=15555551212&to=17775551212&text=Enter+some+digits+please&callback=https%3a%2f%2ftest.test.com%2fapi%2ftts&bye_text=Thanks&api_key={0}&api_secret={1}&", ApiKey, ApiSecret), postData);

Assert.AreEqual("ffffffffc839189f36354f94b878ffff", result.call_id);
Assert.AreEqual("17775551212", result.to);
Assert.AreEqual("0", result.status);
Assert.AreEqual("Success", result.error_text);
}

[Test]
public void should_text_to_speech_confirm()
{
var resp = new Mock<IWebResponse>();
resp.Setup(e => e.GetResponseStream()).Returns(new MemoryStream(Encoding.UTF8.GetBytes("{\"call_id\":\"ffffffffc839189f36354f94b878ffff\",\"to\":\"17775551212\",\"status\":\"0\",\"error_text\":\"Success\"}")));
var postDataStream = new MemoryStream();
_request.Setup(e => e.GetRequestStream()).Returns(postDataStream);
_request.Setup(e => e.GetResponse()).Returns(resp.Object);
var result = Voice.TextToSpeechPrompt(new Voice.TextToSpeechPromptConfirmCommand
{
to = "17775551212",
text = "Please enter your 6 digit pin",
max_digits = "6",
pin_code = "123457",
bye_text = "Correct!",
failed_text = "Failed, ",
callback = "https://test.test.com/api/tts"
});

_mock.Verify(h => h.CreateHttp(new Uri(
string.Format("{0}/tts-prompt/json", ApiUrl))),
Times.Once);
postDataStream.Position = 0;
var sr = new StreamReader(postDataStream);
var postData = sr.ReadToEnd();
Assert.AreEqual(string.Format("pin_code=123457&failed_text=Failed%2c+&to=17775551212&text=Please+enter+your+6+digit+pin&callback=https%3a%2f%2ftest.test.com%2fapi%2ftts&max_digits=6&bye_text=Correct!&api_key={0}&api_secret={1}&", ApiKey, ApiSecret), postData);

Assert.AreEqual("ffffffffc839189f36354f94b878ffff", result.call_id);
Assert.AreEqual("17775551212", result.to);
Assert.AreEqual("0", result.status);
Assert.AreEqual("Success", result.error_text);
}
}
}
1 change: 1 addition & 0 deletions Nexmo.Api.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nexmo.Web.Sample", "Nexmo.W
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{755C0EA2-03BC-4446-B933-550BEB1EFEF5}"
ProjectSection(SolutionItems) = preProject
CHANGELOG.md = CHANGELOG.md
LICENSE.md = LICENSE.md
README.md = README.md
EndProjectSection
Expand Down
Loading

0 comments on commit e5fbb05

Please sign in to comment.