• Stars
    star
    141
  • Rank 259,971 (Top 6 %)
  • Language
    Ruby
  • License
    Apache License 2.0
  • Created almost 10 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ruby SDK for Dialogflow

DEPRECATED The API.AI ruby gem

Deprecated
This Dialogflow client library and Dialogflow API V1 have been deprecated and will be shut down on October 23th, 2019. Please migrate to Dialogflow API V2 and the v2 client library

Gem Version

A Ruby SDK to the https://api.ai natural language processing service.

Installation

gem install api-ai-ruby

Basic Usage

Just pass correct credentials to ApiAiRuby::Client constructor

client = ApiAiRuby::Client.new(
    :client_access_token => 'YOUR_CLIENT_ACCESS_TOKEN'
)

After that you can send text requests to the https://api.ai with command

response = client.text_request 'hello!'

Or try to invocate intent via defined 'event':

response_zero = client.event_request 'MY_CUSTOM_EVENT_NAME';
response_one = client.event_request 'MY_EVENT_WITH_DATA_TO_STORE', {:param1 => 'value'}
response_two = client.event_request 'MY_EVENT_WITH_DATA_TO_STORE', {:some_param => 'some_value'}, :resetContexts => true

voice_request and text_request methods returns symbolized https://api.ai response. Structure of response can be found at https://docs.api.ai/docs/query#response.

Advanced usage

During client instantiating you can additionally set parameters like api url, request language and version (more info at https://docs.api.ai/docs/versioning, https://docs.api.ai/docs/languages)

ApiAiRuby::Client.new(
    client_access_token: 'YOUR_ACCESS_TOKEN',
    api_lang: 'FR',
    api_base_url: 'http://example.com/v1/',
    api_version: 'YYYYMMDD',
    api_session_id: 'some_uuid_or_whatever'
)

And you also can send additional data to server during request, use second parameter of text_request and voice_request methods to do that

    response = client.text_request 'Hello', :contexts => ['firstContext'], :resetContexts => true
    response = client.voice_request file, :timezone => 'America/New_York'

More information about possible parameters can be found at https://docs.api.ai/docs/query page

User Entities

Another possibility is to send and retrieve custom entities to the server.

You can do it along with query request

client.text_request 'call Mozart', entities: [
    {
        name: 'contacts',
        entries: [
            ApiAiRuby::Entry.new('Mozart', %w(Mozart Wolfgang)),
            ApiAiRuby::Entry.new('Salieri', %w(Salieri Antonio))
        ]
    }
]

# the same without ApiAiRuby::Entry wrapper

client.text_request 'call Mozart', entities: [
    {
        name: 'contacts',
        entries: [
            {value: 'Mozart', synonyms: %w(Mozart Wolfgang)},
            {value: 'Salieri', synonyms: %w(Salieri Antonio)}
        ]
    }
]

Or with separate create_user_entities_request object with full CRUD support:

# preparations
entries_composers = [
    ApiAiRuby::Entry.new('Mozart', %w(Mozart Wolfgang)),
    ApiAiRuby::Entry.new('Salieri', %w(Salieri Antonio))
]

entries_unknown = [
    ApiAiRuby::Entry.new('John Doe', %w(John Unknown)),
    ApiAiRuby::Entry.new('Jane Doe', %w(Jane))
]

entity_contacts = ApiAiRuby::Entity.new('contacts', entries_composers)

# let's go
uer = client.create_user_entities_request
uer.create(entity_contacts) # or uer.create([entity1, entity2...])

client.text_request 'call Mozart' # will work

uer.update('contacts', entries_unknown)

client.text_request 'call Mozart' # will NOT work
client.text_request 'call John' # will work

uer.retrieve('contacts') # will return current state of user entity
uer.delete('contacts') # will remove user entities for given session    

Context

Also SDK has full support of contexts API.AI endpoint with special object, called contexts_request Usage is simple:

# some preparations
lifespan = 5
parameters = {
  :param_name => 'param_value'
}
name = 'test_context'

# you can create context using built-in model ApiAiRuby::Context
test_context = ApiAiRuby::Context.new(name, lifespan, parameters)
another_test_context = ApiAiRuby::Context.new('another_test_context')
one_more_test_context = ApiAiRuby::Context.new('one_more_test_context', 4)

# ok, we are ready

context_request = @client.create_contexts_request

# there are different options to be used with .create

context_request.create(test_context)
context_request.create([another_test_context, one_more_test_context])
context_request.create('one_more_super_final_mega_context')

context_request.retrieve('test_context') # will return you single context or nothing
context_request.list() # will return you list of all contexts used in current session
context_request.delete('test_context') # will remove single context
context_request.delete() # will remove all context in session

Timeouts

ApiAiRuby::Client uses the http gem under the hood. You can use timeout_options on the client to set these.

ApiAiRuby::Client.new(
    client_access_token: 'YOUR_ACCESS_TOKEN',
    api_lang: 'FR',
    api_base_url: 'http://example.com/v1/',
    api_version: 'YYYYMMDD',
    api_session_id: 'some_uuid_or_whatever',
    timeout_options: [:global, { write: 1, connect: 1, read: 1 }]
)

Please see the httprb wiki on timeouts for more information.

Error handling

ApiAiRuby::Client currently able to raise two kind of errors: ApiAiRuby::ClientError (due to configuration mismatch) and ApiAiRuby::RequestError in case of something goes wrong during request. For both kind of errors you can get error.message (as usual) and ApiAiRuby::RequestError can additionally give you code of server error (you can get it with error.code)

Changelog

2.0.0

Breaking:

  • http gem dependency updated to 2.0, it does no longer raise Errno::ETIMEDOUT. Thanks to @tak1n

1.3.0

Non-breaking:

Breaking:

  • ApiAiRuby::Client::user_entities_request renamed to ApiAiRuby::Client::create_user_entities_request
  • ApiAiRuby::Entity::addEntry renamed to ApiAiRuby::Entity::add_entry

Previous

  • 1.2.3 - events support
  • 1.2.2 - added configurable timeouts for requests (thanks bramski)
  • 1.2.1 - fixed UTF-8 in text-requests
  • 1.2.0 - added configurable session_id and full userEntities support
  • 1.1.4 - removed unused dependency and updated default API version
  • 1.1.3 - fixed non-correctly serialized parameters in new contexts during query send process
  • 1.1.2 - fixed compatibility with ruby version less then 2.1.6

How to make contributions?

Please read and follow the steps in the CONTRIBUTING.md.

License

See LICENSE.

Terms

Your use of this sample is subject to, and by using or downloading the sample files you agree to comply with, the Google APIs Terms of Service.

This is not an official Google product.

More Repositories

1

dialogflow-nodejs-client

Node.js SDK for Dialogflow
JavaScript
658
star
2

dialogflow-fulfillment-nodejs

Dialogflow agent fulfillment library supporting v1&v2, 8 platforms, and text, card, image, suggestion, custom responses
JavaScript
599
star
3

dialogflow-android-client

Android SDK for Dialogflow
Java
575
star
4

dialogflow-python-client

Python library for Dialogflow
Python
556
star
5

dialogflow-javascript-client

JavaScript Web SDK for Dialogflow
TypeScript
412
star
6

dialogflow-apple-client

iOS SDK for Dialogflow
Objective-C
244
star
7

agent-human-handoff-nodejs

A simple Dialogflow agent, a server, and a web interface that shows an approach for handling text-based conversations between a Dialogflow agent and a human operator
JavaScript
211
star
8

fulfillment-webhook-json

Dialogflow's Fulfillment: Webhook JSON (Requests & Responses)
190
star
9

asr-server

FastCGI support for Kaldi ASR
C++
184
star
10

selfservicekiosk-audio-streaming

A best practice for streaming audio from a browser microphone to Dialogflow or Google Cloud STT by using websockets.
JavaScript
140
star
11

dialogflow-java-client

Java client library for Dialogflow
Java
133
star
12

resources

Links to all Dialogflow libraries and samples
119
star
13

fulfillment-weather-nodejs

Integrating an API with Dialogflow's Fulfillment
JavaScript
81
star
14

fulfillment-firestore-nodejs

Integrating Firebase's Firestore database with Dialogflow
JavaScript
81
star
15

fulfillment-bike-shop-nodejs

Integrating Google Calendar API with Dialogflow's Fulfillment & Knowledge Connectors
JavaScript
79
star
16

dialogflow-dotnet-client

.NET framework for Dialogflow
C#
70
star
17

dialogflow-botkit-client

Botkit library for Dialogflow
JavaScript
62
star
18

dialogflow-java-client-v2

Java client for Dialogflow: Design and integrate a conversational user interface into your applications and devices.
59
star
19

dialogflow-unity-client

Unity library for Dialogflow
C#
54
star
20

fulfillment-translate-python

Python
45
star
21

dialogflow-cordova-client

Cordova library for Dialogflow
Objective-C
42
star
22

api-ai-english-asr-model

Api.ai English Speech Recognition (ASR) Model for Kaldi
36
star
23

fulfillment-actions-library-nodejs

Integrating Actions on Google Client Library with Dialogflow's Fulfillment Library
JavaScript
29
star
24

dialogflow-cpp-client

C++ library for Dialogflow
C++
28
star
25

api-ai-cocoa-swift

Cocoa Swift library
Swift
28
star
26

fulfillment-telephony-nodejs

Sample integrating Telephony, Google Sheets, and Slot Filling with Dialogflow
JavaScript
24
star
27

fulfillment-importer-nodejs

Dialogflow's Importer for Alexa Skills to import a Alexa Skill to Dialogflow
JavaScript
22
star
28

fulfillment-slot-filling-nodejs

Slot Filling with Dialogflow Fulfillment
JavaScript
19
star
29

fulfillment-temperature-converter-nodejs

Sample demonstrating how to make a Dialogflow agent compatible with 9 platforms
JavaScript
19
star
30

fulfillment-faq-nodejs

Integrating Dialogflow's Knowledge Connectors, Phone Gateway, and Actions on Google
JavaScript
17
star
31

dialogflow-xamarin-client

Xamarin SDK for Dialogflow
C#
10
star
32

fulfillment-multi-locale-nodejs

Sample showing how to use multiple languages and locales (e.g. English and French)
JavaScript
10
star
33

city-streets-trivia-nodejs

This sample demonstrates how to create and update developer entities using the Dialogflow Node.js Client and the Dialogflow Fulfillment Library. It also demonstrates how to create session entities from your fulfillment code.
JavaScript
10
star
34

fulfillment-regex-nodejs

Validate Entities with Regular Expressions in Dialogflow's Fulfillment
JavaScript
8
star