2

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.

Sairaj Sawant
  • 1,842
  • 1
  • 12
  • 16
TK06
  • 21
  • 5
  • A simple google search for dialog flow and angular turned this up: https://www.kommunicate.io/blog/integrate-dialogflow-angular-js/ give it a try and see if that works for you – camba1 Jul 28 '19 at 19:08
  • for this you need a https://www.kommunicate.io/ which not what i'm looking for. – TK06 Jul 28 '19 at 21:54

1 Answers1

1

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);
            },
        );
    });
},
Nikhil Savaliya
  • 2,138
  • 4
  • 24
  • 45
  • Thanks a lot for you answer, however I'm working with cloud functions deployed to firebase, and I don't want my user to be logged! do you have an idea on how to achieve it ? – TK06 Jul 30 '19 at 06:21
  • session id is something that just need to be unique for user you can define – Nikhil Savaliya Jul 30 '19 at 08:26
  • ok great understood, just a last question. I have some issues installing gcloud sdk to generate the access token, is there any package like in nodejs to generate the token ? https://gist.github.com/weikangchia/d5bb0aa1466407c86692ba00d8f16332 ? thanks so much – TK06 Jul 31 '19 at 09:37
  • thank for your help. For this we will need to set up another server while we are working on firebase. I will try to install gcloud and give it a try. You can see an example of react-native implementation https://www.npmjs.com/package/react-native-dialogflow ! Thanks for your help :) – TK06 Jul 31 '19 at 10:11
  • @NikhilSavaliya do you any demo for the same. – Gauri deshmukh Oct 29 '20 at 06:40
  • https://chatbotslife.com/dialogflow-v2-rest-api-communication-6cf7ab66ab36 – Nikhil Savaliya Nov 05 '20 at 07:28