0

I have created a webhook for WhatsApp Chatbot using NodeJS following this online article: https://dev.to/newtonmunene_yg/creating-a-whatsapp-chatbot-using-node-js-dialogflow-and-twilio-31km

The webhook is linked to Twilio Sandbox for WhatsApp.

I have also provided the DialogFlow Admin API permission to service account on Google Cloud Platform.

When I send a new message from WhatsApp, it's received on Twilio and the webhook is triggered, but I am getting the following error on the console on my local machine.

"Error: 7 PERMISSION_DENIED: IAM permission 'dialogflow.sessions.detectIntent' on 'projects/xxxx-xxx-xxxx/agent' denied." 

I am using Ngrok to tunnel the localhost build to the web and using that URL as the webhook URL in Twilio.

We have a client demo for this feature, so any quick help is appreciated. I am placing my dialog flow code and controller code below

dialogflow.ts

const dialogflow = require("dialogflow");
const credentials = require("../../credential-new.json");

const sessionClient = new dialogflow.SessionsClient({
  credentials: credentials
});
const projectId: string = process.env.DIALOGFLOW_PROJECT_ID!;

export const runQuery = (query: string, number: string) => {
  return new Promise(async (resolve, reject) => {
    try {
      // A unique identifier for the given session
      //const sessionId = uuid.v4();
      const sessionId = number;
      // Create a new session

      const sessionPath = sessionClient.sessionPath(projectId, sessionId);

      // The text query request.
      const request = {
        session: sessionPath,
        queryInput: {
          text: {
            // The query to send to the dialogflow agent
            text: query,
            // The language used by the client (en-US)
            languageCode: "en-US"
          }
        }
      };

      // Send request and log result
      const responses = await sessionClient.detectIntent(request);

      const result = responses[0].queryResult;

      resolve(result);
    } catch (error) {
      reject(error);
    }
  });
};
Vishal K
  • 1,368
  • 1
  • 7
  • 15
Puttu
  • 33
  • 1
  • 9

2 Answers2

1

This issue got resolved by creating a new account on DialogFlow and providing new API key.

Puttu
  • 33
  • 1
  • 9
0

I think the problem is with the service account. Make sure you use the same email which is registered with Dialogflow and GCP and then create a service account and also make sure the service account email is present in the credential-new.json file is the same service account which has the Dialog Flow Admin Role and also check you have given the valid path to access credential-new.json file in the code.

You can safely do this by going to the settings menu on Dialogflow and then clicking on the project id, it will take you to the correct place.

Also, there may be a possibility that you forget to enable the Dialogflow API from the API section on GCP.

Vishal K
  • 1,368
  • 1
  • 7
  • 15
Raj
  • 51
  • 1
  • 6