如何通过AsterNET.ARI调用Asterisk语音识别应用并获取结果?
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:
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.confloads the required speech modules, andspeech.confis properly configured for your chosen engine.
- Ensure you have a speech recognition engine enabled (e.g.,
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
SpeechRecognitionResultEventSubscription: 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'sSpeechBackground—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 viaresultEvent.Result.Textin the speech recognition event handler.
Additional Tips
- If you need to re-prompt the user for input (like how
SpeechBackgroundretries), wrap the play/recognition logic in a loop. - Double-check your
ari.confto ensure your ARI user has permissions for speech recognition actions.
内容的提问来源于stack exchange,提问作者shda

