TK06
TK06

Reputation: 21

How to integrate Dialogflow v2 with angular 7

I am building a chatbot using Dialogflow, and I deployed the cloud function using inline editor to firebase.

However, compared to V1 there is no javascript SDK to interact with the API. I'm stuck and I cannot find anything helpful in the docs.

Can you please share some example code on how to interact with the dialogflow API using angular ou any j framework ?

Thanks !

I tried to read the dialogflow doc especially the part about http requests. I tried some code with simple http post request which didn't work.

There is no concrete example on stackoverflow.

Upvotes: 2

Views: 2064

Answers (1)

Nikhil Savaliya
Nikhil Savaliya

Reputation: 2166

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',
        },
    }
}

sessionId => unique Id for your user

const googleAuth = require('google-oauth-jwt');

getToken: async function() {
    return new Promise((resolve) => {
        googleAuth.authenticate(
            {
                email: config.googleClientEmail,
                key: config.googlePrivateKey,
                scopes: ['https://www.googleapis.com/auth/cloud-platform'],
            },
            (err, token) => {
                resolve(token);
            },
        );
    });
},

Upvotes: 1

Related Questions