Skip to content

Latest commit

 

History

History
104 lines (80 loc) · 3.33 KB

GettingStarted.md

File metadata and controls

104 lines (80 loc) · 3.33 KB

Getting Started with Api.ai Xamarin SDK

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

  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);

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;