Dialogflow&Express-实现

时间:2019-09-16 21:23:15

标签: typescript express google-api dialogflow dialogflow-fulfillment

当其他快速通话正常工作时,我无法从dialogflow获取响应。

我知道这是代理商的问题,但是我不确定这个问题是什么或如何解决,这就是为什么我要在这里询问。

要注意的重点。

  • 此代码已添加到现有的经过测试的Express应用程序中
  • 使用postman
  • 进行测试
  • 对话框流填充模板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();

编辑: 更新了代码以匹配注释。在出现问题的地方添加注释。

1 个答案:

答案 0 :(得分:0)

问题在于,this不是您想像的(或想要的),这是由于hours()的调用方式以及this的含义不同所致叫做。 This page详细介绍了详细内容,但总之(适用于您):

  • 被调用的函数已将this应用于全局对象,而不是应用于this的词法绑定版本(即-this的值在班级内
  • 要获取this的词法绑定版本,您需要使用bind()将值绑定到函数,或使用箭头函数进行调用。

在您的情况下,这意味着您应该使用类似的方法注册Intent处理程序

intentMap.set('Hours', agent => this.hours(agent));