brunobrandes / api-ai-csharp

A C# wrapper for the api.ai
Apache License 2.0
18 stars 7 forks source link

C# api.ai

A C# wrapper for the api.ai.
This library makes very simple to integrate .NET applications with api.ai

Installation

To install Api.Ai.Csharp, run the following command in the Package Manager Console

PM> Install-Package Api.Ai.Csharp

Begin

"Api.ai provides developers and companies with the advanced tools they need to build conversational user interfaces for apps and hardware devices". To begin, you need to have an api.ai account.

See api.ai documentation for more details.

Structure

The solution structure (.sln) is based in The Onion Architecture

DataTranferObject (DTO) project, contains the call parameters used as either parameters in the URL or JSON keys in the POST body

ApplicationService project implements the application services.

Usage

  1. Using Dependency Injection, the configuration of the application with Simple Injector might look something like this:

    var container = new Container();
    
    container.RegisterSingleton<IServiceProvider>(container);
    container.Register<IApiAiAppServiceFactory, ApiAiAppServiceFactory>();
    container.Register<IHttpClientFactory, HttpClientFactory>();
  2. Get container api.ai app service factory

    var apiAiAppServiceFactory = container.GetInstance<IApiAiAppServiceFactory>();
  3. Create query app service

    var queryAppService = apiAiAppServiceFactory.CreateQueryAppService("https://api.api.ai/v1", 
    "YOUR_ACCESS_TOKEN");
  4. Create query request

    var queryRequest = new QueryRequest
    {
    SessionId = "1",
    Query = new string[] { "Hello, I want a pizza" },
    Lang = Domain.Enum.Language.English
    };
  5. Call api.ai query by http get method

    var queryResponse = await queryAppService.GetQueryAsync(queryRequest);

    5.1 Or call api.ai query by http post method (recommended)

    var queryResponse = await queryAppService.PostQueryAsync(queryRequest);

Use ApiAiJson to Serialize/Deserialize response.

  var json = ApiAiJson<QueryResponse>.Serialize(queryResponse);

json property value:

  {
    "result": {
      "source": "agent",
      "resolvedQuery": "Hello, I want a pizza",
      "score": 1.0,
      "action": "order.pizza",
      "parameters": {},
      "contexts": [
        {
          "name": "pizza",
          "lifespan": 5
        },
        {
          "name": "pizza-type",
          "lifespan": 5
        }
      ],
      "fulfillment": {
        "speech": "Sure, let me help you choose the best pizza for you! What flavor would you like?"
      },
      "metadata": {
        "intentId": "f5c9fb83-b99e-4af5-bae7-6405a6501d10",
        "webhookUsed": false,
        "intentName": "pizza"
      }
    },
    "id": "46ead378-2d8a-41cf-a876-eed8bb531dab",
    "timestamp": "2016-04-20T21:05:50.997Z",
    "status": {
      "code": 200,
      "errorType": "success"
    }
  }

Download the examples for test your agent :)

See the Bot Application Project sample which contains code of how to create
and publish your bot in Microsoft Bot Framework

Download and install the Bot Application template

TODO

License

This software is open source, licensed under the Apache License.
See LICENSE.me for details.