【发布时间】:2016-11-25 20:30:54
【问题描述】:
我正在实现一个使用 Facebook 发送 API 的机器人。根据documentation,可以使用请求发送文件。该文档提供了两种方法,一种是向文件发送 URL,另一种是上传文件。我不想上传文件并给它一个 URL,因为这是一个开源库,不想假设任何关于实现的东西。
我确实想直接上传文件。上传文件的文档以cURL为例,如下所示:
curl \
-F recipient='{"id":"USER_ID"}' \
-F message='{"attachment":{"type":"file", "payload":{}}}' \
-F filedata=@/tmp/receipt.pdf \
"https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"
我目前的看法是它应该看起来像这样:
facebook_message.access_token = configuration.access_token;
var fileReaderStream = fs.createReadStream('./sampleData.json')
var formData = {
"recipient": JSON.stringify({
"id":message.channel
}),
"attachment": JSON.stringify({
"type":"file",
"payload":{}
}),
"filedata": fileReaderStream
}
request({
"method": 'POST',
"json": true,
"formData": formData,
"uri": 'https://graph.facebook.com/v2.6/me/messages?access_token=' + configuration.access_token
},
function(err, res, body) {
//***
});
当我运行它时,我收到以下响应:
{
message: '(#100) Must send either message or state',
type: 'OAuthException',
code: 100,
error_subcode: 2018015,
fbtrace_id: '***'
}
【问题讨论】:
-
你在你的问题中遗漏了一些非常重要的东西:究竟什么不起作用?你调试了吗?回调中有任何信息吗?它被调用了吗? “这是我的解决方案,但它不起作用”对于stackoverflow来说有点过于宽泛了。
-
现在添加。谢谢。
标签: javascript facebook npm-request facebook-send-api