我正在制作一个可以根据位置搜索餐厅的机器人。谁能帮我为什么这不出现在FB Messenger中?

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

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

 });
}


对餐厅函数的调用返回了YELP API调用(一组餐厅)的结果,但是当我将其添加为对消息的答复时,FB Messenger中没有任何反应。

这是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;


这是我的restaurant.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;

最佳答案

一些想法:


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

关于javascript - 如何使用Recast.ai使用message.addReply从基于 promise 的API调用添加结果?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43627320/

10-17 02:50