Error: No responses defined for platform: null when listing users google calendar events

Error: No responses defined for platform: null when listing users google calendar events

我正在尝试在节点 js 中实现提醒 dialogflow 代理,提醒用户 google 日历即将发生的事件。但是,在调用列出即将发生的事件的意图时,我收到了一个 No responses defined for platform: null 错误。

这是我的代码:

const express = require('express');
const google = require('googleapis').google;
const jwt = require('jsonwebtoken');
const dfff = require('dialogflow-fulfillment')
const {googlec} = require('googleapis');

// Google's OAuth2 client
const OAuth2 = google.auth.OAuth2;
// Including our config file
const CONFIG = require('./config');
// Creating our express application
const app = express();
// Allowing ourselves to use cookies
const cookieParser = require('cookie-parser');
app.use(cookieParser());
// Setting up EJS Views
app.set('view engine', 'ejs');
app.set('views', __dirname);
console.log(dfff)

app.get('/', function (req, res) {
    // Create an OAuth2 client object from the credentials in our config file
    const oauth2Client = new OAuth2(CONFIG.oauth2Credentials.client_id, CONFIG.oauth2Credentials.client_secret, CONFIG.oauth2Credentials.redirect_uris[0]);
    // Obtain the google login link to which we'll send our users to give us access
    const loginLink = oauth2Client.generateAuthUrl({
      access_type: 'offline', // Indicates that we need to be able to access data continously without the user constantly giving us consent
      scope: CONFIG.oauth2Credentials.scopes // Using the access scopes from our config file
    });
    return res.render("index", { loginLink: loginLink });
  });


app.get('/auth_callback', function (req, res) {
    // Create an OAuth2 client object from the credentials in our config file
    const oauth2Client = new OAuth2(CONFIG.oauth2Credentials.client_id, CONFIG.oauth2Credentials.client_secret, CONFIG.oauth2Credentials.redirect_uris[0]);
    if (req.query.error) {
      // The user did not give us permission.
      return res.redirect('/error');
    } else {
      oauth2Client.getToken(req.query.code, function(err, token) {
        if (err)
          return res.redirect('/');
  
        // Store the credentials given by google into a jsonwebtoken in a cookie called 'jwt'
        res.cookie('jwt', jwt.sign(token, CONFIG.JWTsecret));
        return res.redirect('/');
      });
    }
  });

  app.post('/', express.json(),(req,res)=>{
    //if (!req.cookies.jwt) {
      // We haven't logged in
      //return res.redirect('/');
    //}
    const oauth2Client = new OAuth2(CONFIG.oauth2Credentials.client_id, CONFIG.oauth2Credentials.client_secret, CONFIG.oauth2Credentials.redirect_uris[0]);
    const calendar = google.calendar({version: 'v3' , auth:oauth2Client});
    const agent = new dfff.WebhookClient({
        request : req,
        response : res
      })
      function welcome(agent){
        agent.add("Hi")
      }
    
      function listEvents(agent){ 
        calendar.events.list({
        'calendarId': 'primary',
        'auth':oauth2Client,
        'timeMin': (new Date()).toISOString(),
        'showDeleted': false,
        'singleEvents': true,
        'maxResults': 10,
        'singleEvents': true,
        'orderBy': 'startTime'
     }).then((err,response)=> {
        let events = response.result.items;
        if (events.length > 0) {
          for (let i = 0; i < events.length; i++) {
            var event = events[i];
            var when = event.start.dateTime;
            if (!when) {
              when = event.start.date;
            }
             return agent.add('Be ready for '+ event.summary + ' event '+ 'at ' + when )
          }}
        else {
             return agent.add('You dont have any upcoming events.');
        }
      });   
         
      }
    
      let intenMap = new Map()
      intenMap.set('Default_Welcome_Intent',welcome)
      intenMap.set('Remind_me',listEvents)

      agent.handleRequest(intenMap)
   
  });

  // Listen on the port defined in the config file
app.listen(CONFIG.port, function () {
    console.log(`Listening on port ${CONFIG.port}`);
  });

每当处理 listEvents 函数时,我都会收到(错误:没有为平台 null 定义的响应)知道为什么吗?

问题是 listEvents() 执行一个异步操作,该操作 return 是一个 Promise(对 calendar.events.list() 的调用),您通过 .then() 块处理它,但是您没有办法让 agent.handleRequest() 函数知道这一点并等待 Promise 完成。为此,您需要 return 一个 Promise。

幸运的是,在你的情况下,解决方案很简单,因为调用和 then() 链 return 一个 Promise,你可以 return 它。这可以通过在 calendar.events.list() 之前添加 return 关键字来完成。它可能看起来像这样:

      function listEvents(agent){ 
        return calendar.events.list({
          // Parameters go here
        }).then((err,response)=> {
          // Code to handle response and return a message go here
        });   
      }