Skip to content

Instantly share code, notes, and snippets.

@Demabio
Last active February 26, 2019 08:52
Show Gist options
  • Select an option

  • Save Demabio/1815411310f7a72d5d668ec678498c22 to your computer and use it in GitHub Desktop.

Select an option

Save Demabio/1815411310f7a72d5d668ec678498c22 to your computer and use it in GitHub Desktop.
Voice App
using Microsoft.AspNetCore.Mvc;
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using System.IO;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Voice.Controllers
{
[Route("service")]
public class VoiceApplicationController : ApiController
{
private readonly string xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
private readonly string appUrl = "https://finsol-services.com/Try";
[Route("voice")] // https://www.mydomain.com/service/voice // http:1.2.3.4:8008/service/voice
[HttpPost]
public HttpResponseMessage appHttpResponseMessage([FromForm] VoiceResponse voiceResponse)
{
HttpResponseMessage responseMessage = new HttpResponseMessage();
// The Default action here is to <Redirect> smartly...
// Redirect to outbound handler or the call is outbound
// Redirect to inbound hander if the call is inbound
// The value is always derived from the call direction parameter
//string appUrl = hostNameResolver();
//string appHostname = "https://cdb5482a.ngrok.io";
// This is a very dangerous hack strive to do something cleaner
// // You can save the session ID as well
//string sessionId = voiceResponse.sessionId;
string defaultVoiceAction = "";
if (voiceResponse.isActive == "1")
{
switch (voiceResponse.direction)
{
case "Inbound":
//defaultVoiceAction = $"{xmlHeader}<Response><Redirect>{appHostname}/service/inbound</Redirect></Response>";
defaultVoiceAction = $"{xmlHeader}<Response><Redirect>{appUrl}/service/inbound</Redirect></Response>";
break;
case "Outbound":
//defaultVoiceAction = $"{xmlHeader}<Response><Redirect>{appHostname}/service/outbound</Redirect></Response>";
defaultVoiceAction = $"{xmlHeader}<Response><Redirect>{appUrl}/service/outbound</Redirect></Response>";
break;
default:
//defaultVoiceAction = $"{xmlHeader}<Response><Redirect>{appHostname}/service/error</Redirect></Response>";
defaultVoiceAction = $"{xmlHeader}<Response><Redirect>{appUrl}/service/inbound</Redirect></Response>";
break;
}
}
responseMessage = Request.CreateResponse(HttpStatusCode.Created, defaultVoiceAction);
responseMessage.Content = new StringContent(defaultVoiceAction, Encoding.UTF8, "application/xml");
responseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
return responseMessage;
}
[Route("outbound")]
[HttpPost]
public HttpResponseMessage outboundHttpResponseMessage([FromForm] VoiceResponse voiceResponse)
{
HttpResponseMessage responseMessage = new HttpResponseMessage();
string defaultVoiceAction = "";
if (voiceResponse.isActive == "1")
{
defaultVoiceAction = sampleOutboundResponse();
responseMessage = Request.CreateResponse(HttpStatusCode.Created, defaultVoiceAction);
responseMessage.Content = new StringContent(defaultVoiceAction, Encoding.UTF8, "application/xml");
responseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
return responseMessage;
} else {
// do something else, the call is complete
return null;
}
}
[Route("inbound")]
[HttpPost]
public HttpResponseMessage inboundHttpResponseMessage([FromForm] VoiceResponse voiceResponse)
{
HttpResponseMessage responseMessage = new HttpResponseMessage();
string defaultVoiceAction = "";
Console.WriteLine(voiceResponse);
if (voiceResponse.isActive == "1")
{
defaultVoiceAction = sampleInboundAction();
responseMessage = Request.CreateResponse(HttpStatusCode.Created, defaultVoiceAction);
responseMessage.Content = new StringContent(defaultVoiceAction, Encoding.UTF8, "application/xml");
responseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
return responseMessage;
} else {
// do something else, the call is complete
return null;
}
}
[Route("dtmf")]
[HttpPost]
public HttpResponseMessage dtmfHttpResponseMessage([FromForm] DtmfResponse dtmfResponse)
{
HttpResponseMessage responseMessage = new HttpResponseMessage();
string defaultVoiceAction = "";
if (dtmfResponse.dtmfDigits != null)
{
string num = "+254724023851";
defaultVoiceAction = $"{xmlHeader}<Response><Dial phoneNumbers={num} ringbackTone=\"http://197.232.70.193:30003/FTP/EAFF_Voice/IndianaCut.mp3\"/></Response>";
responseMessage = Request.CreateResponse(HttpStatusCode.Created, defaultVoiceAction);
responseMessage.Content = new StringContent(defaultVoiceAction, Encoding.UTF8, "application/xml");
responseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
return responseMessage;
} else {
// do something else, the call is complete
return null;
}
}
[Route("events")]
[HttpPost]
public HttpResponseMessage dtmfHttpResponseMessage([FromForm] VoiceEvents events)
{
// Log and save stuff to DB
return null;
}
public string sampleOutboundResponse()
{
//which one -this =\"?
//in a browser is is a forward slash-could that be the issue, and wen i removed it throws an exception
string outboundDialAction = $"{xmlHeader}<Response><Play url=\"http://197.232.70.193:30003/FTP/EAFF_Voice/IndianaCut.mp3\"/></Response>";
return outboundDialAction;
}
public string sampleInboundAction()
{
string sayActionPrompt = "<Say voice=\"woman\"> Hi Welcome to eGranary. Press 1 and the hash sign wait as your call is being transferred to the next available agent</Say>";
string sayActionTimeout = "<Say voice=\"woman\"> Am sorry we did not get that. Good bye</Say>";
// string getDigitsAction = $"<GetDigits numDigits=\"4\" finishOnKey=\"#\" callbackUrl=\"{appUrl}/service/dtmf\" timeout=\"30\" >{sayActionPrompt}</GetDigits>";
string getDigitsAction = $"<GetDigits numDigits=\"1\" finishOnKey=\"#\" callbackUrl=\"{appUrl}/service/dtmf\" >{sayActionPrompt}</GetDigits>";
//string getDigitsAction = $"{xmlHeader}<Response><dial phoneNumbers='+254724023851' ringbackTone=\"http://197.232.70.193:30003/FTP/EAFF_Voice/IndianaCut.mp3\"></Response>";
string getDigitsActionRes = $"{xmlHeader}<Response>{getDigitsAction}{sayActionTimeout}</Response>";
return getDigitsActionRes;
}
//public string finalDtmf(string phoneNumber)
//{
// string phoneNum = phoneNumber;
// string dtmfGreeter = $"{xmlHeader}<Response> <Say voice=\"woman\">Hello {phoneNum}, thank you</Say> /></Response>";
// return dtmfGreeter;
//}
//public string queing(string phoneNumber)
//{
// string phoneNum = phoneNumber;
// string dtmfGreeter = $"{xmlHeader}<Response><Enqueue name='test' holdMusic=\"http://197.232.70.193:30003/FTP/EAFF_Voice/IndianaCut.mp3\" /></Response>";
// return dtmfGreeter;
//}
//public string Dequeing(string phoneNumber)
//{
// string phoneNum = phoneNumber;
// string dtmfGreeter = $"{xmlHeader}<Response><Say voice=\"woman\">Hello {phoneNum}, thank you</Say></Response>";
// return dtmfGreeter;
//}
public string hostNameResolver()
{
string uri = "http://ifconfig.me";
var getUrlReq = (HttpWebRequest)WebRequest.Create(uri);
getUrlReq.UserAgent = "curl";
getUrlReq.Method = "GET";
using (WebResponse webResponse = getUrlReq.GetResponse())
{
using (var streamReader = new StreamReader(webResponse.GetResponseStream()))
{
string readerVal = streamReader.ReadToEnd();
return (readerVal.Replace("\n",""));
}
}
}
}
}
@TheBeachMaster
Copy link
Copy Markdown

In this section -> https://gist.github.com/Demabio/1815411310f7a72d5d668ec678498c22#file-testclass-L161
Re-write it to {xmlHeader}<Response><Dial phoneNumbers=\"+254724023851\" ringbackTone=\"http://197.232.70.193:30003/FTP/EAFF_Voice/IndianaCut.mp3\" </Response>

@Demabio
Copy link
Copy Markdown
Author

Demabio commented Feb 26, 2019

am actually refeferncing line L116 not L161
check 116 instead

@TheBeachMaster
Copy link
Copy Markdown

Please make the change as I have stated it... That is where your error is

@TheBeachMaster
Copy link
Copy Markdown

TheBeachMaster commented Feb 26, 2019

On this -> defaultVoiceAction = $"{xmlHeader}<Dial phoneNumbers={num} ringbackTone="http://197.232.70.193:30003/FTP/EAFF_Voice/IndianaCut.mp3\"/>";

Change to defaultVoiceAction = $"{xmlHeader}<Response><Dial phoneNumbers=\"{num}\" ringbackTone=\"http://197.232.70.193:30003/FTP/EAFF_Voice/IndianaCut.mp3\"/></Response>";

@TheBeachMaster
Copy link
Copy Markdown

It should always be surrounded by double quotes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment