Node.js 通过NodeJS将文件加载到facebook messenger时出现问题

Node.js 通过NodeJS将文件加载到facebook messenger时出现问题,node.js,facebook,facebook-graph-api,request,chatbot,Node.js,Facebook,Facebook Graph Api,Request,Chatbot,我正在使用NodeJS为facebook构建一个聊天机器人,我很难通过facebook的API通过messeger发送本地文件,根据要执行文件加载,必须进行远程调用,如下例所示: curl \ -F 'message={"attachment":{"type":"image", "payload":{"is_reusable":true}}}' \ -F 'filedata=@/tmp/shirt.png;type=image/png' \ "https://graph.faceb

我正在使用NodeJS为facebook构建一个聊天机器人,我很难通过facebook的API通过messeger发送本地文件,根据要执行文件加载,必须进行远程调用,如下例所示:

curl  \
  -F 'message={"attachment":{"type":"image", "payload":{"is_reusable":true}}}' \
  -F 'filedata=@/tmp/shirt.png;type=image/png' \
  "https://graph.facebook.com/v2.6/me/message_attachments?access_token=<PAGE_ACCESS_TOKEN>"
我不是Node/JavaScript方面的专家,所以我可能犯了一些愚蠢的错误。。。无论如何,下面是我负责组装对象并将其发送到facebook的代码片段。欢迎任何帮助

function callSendAPI(messageData) {
    request({
        url: 'https://graph.facebook.com/v2.6/me',
        qs : { access_token: TOKEN },
        method: 'POST',
        json: messageData
    }, function(error, response, body) {
        if (error) {
            console.log(error);
        } else if (response.body.error) {
            console.log(response.body.error);
        }
    })
}

function sendAttachment(recipientID) {
    var messageData = {
        recipient: {
            id: recipientID
        },
        message: {
            attachment: {
                type: 'file', 
                payload: {
                    'is_reusable': true,
                }
            }
        },
        filedata: '@pdf_exemple.pdf;type=application/pdf'
    };
    callSendAPI(messageData);
}

经过大量搜索,我能够对我的应用程序的方法进行必要的更改,使其能够通过messeger传输文件,这个概念几乎是正确的,错误的是数据的发送方式,正确的是通过表单发送。以下是解决方案:

function callSendAPI(messageData, formData) {
    request({
        url: 'https://graph.facebook.com/v2.6/me',
        qs : { access_token: TOKEN },
        method: 'POST',
        json: messageData,
        formData: formData,
    }, function(error, response, body) {
        if (error) {
            console.log(error);
        } else if (response.body.error) {
            console.log(response.body.error);
        }
    })
}

function sendAttachment(recipientID, fileName) {
    var fileReaderStream = fs.createReadStream(fileName)
    var formData = {
                recipient: JSON.stringify({
                id: recipientID
            }),
            message: JSON.stringify({
            attachment: {
                type: 'file',
                payload: {
                    is_reusable: false
                }
            }
        }),
       filedata: fileReaderStream
    }
    callSendAPI(true, formData);
}

-F'filedata=@/tmp/shirt.png;type=image/png'
-此@syntax告诉cURL发送此参数的实际文件内容,但您不能期望在不同语言中通过将此“原样”作为字符串值以相同方式工作。建议您研究一下如何使用node.js执行文件上传请求。谢谢,您让我走上了正确的道路。fileReaderStream对您有用吗?当我尝试以这种方式发送时,它给了我一个错误:图像文件有问题
function callSendAPI(messageData, formData) {
    request({
        url: 'https://graph.facebook.com/v2.6/me',
        qs : { access_token: TOKEN },
        method: 'POST',
        json: messageData,
        formData: formData,
    }, function(error, response, body) {
        if (error) {
            console.log(error);
        } else if (response.body.error) {
            console.log(response.body.error);
        }
    })
}

function sendAttachment(recipientID, fileName) {
    var fileReaderStream = fs.createReadStream(fileName)
    var formData = {
                recipient: JSON.stringify({
                id: recipientID
            }),
            message: JSON.stringify({
            attachment: {
                type: 'file',
                payload: {
                    is_reusable: false
                }
            }
        }),
       filedata: fileReaderStream
    }
    callSendAPI(true, formData);
}