Skip to content

Latest commit

 

History

History
119 lines (93 loc) · 3.51 KB

ruby-client-for-dialogflow-api.md

File metadata and controls

119 lines (93 loc) · 3.51 KB

Ruby Client for Dialogflow API

Google cloud provides Ruby Client for Dialogflow API. Before using this gem you may need to create an agent and get special credentials.

Gem Installation

Add this line to your Gemfile

gem 'google-cloud-dialogflow'

Then create config/initializers/google_cloud_dialogflow.rb

require 'google/cloud/dialogflow/v2'

Gem Usage

This is a class example which allows developers to detect intent by text or event. It requires the following arguments

  • keyfile_json - content of keyfile.json file. See this article to check how to get it
  • parameters - hash of configurations. It allows you to set following parameters:
    • :language_code - for example en-GB
    • :environment - Dialogflow environment to use. Leave it blank if you don't use this feature
    • :contexts - array of contexts to be attached to the request
  • session - unique string, e.g. Twilio call Sid
  • utterance - user's input or event name which you are going to use to detect intent
module Dialogflow
  class SessionsClient
    def initialize(keyfile_json:, parameters: [], session:, utterance:)
      self.keyfile_json = keyfile_json
      self.parameters = parameters
      self.session = session
      self.utterance = utterance
    end

    def detect_text_intent
      @detect_text_intent ||= sessions_client.detect_intent(
        session_string,
        {
          text: {
            text: utterance,
            language_code: language_code
          }
        },
        query_params: { contexts: contexts }
      )
    end

    def detect_event_intent
      @detect_event_intent ||= sessions_client.detect_intent(
        session_string,
        {
          event: {
            name: utterance,
            language_code: language_code
          }
        },
        query_params: { contexts: contexts }
      )
    end

    private

    attr_accessor :keyfile_json, :parameters, :session, :utterance

    def environment_string
      environment_enabled? ? "environments/#{environment}/users/-/" : ''
    end

    def environment_enabled?
      parameters[:environment].present?
    end

    def environment
      parameters[:environment].presence || '-'
    end

    def language_code
      parameters[:language_code]
    end

    def session_string
      "projects/#{keyfile['project_id']}/agent/#{environment_string}" \
        "sessions/#{session}"
    end

    def keyfile
      @keyfile ||= JSON.parse(keyfile_json)
    end

    def contexts
      parameters[:contexts].to_a.map do |context|
        {
          lifespan_count: 1,
          name: "#{session_string}/contexts/#{context}",
          parameters: {}
        }
      end
    end

    def sessions_client
      @sessions_client = Google::Cloud::Dialogflow::V2::SessionsClient.new(
        credentials: keyfile
      )
    end
  end
end

Both methods detect_text_intent and detect_event_intent will return instance of Google::Cloud::Dialogflow::V2::DetectIntentResponse. So, to get response parameters such as whisper_message, you can use:

result.query_result.parameters.fields['whisper_message'].string_value

Other Links

For more information visit https://googleapis.github.io/google-cloud-ruby/docs/google-cloud-dialogflow/latest/Google/Cloud/Dialogflow/V2/SessionsClient.html