Twilio - 使用C#获取传入的电话号码

时间:2017-12-19 23:19:37

标签: twilio-api

在我的C#asp.net核心web api项目中,我正在使用webhooks从我的Twilio电话号码中捕获传入的语音呼叫。除了处理呼叫之外,我还想通过电子邮件通知管理员来电。为此,我需要获取传入的电话号码。我该怎么做?

这是我的代码:

[HttpPost("incomingVoice")]
public async Task<IActionResult> IncomingVoice(string digits)
{
    var response = new VoiceResponse();

    if (!string.IsNullOrEmpty(digits))
    {
        ... Handle the user selection
    }

    response.Append(new Gather(numDigits: 1).Say("Welcome to ...! For sales, press 1. For marketing, press 2. For support press 3", voice: "alice", language: "en-GB"));

    //handle the lack of response
    response.Say("Please leave a message after the tone.  Hang up when finished.", voice: "alice", language: "en-GB");
    response.Record();
    response.Hangup();

    string phoneNumber = <??? INCOMING PHONE NUMBER ???>

    IRestResponse mgresp = await SendMGMessage(phoneNumber);

    return Content(response.ToString(), "application/xml");
}

谢谢。

2 个答案:

答案 0 :(得分:1)

经过一番挖掘,我找到了这个链接:https://www.twilio.com/docs/api/twiml/twilio_request

在其中,指定TwiML请求包括各种参数。从那里我能够提取电话号码:

string phoneNumber = Request.Form["From"];

希望为您节省一些研究时间。

答案 1 :(得分:0)

除了dpdragnev's solution之外,您还可以打印出所有可用的值,像这样...

[HttpGet]
public TwiMLResult Welcome()
{
  var lst = Request.QueryString.AllKeys
    .Select(key => new {Name = key.ToString(), Value = Request.QueryString[key.ToString()]})
    .ToArray();
  foreach (var v in lst)
  {
    _logger.Info(JsonConvert.SerializeObject(v, Formatting.Indented));
  }
  ...
}

NLog示例输出:

2019-05-07 17:00:36.7601|INFO|TwilioMvcDemo.Controllers.IvrController|{
  "Called": "+15555555555",
  "ToState": "CA",
  "CallerCountry": "US",
  "Direction": "inbound",
  "ApiVersion": "2010-04-01",
  "CallStatus": "ringing",
  "From": "+15555554444",
  "AccountSid": "CdEf567",
  "CalledCountry": "US",
  "CallerCity": "MARS OUTPOST",
  ...
}