api-ai-cordova
Plugin makes it easy to integrate your Cordova application with api.ai natural language processing service. This plugin supports Android and iOS mobile operation systems.
Project on Github https://github.com/api-ai/api-ai-cordova
Page in Cordova Plugins Registry http://plugins.cordova.io/#/package/ai.api.apiaiplugin
Github issues https://github.com/api-ai/api-ai-cordova/issues
Demo application sources https://github.com/api-ai/api-ai-cordova-sample
Installation
- Make sure that Cordova CLI is installed
- Install api.ai plugin with Cordova CLI:
cordova plugin add ai.api.apiaiplugin
Usage
Add to your index.js file (typically in js folder) in function onDeviceReady following code
ApiAIPlugin;
Add to your page with mic button function to make voice requests:
{ try ApiAIPlugin; catch e ; }
If you want to create voice level visualization use function levelMeterCallback
to set callback for processing soundLevel:
ApiAIPlugin;
If you want to handle start and stop listening events, add appropriate handlers:
ApiAIPlugin; ApiAIPlugin;
Please note, that handlers must be added before ApiAIPlugin.requestVoice
call, like here:
{ try // !!! ApiAIPlugin; ApiAIPlugin
Then add call sendVoice
function from your button's onclick
:
Mic
If you want make text requests add the following code:
{ try ApiAIPlugin; catch e ; }
Also you can use function to cancel current api.ai request:
ApiAIPlugin;
API
// Initialize plugin// options - JSON object - `{// subscriptionKey: "your_subscription_key",// clientAccessToken: "your_access_token",// lang: "one_of_supported_languages"// }`// success - Function (optional) - callback for initialization success: function () {}// error - Function (optional) - callback for initialization error: function (error) {}ApiAIPlugin // Start listening, then make voice request to api.ai service// options - JSON object - voice request options (reserved for future use)// success - Function (optional) - callback for request success `function (response) {}` where response is Object // error - Function (optional) - callback for request error `function (error) {}` where error is StringApiAIPlugin // Make text request to api.ai service// options - JSON object - `{ query: "queryText" }`// success - Function (optional) - callback for request success `function (response) {}` where response is Object // error - Function (optional) - callback for request error `function (error) {}` where error is StringApiAIPlugin // Set callback for sound level. Need to call only once after initialization// callback - Function - function must be `function(level) { }`, level is float value from 0 to 1ApiAIPlugin // Cancel all pending requestsApiAIPlugin // Stop current listening process and send request to serverApiAIPlugin // Set callback for listening started event// callback - Function - must be simple function without arguments: function () {} ApiAIPlugin // Set callback for listening finished callback// callback - Function - must be simple function without arguments: function () {}ApiAIPlugin
Request Options
The options
parameter may contains following fields:
-
query
- text query, only appliable torequestText
function -
contexts
- list of strings, input context for the request (See Contexts Quick Start for more information about Contexts)contexts: "weather" "home" -
resetContexts
- boolean flag, set it to true to reset current active contextsresetContexts: true -
entities
- array of entities that replace developer defined entities for this request only. The entity(ies) need to exist in the developer console. Each entity is the pair of name andentries
array. Entries array contains one or more items withvalue
andsynonyms
fields.entities:name: "dwarfs"entries:value: "Ori"synonyms:"Ori""Nori"value: "bifur"synonyms:"Bofur""Bombur"
Supported Languages
- en
- es
- ru
- de
- pt
- pt-BR
- es
- fr
- it
- ja
- ko
- zh-CN
- zh-HK
- zh-TW
Promise-Based Wrapper
The promise-based wrapper was added for ease of use and better interoperability with other JavaScript code. Wrapper implemented using the Q library. You can use the wrapper through ApiAIPromises
module. For example:
ApiAIPromises;
More samples you can find in the tests module.