Twilio在ASP.NET MVC中转录语音

时间:2019-02-04 12:12:50

标签: c# asp.net-mvc twilio

我正在尝试转录呼叫者的语音反应,并通过twilio以编程方式读出用户的语音反应。

因此,当用户最初调用twilio号码时,该呼叫就被钩接到ASP.NET MVC应用程序的以下操作方法(在https://www.twilio.com/docs/voice/twiml/record?code-sample=code-record-a-voicemail&code-language=C%23&code-sdk-version=5.x中进行了查看)。

[HttpPost]
public TwiMLResult Welcome()
{
    var response = new VoiceResponse();
    try
    {
        response.Say("Please say your user Id, example ABC123, \n and press star when done", Say.VoiceEnum.Alice, null, Say.LanguageEnum.EnGb);
        // record and transcribe users voice        
        response.Record(
        transcribe: true,
        transcribeCallback: new Uri("https://35eb31e3.ngrok.io/Ivr/HandleTranscribedVrn"),
        finishOnKey: "*");
        response.Say("I did not receive a recording");
    }
    catch (Exception e)
    {
        ErrorLog.LogError(e, "Error within ivr/Welcome");
        response = RejectCall();
    }

    return TwiML(response);
}  

注意-https://35eb31e3.ngrok.io/Ivr/HandleTranscribedVrn是ngRok隧穿的公共URL,用于回调方法。

因此,在用户说出自己的用户ID然后按*键之后,我试图记录用户的语音输入。因此,按下*后,我希望twilio进行转录,并使用转录文本和其他转录信息来响应以下回调操作方法(https://35eb31e3.ngrok.io/Ivr/HandleTranscribedVrn)。

[HttpPost]
public TwiMLResult HandleTranscribedVrn()
{
    var response = new VoiceResponse();
    try
    {
        // get the transcribed result - https://www.twilio.com/docs/voice/twiml/record#transcribe
        var result = new TranscribedResult
        {
            TranscriptionSid = Request.Params["TranscriptionSid"],
            TranscriptionText = Request.Params["TranscriptionText"],
            TranscriptionUrl = Request.Params["TranscriptionUrl"],
            TranscriptionStatus = Request.Params["TranscriptionStatus"],
            RecordingSid = Request.Params["RecordingSid"],
            RecordingUrl = Request.Params["RecordingUrl"],
            AccountSid = Request.Params["AccountSid"]
        };

        // reading the transcibed result
        response.Say("You said,\n {0}", result.TranscriptionText);

        // done
        response.Say("Good Bye", Say.VoiceEnum.Alice, null, Say.LanguageEnum.EnGb);
    }
    catch (Exception e)
    {
        ErrorLog.LogError(e, "Error within ivr/HandleTranscribedVrn");
        response.Say(ConversationHelper.NothingReceived, ConversationHelper.SpeakVoice, 1, ConversationHelper.SpeakLanguage);
    }
    return TwiML(response);
}

简而言之,我希望上面的回调操作能够将记录抄录为用户语音输入并读出,例如

您说的{Users Voice Transcript-example-abc123},再见

问题

当用户拨打twilio号码时,它会执行Welcome()操作控制器,并说

“请说出您的用户ID,例如ABC123,\ n,并在输入完成后按星号”

用户说出他/她的用户ID-EFG456,然后照常按*键。

然后再次说(无限次,直到用户断开呼叫),而不进行转录的回叫操作-HandleTranscribedVrn-“请说出您的用户ID,例如ABC123,\ n,并在完成后按星号”

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

在Twilio支持的帮助下,我们设法找到了该解决方案。因此,我们必须使用Twilio提供的<record>功能来代替<gather>。总体而言,我们可以使用语音,DTMF音调(键盘输入)或同时使用两者。 语音转录准备就绪后,将执行collect complete回调方法。可以在https://www.twilio.com/docs/voice/twiml/gather

上找到更多信息。

下面是示例代码。希望对遇到类似问题的人有所帮助。

[HttpPost]
public ActionResult Welcome()
{
    var response = new VoiceResponse();
    try
    {
        var gatherOptionsList = new List<Gather.InputEnum>
        {
            Gather.InputEnum.Speech,
            //Gather.InputEnum.Dtmf
        };
        var gather = new Gather(
            input: gatherOptionsList,
            timeout: 60,
            finishOnKey:"*",
            action: Url.ActionUri("OnGatherComplete", "Ivr")
            );
        gather.Say("Please say \n", Say.VoiceEnum.Alice, 1, Say.LanguageEnum.EnGb);
        response.Append(gather);           
    }
    catch (Exception e)
    {
        ErrorLog.LogError(e, "Error within ivr/Welcome");           
    }
    return TwiML(response);
}

[HttpPost]
public TwiMLResult OnGatherComplete(string SpeechResult, double Confidence)
{
    var response = new VoiceResponse();
    try
    {
        var identifyingConfidence = Math.Round(Confidence * 100, 2);
        var transcript = $"You said {SpeechResult} with Confidence {identifyingConfidence}.\n Good Bye";
        var say = new Say(transcript);         
        response.Append(say);
    }
    catch (Exception e)
    {
        ErrorLog.LogError(e, "Error within ivr/OnGatherComplete");          
    }
    return TwiML(response);
}