作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个根据位置搜索餐馆的机器人。任何人都可以帮助我为什么这不会出现在 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;
这是我的 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;
最佳答案
一些想法:
message.reply
是可用的,因此 在两个地方返回 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/
我正在制作一个根据位置搜索餐馆的机器人。任何人都可以帮助我为什么这不会出现在 FB Messenger 中?: restaurants(result.getMemory('location').raw
我是一名优秀的程序员,十分优秀!