1
votes

Below is my code for both methods -

GatherCall is returning Twiml still I get HTTP 415 & same for StatusCallback method. Can someone please help here? I am not even able to test this using ngrok as Tunneling tools don't work in my organisation network. I am using Azure to test this one by logging everything.

public TwiMLResult GatherCall([FromRoute] string id, [FromBody] VoiceRequest voiceRequest )
{
    _logger.LogInformation("*****************GatherCall - Start****************");
    var response = new VoiceResponse();

    try
    {
        _logger.LogInformation("Gather call back for -" + id);

        _logger.LogInformation("VoiceRequest parameters-------------------------");
        _logger.LogInformation("CallSid : " + voiceRequest.CallSid);
        _logger.LogInformation("CallStatus : " + voiceRequest.CallStatus);
        _logger.LogInformation("AccountSid : " + voiceRequest.AccountSid);
        _logger.LogInformation("From : " + voiceRequest.From);
        _logger.LogInformation("To : " + voiceRequest.To);
        _logger.LogInformation("Digits : " + voiceRequest.Digits);
        _logger.LogInformation("Direction : " + voiceRequest.Direction);
        _logger.LogInformation("TranscriptionText : " + voiceRequest.TranscriptionText);

        if (voiceRequest.Digits == "1234")
        {
            response.Say("Your response has been recorded. Thank you.", voice: "alice");
            response.Hangup();
            //acctepted response
            //update call status to db
            //acknowledge incident
        }
        else
        {
            response.Say("Incorrect code enterted. Please enter correct code to accespt this incident", voice: "alice");
            response.Pause(3);
            response.Hangup();
        }

        _logger.LogInformation("*****************GatherCall - EndTry****************");
    }
    catch (Exception ex)
    {
        _logger.LogError(ex.Message);
        _logger.LogInformation("*****************GatherCall - EndCatch****************");
    }
    return TwiML(response);
    // return TwiML(response.ToString(), "application/xml");
}

public IActionResult StatusCallBack([FromRoute] string id, [FromBody] StatusCallbackRequest statusCallbackRequest)
{
    _logger.LogInformation("*****************StatusCallBack - Start****************");
    try
    {
        _logger.LogInformation("Status call back called for -" + id );

        _logger.LogInformation("StatusCallbackRequest parameters-------------------------");
        _logger.LogInformation("CallSid : " + statusCallbackRequest.CallSid);
        _logger.LogInformation("CallStatus : " + statusCallbackRequest.CallStatus);
        _logger.LogInformation("AccountSid : " + statusCallbackRequest.AccountSid);
        _logger.LogInformation("From : " + statusCallbackRequest.From);
        _logger.LogInformation("To : " + statusCallbackRequest.To);
        _logger.LogInformation("Digits : " + statusCallbackRequest.Digits);
        _logger.LogInformation("Direction : " + statusCallbackRequest.Direction);
        _logger.LogInformation("TranscriptionText : " + statusCallbackRequest.TranscriptionText);


        _logger.LogInformation("*****************StatusCallBack - EndTry****************");
        return Ok("Handled");
    }
    catch (Exception ex)
    {
        _logger.LogError(ex.Message);
        _logger.LogInformation("*****************StatusCallBack - EndCatch****************");
        return Ok("Handled in catch");
    }
}
1

1 Answers

9
votes

Twilio Evangelist here... Sorry that you're having a tough time. It looks like your code is trying to bind the voiceRequest parameter to the body of the incoming POST request from Twilio (using [FromBody]). If Twilio is sending a POST request, you'll want to bind to the form (using [FromForm]). Otherwise, ASP.NET Core will return a 415 error code.

Try swapping out the [FromBody] with a [FromForm]. That should fix this on a POST request. I would also suggest verifying that Twilio is in fact sending a POST, not a GET request.

If you run into more problems, send a note across via email to [email protected]. Hope this helps.