0

我正在使用 WebAPI 构建一个 Twilio IVR 并遇到了一些障碍。我正在尝试循环播放录音,并在每个录音结束时提供按 1 删除录音的选项。

我在 C# WebAPI 实现中的任何地方都找不到任何示例,并且以下内容不起作用,我不知道该怎么做。

我知道的;

  • 需要收集输入的数字
  • 不知何故将录音 sid 传回给 api 以知道要删除哪个录音。

这是我到目前为止编写的代码:

public HttpResponseMessage Messages() {
    string baseUrl = Url.Request.RequestUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
        var twilio = new TwilioRestClient(_accountSid, _authToken);
            var recordings = twilio.ListRecordings(null, DateTime.Today, null, null);
            var twilioResponse = new TwilioResponse();

            if (recordings != null && recordings.Recordings.Count > 0)
            {
                var msgCount = 1;
                var msgTotal = recordings.Recordings.Count();
                foreach (var recording in recordings.Recordings)
                {
                    var caller = twilio.GetCall(recording.CallSid);
                    var callerNumber = Regex.Replace(caller.From, @"([0-9]{1})", "$1,");
                    var callDate = recording.DateCreated.ToString("dddd MMMM d");
                    var callTime = recording.DateCreated.ToString("h m t");

                    twilioResponse.Say(string.Format("Playing message {0} of {1}, from {2} on {3} at {4} M.", msgCount, msgTotal, callerNumber, callDate, callTime),
                    new { voice = "woman" });

                    var voiceFile = string.Format("{0}2010-04-01/Accounts/{1}/Recordings/{2}.mp3", twilio.BaseUrl, _accountSid, recording.Sid);
                    twilioResponse.Play(voiceFile);

                    //twilioResponse.BeginGather(new
                    //                           {
                    //                               action = baseUrl + "/api/Recording/Delete",
                    //                               numDigits = 1,
                    //                           });

                    //twilioResponse.Say("TO DELETE THIS MESSAGE PRESS 1");
                    //twilioResponse.EndGather();

                    msgCount++;
                }
            }

            return Request.CreateResponse(HttpStatusCode.OK, twilioResponse.Element, Configuration.Formatters.XmlFormatter);
        }

我以前使用过 Twilio 的 API,但这对我来说是一个新的,我真的不知道这是否可能。他们拥有的 PHP 示例正在这样做,所以也许我只是错过了标记,因为我应该对此采取不同的方式。

4

1 回答 1

0

Twilio 布道者在这里。

你所拥有的看起来非常接近。让我们看看在用户按下一个的情况下传回 Recording SID,因为通过使用您在 Gather 动词上设置的操作 URL 为您保存一些状态相对容易:

action = baseUrl + "/api/Recording/Delete?recordingSid=" + recording.Sid;

现在,当用户按下一个时,Twilio 将向包含recordingSid 参数的Action URL 发出请求。

删除录音后,您只需重定向回您的消息端点以继续收听更多录音。为了跟踪您已经听过哪些录音,您可能需要在操作 URL 中传递一些额外的参数,这些参数可以通过您的删除工作流程并返回到收听工作流程。

希望有帮助。

于 2014-05-27T16:18:09.470 回答