如何使用 Twilio AMD 留下语音邮件?

How to leave a voicemail using Twilio AMD?

给定客户端代码,发出这样的呼出电话:

var accountSid = configuration["accountSid"];
        var authToken = configuration["authToken"];

        TwilioClient.Init(accountSid, authToken);

        var call = CallResource.Create(
            machineDetection: "DetectMessageEnd",
            asyncAmd: "true",
            asyncAmdStatusCallback: new Uri("[URL]/callback/index"),
            asyncAmdStatusCallbackMethod: HttpMethod.Post,
            twiml: new Twilio.Types.Twiml("<Response><Say>Ahoy there!</Say></Response>"),
            from: new Twilio.Types.PhoneNumber(configuration["fromPhoneNumber"]),
            to: new Twilio.Types.PhoneNumber(configuration["toPhoneNumber"])
        );

aka,启用了 asyncAmd 并指定了回调 URL,我的 webhook 控制器操作如下所示:

[ApiController]
[Route("callback")]
public class CallbackController : TwilioController
{
    [HttpPost]
    [Route("index")]
    public IActionResult Index()
    {
        var response = new VoiceResponse();

        if (Request.Form.TryGetValue("AnsweredBy", out var answeredBy))
        {
            if (answeredBy != "human")
            {
                response.Say("this is the voice message");
            }
        }

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

为什么没有留下语音信箱?

注意:我在 CallResource.Create 中包括了我想说的 Twiml b/c 我不希望在人工应答的情况下回调以获取消息内容。

我只需要对AMD检测结果进行回调,然后留言即可。

我用 response.Say 做吗?

谢谢!

这里是 Twilio 开发人员布道者。

Twilio 应答机检测可以在同步或异步模式下进行。来自 this blog post:

With Async AMD on Twilio, AMD is done asynchronously (hence the name). When the call is answered, a call url is executed immediately, so if a person answers the call rather than voicemail, they can begin interacting with your application without any silence. Then “asynchronously”, or “in parallel”, AMD processes the call audio and determines what answered the call. When AMD processing is complete, the result (the AnsweredBy parameter) is sent to a second URL, the asyncAmdStatusCallback.

One key difference between standard AMD and async AMD is how you modify the call once receiving the AMD result. With standard AMD, you have one URL and the result is sent to this URL just like any other outbound-api call. When your URL receives the result, you can check the AnsweredBy parameter and update the call accordingly with TwiML. With Async AMD, your call is already executing TwiML and you instead need to update the call via API.

在您的情况下,您使用的是异步 AMD,但您没有通过 API.

更新调用

你有两个选择。您可以选择使用同步 AMD,然后您可以像目前一样使用 TwiML 响应结果。

或者,您可以继续使用异步 AMD,但不使用 TwiML 响应 webhook,use the REST API to update the call with the new TwiML or with a new webhook URL

有一件事我也会注意。您的初始 TwiML 非常短,您的示例代码显示它发送 <Response><Say>Ahoy there!</Say></Response>。完全有可能在检测到应答机之前完成此 TwiML,并且由于它是呼叫的唯一 TwiML,因此呼叫随后将挂断。您可能需要考虑使用更长的消息或暂停,以便获得 AMD 的结果。

我想我会在这里跟进。感谢@philnash 的帮助。你确实是对的

  • 我从 asyncAMD webhook 返回 Twiml 而不是更新调用。
  • 我在初始调用中非常短的 Twiml 不够长

我完成了第一部分,但即使使用更长的 Twiml,事情仍然失败:

<Response><Say>Hello there. This is a longer message that will be about as long as the real message asking you to confirm or cancel your appointment. Hopefully it's long enough!</Say></Response>

但是,仍然 还不够长!当调用 asyncAMD 回调时,我尝试使用 CallResource.Update,调用已经结束。

我最终将大约 2,000 个 lorem ipsum 字符转储到传出调用 Twiml 中,使用 CallResource.Update 的 asyncAMD 回调工作得很好。