Skip to content

dehghanfar/api-ai-csharp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

  • ApiAiAppService
    Base properties and methods of application service.

  • QueryAppService
    Query application service.
    Process natural language in either text form or sound file.

  • TtsAppService
    Tts application service.
    Used to perform text-to-speech - generate speech (audio file) from text.

  • EntitiesAppService
    Entitie application service The entities app service is used to create, retrieve, update and delete developer-defined entity objects.

  • UserEntitiesAppService
    not implemented

  • IntentsAppService
    not implemented

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>();
  1. Get container api.ai app service factory
var apiAiAppServiceFactory = container.GetInstance<IApiAiAppServiceFactory>();
  1. Create query app service
var queryAppService = apiAiAppServiceFactory.CreateQueryAppService("https://api.api.ai/v1", 
  "YOUR_ACCESS_TOKEN");
  1. Create query request
var queryRequest = new QueryRequest
{
  Query = new string[] { "Hello, I want a pizza" },
  Lang = Domain.Enum.Language.English
};
  1. 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

  • Create tts DataTranferObject
  • Implement TtsAppService
  • Create entities DataTranferObject
  • Implement EntitiesAppService
  • Create webhook DataTranferObject
  • Create userEntities DataTranferObject
  • Implement UserEntitiesAppService
  • Create intents DataTranferObject
  • Implement IntentsAppService
  • Write unit tests

License

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

About

A C# wrapper for the api.ai

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 99.4%
  • Other 0.6%