Giter Site home page Giter Site logo

apiai-dotnet-client's Introduction

api.ai: .NET Library

Build Status Nuget Version

The api.ai .NET Library makes it easy to integrate the API.AI natural language processing API into your .NET application. API.AI allows using voice commands and integration with dialog scenarios defined for a particular agent in API.AI.

Library provides simple programming interface for making text and voice requests to the API.AI service.

Getting started

Installation

Library can be installed with Nuget

PM> Install-Package ApiAiSDK

Or can be downloaded as sources from the Releases page.

Usage

Assumed you already have API.AI account and have at least one agent configured. If no, please see documentation on the API.AI website.

First, add following usages to your module:

using ApiAiSDK;
using ApiAiSDK.Model;

Then add ApiAi field to your class:

private ApiAi apiAi;

Now you need to initialize ApiAi object with appropriate access keys and language.

var config = new AIConfiguration("YOUR_CLIENT_ACCESS_TOKEN", SupportedLanguage.English);
apiAi = new ApiAi(config);

Done! Now you can easily do requests to the API.AI service

  • using TextRequest method for simple text requests

    var response = apiAi.TextRequest("hello");
  • using VoiceRequest method for voice binary data in PCM (16000Hz, Mono, Signed 16 bit) format

    var response = apiAi.VoiceRequest(voiceStream);

Also see unit tests for more examples.

Windows Phone 8

Windows Phone version has some additional features such as system speech recognition for easy API.AI service integration. After installing the library you should add permissions for Internet and Sound recording to your app. Currently, speech recognition is performed using Windows Phone System speech recognition. So, you must be sure languages you are using is installed on device (It can be checked on Settings->speech screen of device).

To use special features you need to use AIService class instead of ApiAi class.

Initialization

First, you need to initialize AIConfiguration object with your keys and desired language.

var config = new AIConfiguration("client access token", SupportedLanguage.English);

Second, create AIService object using the configuration object.

var aiService = AIService.CreateService(config);

Now you need add handlers for OnResult and OnError events

aiService.OnResult += aiService_OnResult;
aiService.OnError += aiService_OnError;

And at the end call Initialization method

await aiService.InitializeAsync();

The entire code snippet:

try
{
    var config = new AIConfiguration("client access token", SupportedLanguage.English);

    aiService = AIService.CreateService(config);
    aiService.OnResult += aiService_OnResult;
    aiService.OnError += aiService_OnError;
    await aiService.InitializeAsync();
}
catch (Exception e)
{
    // Some exception processing
}

Using API.AI

Now you can use methods for listening and requesting results from server, all you need to call StartRecognitionAsync method (don't forget to use await operator, otherwise you will not be able to catch some processing exceptions)

try
{
    await aiService.StartRecognitionAsync();
}
catch (Exception exception)
{
    // Some exception processing
}

Results processing

Results will be passed to the OnResult handler, most of errors will be passed to the OnError handler. Don't forget to use dispatcher when working with UI, because of handlers can be called from the Background thread.

void aiService_OnError(AIServiceException error)
{
    Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        // sample error processing
        ResultTextBlock.Text = error.Message;
    });
}

void aiService_OnResult(ApiAiSDK.Model.AIResponse response)
{
    Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        // sample result processing
        ResultTextBlock.Text = response.Result.ResolvedQuery;
    });
}

Universal Windows Platform

UWP version of the library is similar to Windows Phone version except some differences in API. After installing the library you should add capabilities for Internet(Client) and Microphone to your app. Currently, speech recognition is performed using Windows.Media.SpeechRecognition speech recognition. So, you must be sure languages you are using is installed on device. API for the platform uses async/await feature. So, you don't need to set up any callbacks.

To use special features you need to use AIService class instead of ApiAi class.

Initialization

First, you need to initialize AIConfiguration object with your keys and desired language.

var config = new AIConfiguration("client access token", SupportedLanguage.English);

Second, create AIService object using the configuration object.

var aiService = AIService.CreateService(config);

And at the end call Initialization method

await aiService.InitializeAsync();

The entire code snippet:

try
{
    var config = new AIConfiguration("client access token", SupportedLanguage.English);
    aiService = AIService.CreateService(config);
    await aiService.InitializeAsync();
}
catch (Exception e)
{
    // Some exception processing
}

Using API.AI

Now you can use methods for listening and requesting results from server, all you need to call StartRecognitionAsync method (don't forget to use await operator, otherwise you will not be able to catch some processing exceptions)

try
{
    var response = await aiService.StartRecognitionAsync();
}
catch (Exception exception)
{
    // Some exception processing
}

Results processing

Results will be in the response variable.

Open Source Project Credits

  • JSON parsing implemented using Json.NET.

apiai-dotnet-client's People

Contributors

pmiri avatar xvir avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.