Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Dialogflow V2 API from a webpage?

I have a webpage where I want to use dialogflow chatbot. This is a custom chat window, so I don't want to use one click integration. I am able to access the chat agent V1 API using javascript/ajax (by passing client access token in the request header).

But I don't know how to do it in V2 API. The dialogflow documentation is not clear to me(I have setup Authentication by referring this link. I don't know how to proceed further). I'm not familiar with Google cloud either. So a working sample or a step by step how to access the API guideline will be very much appreciated.

like image 413
Marimuthu Avatar asked Jan 11 '18 06:01

Marimuthu


People also ask

How use Dialogflow REST API?

From Postman, create a new Request and select the “Authorization” tab and choose Type "OAuth 2.0". Click 'Get New Access Token'. See the Dialogflow API Rest reference for the full list of APIs you can query. From Postman, enter the API you want to query in the GET field.


1 Answers

You can use Dialogflow Rest APIs, You need to generate access token with Google cloud sdk (scope: cloud platform, dialogflow)

  public df_client_call(request) {
    var config = {
      headers: {
        'Authorization': "Bearer " + this.accessToken,
        'Content-Type': 'application/json; charset=utf-8'
      }
    };   
   return this.http.post(
      'https://dialogflow.googleapis.com/v2/projects/' + environment.project_id +
      '/agent/sessions/' + sessionId + ':detectIntent',
      request,
      config
    )
  }

In the request you have to pass,

{
    queryInput: {
        text: {
            text: action.payload.text,
            languageCode: 'en-US',
        },
    }
}

to trigger Event:,

    {
        queryInput: {
            event: {
                name: action.payload.event,
                languageCode: 'en-US',
            },
        }
    }

sessionId => unique Id for your user

like image 135
Nikhil Savaliya Avatar answered Oct 24 '22 12:10

Nikhil Savaliya