如何使用 Recast.ai 使用 message.addReply 添加基于承诺的 API 调用的结果?

How to add results from a promise based API call with message.addReply using Recast.ai?

我正在制作一个根据位置搜索餐馆的机器人。任何人都可以帮助我为什么这没有出现在 FB Messenger 中?:

restaurants(result.getMemory('location').raw)
.then(res=>{

  message.addReply(res);
  message.reply();

 });
}

对餐厅函数的调用 returns 来自 YELP API 调用(餐厅数组)的结果,但是当我将其添加为对消息的回复时,没有任何反应在 FB 信使中。

这是 message.js 的完整代码:

    const recastai = require('recastai');

    const restaurants = require('./restaurants');

     // This function is the core of the bot behaviour
    const replyMessage = (message) => {
     // Instantiate Recast.AI SDK, just for request service
     const request = new recastai.request(process.env.REQUEST_TOKEN, 
    process.env.LANGUAGE);
   // Get text from message received
   const text = message.content;

    console.log('I receive: ', text);

  // Get senderId to catch unique conversation_token
  const senderId = message.senderId;

  // Call Recast.AI SDK, through /converse route
  request.converseText(text, { conversationToken: senderId })
  .then(result => {

    //Recast takes text analyses that, returns a result object, generates replies adds messages to reply stack and then sends the replies

    //Call Yelp API with when the intent is Location. When Yelp returns result we add it to the result.replies array. 
    //Then we add everything in result.replies to the messaging queue that sends the responses to FB


    if (result.action) {

      console.log('The conversation action is: ', result.action.slug);

    }

    // If there is not any message return by Recast.AI for this current conversation
    if (!result.replies.length) {
      message.addReply({ type: 'text', content: 'I don\'t have the reply to this yet :)' });
    } else {
      // Add each reply received from API to replies stack
      result.replies.forEach(replyContent => message.addReply({ type: 'text', content: replyContent }));
    }

    // Send all replies
    message.reply()
    //send initial reply generated by Recast first
    .then(() => {
    //call restaurant function that returns a list of results from API  
    //if the action is location and done
      if(result.action && result.action.slug === 'location' && result.action.done){

        restaurants(result.getMemory('location').raw)
          .then(res=>{

            console.log(res);

            message.addReply(res);
            message.reply();

          });

      }

    })
    .catch(err => {
      console.error('Error while sending message to channel', err);
    });
  })
  .catch(err => {
    console.error('Error while sending message to Recast.AI', err);
  });
};

module.exports = replyMessage;

这里是我的 restaurants.js 代码,它被导入到 message.js 文件中用于机器人行为:

const rp = require('request-promise');

// Load configuration
require('./config');

const restaurants = (location) => {
  return Promise.all([
    yelpCall(location)
  ]).then(result => {

    //result contains the return value from Yelp call

    return result;

  });
};

const yelpCall = (location) => {

  const auth = {
    method: 'POST',
    url: 'https://api.yelp.com/oauth2/token?grant_type=client_credentials&client_id='+ process.env.YELP_APP_ID +'&client_secret='+process.env.APP_SECRET
  };

  return rp(auth)
    .then(result => {
    const tokens = JSON.parse(result);
    return tokens;

  })
  .then(result=>{

    const options = {
      url: 'https://api.yelp.com/v3/businesses/search?location=' + location + "&term=thai",
      headers: {Authorization: "Bearer " + result.access_token}  
    };

    return rp(options).then(findings =>{

      return findings;

    });

  });

};

module.exports = restaurants;

一些想法:

  1. message.reply 是可行的,因此 return message.reply() 在两个地方。
  2. request.converseText() 是可行的,因此 return request.converseText(...).
  3. restaurants 是可行的,因此 return restaurants(...).
  4. message.js 中,message.addReply() 在两个地方传递了形式为 {type:..., content:...} 的对象,但最后只是 res。对吗?
  5. restaurants.js中,Promise.all()似乎是不必要的。它将导致其结果被包装在一个数组中。 module.exports = location => yelpCall(location); 似乎更合适。