You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过AsterNET.ARI调用Asterisk语音识别应用并获取结果?

How to Implement Speech Recognition in Asterisk ARI with AsterNET.ARI (Instead of Dialplan SpeechBackground)

I see you're trying to replicate the dialplan's SpeechBackground functionality in an ARI Stasis app using AsterNET.ARI, and your initial attempt with OriginateAsync threw a 400 error—let's break down why that didn't work and how to do it the right way.

Why Your Initial Attempt Failed

Your first approach using OriginateAsync was misaligned with what ARI is designed for. OriginateAsync exists to initiate new outbound calls, not to execute dialplan steps on an existing channel that's already active in a Stasis app. Passing raw dialplan syntax like "1,n,BackGround(hello-world)" as an argument here doesn't make sense to the ARI API, hence the 400 Bad Request.

The Correct ARI-Native Approach

Instead of trying to call dialplan functions from ARI, you'll use ARI's built-in speech recognition APIs to achieve the same behavior as SpeechBackground. Here's a step-by-step implementation:

  1. Pre-requisite: Verify Asterisk Speech Recognition Configuration

    • Ensure you have a speech recognition engine enabled (e.g., res_speech_unimrcp, res_speech_google).
    • Confirm modules.conf loads the required speech modules, and speech.conf is properly configured for your chosen engine.
  2. Updated Stasis App Code
    You'll need to subscribe to speech recognition result events, play your prompt, and start the recognition session directly on the active channel:

static async void c_OnStasisStartEvent(IAriClient sender, StasisStartEvent e)
{
    var channelId = e.Channel.Id;
    
    // Answer the incoming channel
    await sender.Channels.AnswerAsync(channelId);
    
    // Subscribe to speech recognition results for this specific channel
    sender.OnSpeechRecognitionResultEvent += (s, resultEvent) =>
    {
        if (resultEvent.Channel.Id == channelId)
        {
            // Get the recognized text (equivalent to dialplan's ${SPEECH_TEXT(0)})
            var recognizedText = resultEvent.Result.Text;
            Console.WriteLine($"Recognized speech: {recognizedText}");
            
            // Add your custom logic here, then hang up the channel
            sender.Channels.HangupAsync(channelId);
        }
    };
    
    // Play the beep prompt (matches the SpeechBackground(beep) dialplan step)
    await sender.Channels.PlayAsync(channelId, "sound:beep");
    
    // Start speech recognition with configurable parameters
    await sender.Channels.StartSpeechRecognitionAsync(
        channelId,
        new StartSpeechRecognitionOptions
        {
            Language = "en-US", // Set your target language code
            EndSilence = 2000, // Stop recognition after 2 seconds of silence
            MaxDuration = 10000 // Max time to listen for speech (10 seconds)
        }
    );
}

Key Details Explained

  • SpeechRecognitionResultEvent Subscription: This event fires when Asterisk finishes processing speech input and returns the transcribed text. We filter by channel ID to ensure we're handling results for the correct call.
  • StartSpeechRecognitionAsync: This replaces the dialplan's SpeechBackground—it tells Asterisk to start listening for speech on the channel, with customizable rules for language, silence detection, and maximum listen time.
  • Equivalent to ${SPEECH_TEXT(0)}: The recognized text is accessible via resultEvent.Result.Text in the speech recognition event handler.

Additional Tips

  • If you need to re-prompt the user for input (like how SpeechBackground retries), wrap the play/recognition logic in a loop.
  • Double-check your ari.conf to ensure your ARI user has permissions for speech recognition actions.

内容的提问来源于stack exchange,提问作者shda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 14:07:46