0

First off, I'm new to Google Assistant so I have very little idea about what I am doing. I am trying to make a webhook request with the code below in an external js file on a webserver:

// Project Requirements
const { conversation } = require('@assistant/conversation');
const functions = require('firebase-functions');

// Constructor
const app = conversation();

// Search Function
app.handle('contacctSearch', async conv => {

// Get Intent Parameters
const query = $session.params.contactName.original;
const pageType = $session.params.pageType.original;
if (pageType.toUpperCase() == 'WHITE PAGES') {
    const res = await fetch(`https://www.findyello.com/barbados/white-pages/?search=${query}`);
    console.log(res);
    // Parse res into text
    const text = res;
    conv.add(`Here is your first result. ${text}`);
} 

else if (pageType.toUpperCase() =='YELLOW PAGES') {
    const res = await fetch(`https://www.findyello.com/barbados/?search=${query}`);
    console.log(res);
    // Parse res into text
    const text = res;
    conv.add(`Here is your first result. ${text}`);
} 

else if (pageType.toUpperCase() =='GOVERNMENT PAGES') {
    const res = await fetch(`https://www.findyello.com/barbados/government/?search=${query}`);
    console.log(res);
    // Parse res into text
    const text = res;
    conv.add(`Here is your first result. ${text}`);
}
});

But, I am receiving an error: Invalid response from webhook: Failed to translate JSON to ExecuteHttpResponse..

{
"responseJson": "// console.log('Working!');\r\n\r\n// Project Requirements\r\nconst { conversation } 
= require('@assistant/conversation');\r\nconst functions = require('firebase-functions');\r\n\r\n// 
Constructor\r\nconst app = conversation();\r\n\r\n// Search Function\r\napp.handle('contacctSearch', 
async conv => {\r\n\r\n    // Get Intent Parameters\r\n    const query = 
$session.params.contactName.original;\r\n    const pageType = $session.params.pageType.original;\r\n    
if (pageType.toUpperCase() == 'WHITE PAGES') {\r\n        const res = await 
fetch(`https://www.findyello.com/barbados/white-pages/?search=${query}`);\r\n        
console.log(res);\r\n        // Parse res into text\r\n        const text = res;\r\n        
conv.add(`Here is your first result. ${text}`);\r\n    } \r\n    \r\n    else if 
(pageType.toUpperCase() =='YELLOW PAGES') {\r\n        const res = await 
fetch(`https://www.findyello.com/barbados/?search=${query}`);\r\n        console.log(res);\r\n        
// Parse res into text\r\n        const text = res;\r\n        conv.add(`Here is your first result. 
${text}`);\r\n    } \r\n    \r\n    else if (pageType.toUpperCase() =='GOVERNMENT PAGES') {\r\n        
const res = await fetch(`https://www.findyello.com/barbados/government/?search=${query}`);\r\n        
console.log(res);\r\n        // Parse res into text\r\n        const text = res;\r\n        
conv.add(`Here is your first result. ${text}`);\r\n    }\r\n    });\r\n"
}

Any help would be amazing!

Jose Luis Delgadillo
  • 2,348
  • 1
  • 6
  • 16
  • Is this your entire file? What is the filename? – Nick Felker Feb 01 '21 at 17:27
  • @NickFelker Yes, this is my entire file and I have it linked to https://simplifiedapps.co/ga_barbados/js/google-assistant-updated-contact-search.js. Not sure why it isn't working at all. – kkirpalani90 Feb 02 '21 at 13:28

1 Answers1

0

It seems to me that your file is not being understood as code, but rather a plain text file. There are several things to change:

  1. Be sure to declare a Firebase Function at the bottom of your file:
app.handle('contacctSearch', async conv => {
  // ...
})

exports.fulfillment = app
  1. It seems like you're pointing your webhook to your textfile rather than ensuring it is available as something executable. You should make sure you deploy your function, which you're using Firebase Functions, then use the webhook URL that Firebase gives you:
firebase deploy --only functions

It will be a function in the format, with your project's ID:

https://us-central1-MY_PROJECT_ID.cloudfunctions.net/fulfillment
Nick Felker
  • 11,536
  • 1
  • 21
  • 35