Search code examples
firebasegoogle-cloud-functionsdialogflow-esactions-on-googledialogflow-es-fulfillment

Google Cloud functions is not getting called in Dialogflow webhook


I am creating an Google Assistant action in which i have used an firebase cloud function and deployed the code to firebase cloud functions.

I have copied the webhook url and pasted it in Dialogflow console as well as enabled the Webhook call for the Default Welcome Intent.

When i run the code i am getting the error like below

enter image description here

Here is the cloud function code

const { dialogflow, Suggestions } = require("actions-on-google");

const functions = require("firebase-functions");

const app = dialogflow({ debug: true });

app.intent("Default Welcome Intent", conv => {
  if (conv.user.last.seen) {
    conv.ask(`Welcome back to Standup Sheet`);
  } else {
    const ssml = `<speak>Welcome to Standup Sheet. I will assist you to know about your teams daily task as well the over all time taken for each project.</speak>`;
    conv.ask(ssml);
  }
  conv.ask(
    new Suggestions(["Todays entries", "Yesterday entries", "Team", "Projects"])
  );
});

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

Package.json file

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "dependencies": {
    "actions-on-google": "^2.12.0",
    "firebase-admin": "^8.0.0",
    "firebase-functions": "^3.0.0"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.1.6"
  },
  "private": true
}

Logs from Firebase cloud function

dialogflowFirebaseFulfillment
{
    "@type":"type.googleapis.com/google.cloud.audit.AuditLog",
    "authenticationInfo":{
      "principalEmail":"nidhinkumar06@gmail.com"
    },
    "requestMetadata":{
        "callerIp":"2409:4072:9e:14e2:ddf4:6a38:779f:f147",
        "callerSuppliedUserAgent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36,gzip(gfe)",
        "requestAttributes":{
            "time":"2020-03-31T17:07:39.032Z",
            "auth":{}
        },
        "destinationAttributes":{}
    },
    "serviceName":"cloudfunctions.googleapis.com",
    "methodName":"google.cloud.functions.v1.CloudFunctionsService.UpdateFunction",
    "authorizationInfo":[
        {
            "resource":"projects/standup-sheet/locations/us-central1/functions/dialogflowFirebaseFulfillment",
            "permission":"cloudfunctions.functions.update",
            "granted":true,
            "resourceAttributes":{}
        }
    ],
    "resourceName":"projects/standup-sheet/locations/us-central1/functions/dialogflowFirebaseFulfillment",
    "request":{
        "updateMask":"description,entryPoint,timeout,availableMemoryMb,labels,environmentVariables,runtime,httpsTrigger,maxInstances,ingressSettings,vpcConnector,serviceAccountEmail,sourceUploadUrl",
        "@type":"type.googleapis.com/google.cloud.functions.v1.UpdateFunctionRequest",
        "function":{
            "ingressSettings":"ALLOW_ALL",
            "httpsTrigger":{},
            "serviceAccountEmail":"standup-sheet@appspot.gserviceaccount.com",
            "entryPoint":"dialogflowFirebaseFulfillment",
            "labels":{
                "deployment-tool":"console-cloud"
            },
            "sourceUploadUrl":"https://storage.googleapis.com/gcf-upload-us-central1-6a75406d-6f0b-41ee-aeb5-e264da4377bc/22bd91a1-48f6-4daa-981a-e76122332e1c.zip?GoogleAccessId=service-1064672465268@gcf-admin-robot.iam.gserviceaccount.com&Expires=1585676258&Signature=QqhcwyJXqap%2BWXvJ6Xu2hqINSaw5uNAdRadrfIFAyXEd%2F5DTkKeEmUBjlOWrcHOrbdR6lXzgLeiIERBGHud4sZ5FQSWHOJ6HBJ3OHTI0PhEuUPBhEyuGSn%2FqI3kav9QHf5tETNw2L1DXvUw0oU%2BcD6V0MSwC35D4pGCUPoqvxW38W0q7Yz0bXm26Wnlk3HwTm%2FjP2OwbQEH5Ae5mntn%2BdQHH4n2RFjXyg2JIIDVBe1f1rOzSh0vDL4Eg6Fc9n7HMfOSfJ04RoqnAj1gUUZo38uQSVfIfjVjSqEjbR%2BGvF8E2lw5CUX%2FS8BKeqq4hvSpTUmvdmGJe0qGt8ohBq524Lw%3D%3D",
            "timeout":"60s",
            "availableMemoryMb":256,
            "name":"projects/standup-sheet/locations/us-central1/functions/dialogflowFirebaseFulfillment",
            "runtime":"nodejs8"
        }
    },
    "resourceLocation":{
        "currentLocations":["us-central1"]
    }
}

Solution

  • Now the code is working now done below steps to make it work. If anyone faces this kind of issue pls check the below steps.

    Root cause #1

    In Dialogflow console i have removed all the default response in the Default Welcome Intent that causes the above error. So i added a default response and tested the action which works fine. But the response from the cloud function was not shown.

    Root cause #2

    Tried to use the inline editor in Dialogflow and check whether that function is getting called or not. Tried to enable the inline editor but it doesn't enable stating Google Cloud needs to be provisioned, pls refresh the page. Looked the GCP console and compared other actions which i have created every action has storage but this action doesn't so i have created storage in Firebase after enabling the Storage Inline editor was enabled (Raised a Query in Dialogflow to confirm whether it is because of Storage does the inline editor is not opened).

    Solution 1

    Once the inline editor is opened pasted the above code and the package.json file and deployed the code and tested it works as expected

    Solution 2

    Tried to deploy the same code from my machine and enabled Webhook instead of the inline editor and tested the action works as expected

    Solution 3

    Changed the exports dialogflowFirebaseFulfillment name to fulfillment and deployed the code and tested it doesn't work so compared both the functions and found that the fulfillment function doesn't have the permission like the below image

    enter image description here

    If you notice in the above image there is a role named allUsers whereas in the fulfillment function it doesn't have the allUsers role

    enter image description here

    As well as if you look at the functions dashboard(dialogflowFirebasefulfillment) you can notice the difference

    enter image description here

    fulfillment(dashboard)

    enter image description here

    To enable the permission for it click on the info panel and click Add Members and add allUsers like below

    enter image description here

    Once it is done add the role for allUsers by clicking the Cloud Functions and Cloud Functions Invoker and click save.

    enter image description here

    Now you can see the updated dashboard like below

    enter image description here

    Now if you test the action it will call the cloud function.