Created
January 12, 2017 23:21
-
-
Save devinrader/3d3fe709e759d1b88881ff56ed2eb083 to your computer and use it in GitHub Desktop.
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 System.Net; | |
using System.Text.RegularExpressions; | |
using Twilio.TwiML; | |
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) | |
{ | |
log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}"); | |
var data = await req.Content.ReadAsStringAsync(); | |
data = data.Replace("%3A", ":"); | |
data = data.Replace("%40", "@"); | |
log.Info(data); | |
Dictionary<string,string> formValues = data.Split('&') | |
.Select(value => value.Split('=')) | |
.ToDictionary(pair => pair[0], pair => pair[1]); | |
var to = formValues["To"]; | |
Regex exp_e164_pstn = new Regex(@"^sip:([+][0-9]{10,14})@"); | |
Match match_e164_pstn = exp_e164_pstn.Match(to); | |
Regex exp_011_pstn = new Regex(@"^sip:011([0-9]{10,14})@"); | |
Match match_011_pstn = exp_011_pstn.Match(to); | |
Regex exp_us_pstn = new Regex(@"^sip:[+]?1?([0-9]{10})@"); | |
Match match_us_pstn = exp_us_pstn.Match(to); | |
if (match_e164_pstn.Success) { | |
to = String.Format("{0}", match_e164_pstn.Groups[1]); | |
} else if (match_011_pstn.Success) { | |
to = String.Format("+{0}", match_011_pstn.Groups[1]); | |
} else if (match_us_pstn.Success) { | |
to = String.Format("+1{0}", match_us_pstn.Groups[1]); | |
} | |
log.Info(to); | |
var response = new TwilioResponse(); | |
if (to.StartsWith("sip:")) { | |
response.Dial(new Sip(to)); | |
} else { | |
string caller_id = System.Environment.GetEnvironmentVariable("TWILIO_CALLERID", EnvironmentVariableTarget.Process); | |
response.Dial(to, new { callerId=caller_id }); | |
} | |
return new HttpResponseMessage | |
{ | |
Content = new StringContent(response.ToString(), System.Text.Encoding.UTF8, "application/xml") | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment