A C# wrapper for the api.ai. This library makes very simple to integrate .NET applications with api.ai
To install Api.Ai.Csharp, run the following command in the Package Manager Console
PM> Install-Package Api.Ai.Csharp
"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.
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
userEntities - manage user-level entities not implemented
intents - manage intents not implemented
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
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>();
Get container api.ai app service factory
var apiAiAppServiceFactory = container.GetInstance<IApiAiAppServiceFactory>();
Create query app service
var queryAppService = apiAiAppServiceFactory.CreateQueryAppService("https://api.api.ai/v1",
"YOUR_ACCESS_TOKEN");
Create query request
var queryRequest = new QueryRequest
{
SessionId = "1",
Query = new string[] { "Hello, I want a pizza" },
Lang = Domain.Enum.Language.English
};
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
This software is open source, licensed under the Apache License. See LICENSE.me for details.