Getting Started with Api.ai Xamarin SDK

Before you start coding

Modify app permissions

  • 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.

Initialize the SDK

  1. Connect API.AI SDK Component to your app.
  2. Get client access token key from Api.ai developer console => agent's settings.
  3. In your app code create instance of the AIConfiguration class. You must specify client access token and a language for the agent(see SupportedLanguage enumeration and supported languages).

    var config = new AIConfiguration("accessToken", SupportedLanguage.English);
  4. 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);

Define event handlers

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;    
                resultTextView.Text = response.Status.ErrorDetails;

Sample OnError handler:

void AiService_OnError(AIServiceException exception)
    Log.Debug(TAG, "AIService Error: ", exception.ToString());

Start voice input

Now for start listening call StartListening method. E.g. it could be started when the user presses the mic button:


Define additional listeners for voice input

Also you can add additional listeners for another recognition events:

aiService.ListeningStarted += AiService_ListeningStarted;
aiService.ListeningFinished += AiService_ListeningFinished;
aiService.AudioLevelChanged += AiService_AudioLevelChanged;