简体   繁体   English

无法在twilio消息接收服务中接收正文C#

[英]Unable to receive body in a twilio message receive service c#

I am trying to receive msg body using Twilio API and webhook. 我正在尝试使用Twilio API和webhook接收味精正文。 The webhook service is hit when I send message on the subscribed number but the body of the message is null. 当我在订阅的号码上发送消息时,Webhook服务被命中,但消息的主体为空。

C# code snippet below, the requestBody is always null in this case even though I send "hello" 下面的C#代码段,即使我发送“ hello”,在这种情况下requestBody始终为null

TwilioClient.Init(accountSid, authToken);
var requestBody = Request.Form["Body"];
            var response = new MessagingResponse();
            if (requestBody == "hello")
            {
                response.Message("Hi!");
            }
            else if (requestBody == "bye")
            {
                response.Message("Goodbye");
            }
 return TwiML(response);

Twilio Evangelist Here.... Twilio传播者在这里...

I took a look at your question and thought it could have 2 possible things going on. 我查看了您的问题,认为可能有2种可能的情况。

The first is it seems that the line var requestBody = Request.Form["Body"]; 第一个似乎是var requestBody = Request.Form["Body"]; may not be parsing the POST request body correctly. 可能无法正确解析POST请求主体。 You may want to debug and confirm that the Request.Form["Body"] does in fact hold the message body being sent in. 您可能需要调试并确认Request.Form["Body"]实际上确实保留了正在发送的消息正文。

The second thing here would be there isn't a default case in the event the if/else if conditions don't evaluate to true. 第二件事是,如果if / else if条件未评估为true,则没有默认情况。 So I wrote up a sample that we tested out on my 7/30 Twitch stream (www.twitch.tv/cldubya) for you. 因此,我为您编写了一个样本,我们在7/30 Twitch流(www.twitch.tv/cldubya)上进行了测试。 It is written for ASP.NET Core and uses parameter binding to grab the message body. 它是为ASP.NET Core编写的,并使用参数绑定来获取消息正文。 Take a look below. 看看下面。

[HttpPost]
public IActionResult Post([FromForm] string body)
{
    var requestBody = body;
    var response = new MessagingResponse();
    if (string.Equals(requestBody,"hello",StringComparison.CurrentCultureIgnoreCase))
    {
        response.Message("Hi!");
    }
    else if (string.Equals(requestBody, "bye", StringComparison.CurrentCultureIgnoreCase))
    {
         response.Message("Goodbye");
    }
    // adding a default message in the event that the if/else if condition doesn't evaluate to true
   else
   {
         response.Message("Couldn't determine what to respond with");
   }
   return new ContentResult { Content = response.ToString(), ContentType = "application/xml", StatusCode = 200 };
}

Take a look and let me know what you think. 看一看,让我知道您的想法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM