- Before you start coding
- Modify app permissions
- Initialize the SDK
- Define event handlers
- Start voice input
- Define voice input listeners (optional)
- To use the SDK, you'll need to create an Api.ai account
- Create an agent to get client access token key.
-
On Android: Modify
AndroidManifest.xml
and add Internet and Audio recording permissions:<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECORD_AUDIO" />
-
On iOS - no extra actions are required.
-
Connect API.AI SDK Component to your app.
-
Get client access token key from Api.ai developer console => agent's settings.
-
In your app code create instance of the
AIConfiguration
class. You must specify client access token and a language for the agent(seeSupportedLanguage
enumeration and supported languages).var config = new AIConfiguration("accessToken", SupportedLanguage.English);
-
Then create AIService instance using
AIService.CreateService
method.-
On iOS platform you need only AIConfiguration instance
aiService = AIService.CreateService(config);
-
On Android platform you need also context and optionally can specify recognition engine option
aiService = AIService.CreateService(context, config);
-
Now you need to specify event handlers for Api.ai results processing:
aiService.OnResult += AiService_OnResult;
aiService.OnError += AiService_OnError;
Sample OnResult
handler. Make sure you interact with the UI in the UI thread:
void AiService_OnResult(AIResponse response)
{
RunOnUiThread(() =>
{
if (!response.IsError)
{
if (response.Result != null)
{
resultTextView.Text = response.Result.Action;
}
}
else
{
resultTextView.Text = response.Status.ErrorDetails;
}
}
);
}
Sample OnError
handler:
void AiService_OnError(AIServiceException exception)
{
Log.Debug(TAG, "AIService Error: ", exception.ToString());
}
Now for start listening call StartListening
method. E.g. it could be started when the user presses the mic button:
aiService.StartListening();
Also you can add additional listeners for another recognition events:
aiService.ListeningStarted += AiService_ListeningStarted;
aiService.ListeningFinished += AiService_ListeningFinished;
aiService.AudioLevelChanged += AiService_AudioLevelChanged;