Last active
February 26, 2019 08:52
-
-
Save Demabio/1815411310f7a72d5d668ec678498c22 to your computer and use it in GitHub Desktop.
Voice App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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","")); | |
} | |
} | |
} | |
} | |
} |
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>";
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
Please make the change as I have stated it... That is where your error is