Dialogflow & Express——实现
Dialogflow & Express -- Fulfilment
我在从 dialogflow 获取响应时遇到问题,而其他 express 调用工作正常。
我知道这是代理的问题,但我不确定问题是什么或如何解决,这就是我在这里提问的原因。
注意事项
- 正在将此代码添加到已测试的现有 Express 应用程序中
- 一直在使用 postman
进行测试
- 可以找到 Dialogflow fullfillment 模板 webhook here
- 控制台成功输出
We got hours!
然后没有继续
- 如果您注释行
57 through 62 (the if section)
并取消注释行 56
,代码会按预期响应
- 问题预计在
agents.hours
函数中
- 也试过
this.currentlyOpen() === true
非常感谢。
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();
编辑:
更新代码以匹配评论。在出现问题时添加注释。
问题是 this
不是您认为的(或希望的),因为 hours()
的调用方式以及 this
的不同含义取决于它是怎么称呼的。 This page 深入细节,但简而言之(适用于您):
- 被调用的函数
this
应用于 全局对象 ,而不是 this
的词法绑定版本(即 - 的值this
里面有一个 class
- 要获得
this
的词法绑定版本,您需要使用 bind()
将值绑定到函数,或使用箭头函数进行调用。
在您的情况下,这意味着您应该使用类似
的方式注册 Intent Handler
intentMap.set('Hours', agent => this.hours(agent));
我在从 dialogflow 获取响应时遇到问题,而其他 express 调用工作正常。
我知道这是代理的问题,但我不确定问题是什么或如何解决,这就是我在这里提问的原因。
注意事项
- 正在将此代码添加到已测试的现有 Express 应用程序中
- 一直在使用 postman 进行测试
- 可以找到 Dialogflow fullfillment 模板 webhook here
- 控制台成功输出
We got hours!
然后没有继续 - 如果您注释行
57 through 62 (the if section)
并取消注释行56
,代码会按预期响应 - 问题预计在
agents.hours
函数中 - 也试过
this.currentlyOpen() === true
非常感谢。
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();
编辑: 更新代码以匹配评论。在出现问题时添加注释。
问题是 this
不是您认为的(或希望的),因为 hours()
的调用方式以及 this
的不同含义取决于它是怎么称呼的。 This page 深入细节,但简而言之(适用于您):
- 被调用的函数
this
应用于 全局对象 ,而不是this
的词法绑定版本(即 - 的值this
里面有一个 class - 要获得
this
的词法绑定版本,您需要使用bind()
将值绑定到函数,或使用箭头函数进行调用。
在您的情况下,这意味着您应该使用类似
的方式注册 Intent HandlerintentMap.set('Hours', agent => this.hours(agent));