简体   繁体   English

Twilio - 获取来电号码 C#

[英]Twilio - Get the incoming phone number with C#

In my C# asp.net core web api project I am using webhooks to capture the incoming voice call from my Twilio phone number.在我的 C# asp.net core web api 项目中,我使用 webhooks 从我的 Twilio 电话号码捕获传入的语音呼叫。 In addition to handling the call, I would like to notify the admin for the incoming call via email. To do this I need to get the incoming phone number.除了处理电话外,我还想通过 email 通知管理员来电。为此,我需要获取来电号码。 How do I do that?我怎么做?

Here is my code:这是我的代码:

[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");
}

Thank you.谢谢你。

After some digging around, I found this link: https://www.twilio.com/docs/api/twiml/twilio_request经过一番挖掘,我发现了这个链接: https://www.twilio.com/docs/api/twiml/twilio_request

In it, it is specified that the TwiML request includes a whole variety of parameters.其中,指定 TwiML 请求包括各种参数。 From there I was able to extract the phone number as:从那里我能够提取电话号码为:

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

Hope that saves you some research time.希望能为您节省一些研究时间。

In addition to dpdragnev's solution , you can print out all of the available values like this...除了dpdragnev 的解决方案之外,您还可以像这样打印出所有可用值...

[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 sample output: NLog 示例 output:

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",
  ...
}

I had a similar problem, but I am trying to solve this using Minimal Web API with .NET 6.我有一个类似的问题,但我正在尝试使用 Minimal Web API 和 .NET 6 来解决这个问题。

Twilio has this great tutorial: Twilio 有这个很棒的教程:

https://www.twilio.com/blog/sms-voice-do.net-6-minimal-api https://www.twilio.com/blog/sms-voice-do.net-6-minimal-api

But it didn't answer how to access the incoming phone number from inside the webhook.但它没有回答如何从 webhook 内部访问传入的电话号码。

I'm not sure how to convert from Microsoft.AspNetCore.HttpRequest object to the preferred Twilio.AspNet.Common.SmsRequest object.我不确定如何从 Microsoft.AspNetCore.HttpRequest object 转换为首选 Twilio.AspNet.Common.SmsRequest object。

The below code appears to work, but would welcome advice for a better approach.下面的代码似乎有效,但欢迎就更好的方法提出建议。

//app.MapPost("/smshandler", (SmsRequest incomingMessage) => SmsHandler(incomingMessage));  // does not work
app.MapPost("/smshandler", (HttpRequest request) => SmsHandler(request));

private static IResult SmsHandler(HttpRequest request)
    {
        try
        {
            string from = request.Form["From"];
            string body = request.Form["Body"];

            DoWork(from, body);
            return Results.Ok();
        }
        catch (Exception ex)
        {
            return Results.Problem(ex.Message);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Twilio studio flow 获取属性值和电话号码到 twilio flex? - How to get attribute values and phone number from Twilio studio flow to twilio flex? 在 C# 中使用 AWS SNS 确认电话号码 - Phone number confirmation with AWS SNS in C# 如何将Twilio号码的来电转接到另一个号码? - How to forward an incoming call to a Twilio number to another number? RestException 不是 Twilio 试用帐户上的有效电话号码 - RestException is not a valid phone number on Twilio trial account Twilio:Webhook 未在电话号码上调用指定的 URL - Twilio: Webhook not calling specified URL on phone number twilio 错误“发件人电话号码无效” - twilio error "The from phone number is not valid" Twilio 工作室要中介电话吗? - Twilio Studio to have a phone number as an intermediary? 获取重定向到 Twilio 的原始 To &amp; From 电话号码 - Get original To & From phone numbers redirected to Twilio twilio 来电事件侦听器未调用但在我拨打我的 twilio 号码时响铃 python flask - twilio incoming call event listener not called but ringing when i call my twilio number python flask 在 Twilio 试用帐户中,我无法收到 OTP 场景的传入消息 - In Twilio trial account I unable to get incoming message for OTP scenario
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM