I'm facing a challenge with receiving responses from dialogflow, even though other express calls are functioning properly.
I believe the issue lies within the agents, but I am unsure of the specific problem or how to resolve it. That's why I have turned to this platform for assistance.
Important points to keep in mind:
- This code is being integrated into an existing, tested express application
- Testing has been conducted using Postman
- The Dialogflow fulfillment template webhook can be accessed here
- The console successfully outputs
We got hours!
, but then stops - If you comment out lines
57 through 62 (the if section)
and uncomment line56
, the code responds as expected - The issue is likely within the
agents.hours
function - I have also attempted to use
this.currentlyOpen() === true
Many thanks in advance.
dialogflow.ts
// import { google } from 'googleapis';
const {WebhookClient} = require('dialogflow-fulfillment');
// import { Card, Suggestion } from 'dialogflow-fulfillment';
/**
* @private
* Initialise the ai assist api
*/
export class Agents {
aiassist:any
async initialize (message:any) {
var aiassist = {
info: {
configsuccess: false,
message: message,
data: [],
}
}
process.env.DEBUG = 'dialogflow:debug';
const url = message.headers.configlocation;
await message.core.request({url: url, json: true}, function (error: any, response: { statusCode: number; }, data: any) {
aiassist.info.data = data
if (!error && response.statusCode === 200) {
aiassist.info.configsuccess = true
}
return aiassist
})
this.aiassist = aiassist
this.WebhookProcessing();
}
/**
* @private
* Find the map the agent
*/
WebhookProcessing () {
const agent = new WebhookClient({request: this.aiassist.info.message.full, response: this.aiassist.info.message.res});
let intentMap = new Map();
intentMap.set('Hours', this.hours);
agent.handleRequest(intentMap);
}
/****************
* AGENT *
* DECLARATION *
****************/
/**
* [hours description]
* @param agent [description]
* @return [description]
*/
hours (agent:any) {
console.log("We got hours!")
// agent.add(`We're open now! We close at 17:00 today. Is there anything else I can help you with?`);
if (currentlyOpen(this.aiassist)) { // TypeError: Cannot read property 'aiassist' of undefined
console.log("open!")
agent.add(`We're open now! We close at 17:00 today. Is there anything else I can help you with?`);
} else {
console.log("closed!")
}
}
}
/******************
* FUNCTIONS *
* DECLARATION *
******************/
// Check if currently open - Issues getting "aiassist" into this function
function currentlyOpen (aiassist:any) {
// Get current datetime with proper timezone
console.log("We got currentlyOpen!")
// const date = new Date()
console.log(aiassist.info.data.timeZoneOffset)
// console.log("We finished currentlyOpen")
// date.setHours(date.getHours() + parseInt(agent.this.aiassist.info.data.timeZoneOffset.split(':')[0]));
// date.setMinutes(date.getMinutes() + parseInt(agent.this.aiassist.info.data.timeZoneOffset.split(':')[0][0] + agent.this.aiassist.info.data.timeZoneOffset.split(':')[1]));
// return date.getDay() >= 1 &&
// date.getDay() <= 5 &&
// date.getHours() >= agent.this.aiassist.info.data.businessSetings.openTime &&
// date.getHours() <= agent.this.aiassist.info.data.businessSetings.closeTime;
return true
}
TypeError: Cannot read property 'aiassist' of undefined
File "C:\Users\sjona\Desktop\TSC\repo\curr\built\routes\v1\dialogflow.js", line 53, col 32, in hours
if (currentlyOpen(this.aiassist)) {
File "C:\Users\sjona\Desktop\TSC\repo\curr\node_modules\dialogflow-fulfillment\src\dialogflow-fulfillment.js", line 313, col 44, in WebhookClient.handleRequest
let result = handler.get(this.intent)(this);
File "C:\Users\sjona\Desktop\TSC\repo\curr\built\routes\v1\dialogflow.js", line 39, col 15, in Agents.WebhookProcessing
agent.handleRequest(intentMap);
File "C:\Users\sjona\Desktop\TSC\repo\curr\built\routes\v1\dialogflow.js", line 29, col 14, in Agents.initialize
this.WebhookProcessing();
UPDATE: Revised code to align with comments. Noted where challenges are arising.