Chatwoot -发送附件- NodeJS

gopyfrb3  于 2022-11-22  发布在  Node.js
关注(0)|答案(1)|浏览(135)

我需要一些帮助...我尝试通过ChatWoot API发送文件,但我无法完成...
我使用的是NestJS,一个NodeJS框架。
我使用“@nestjs/axios”包中的“HttpService”发出请求。
FormData使用“表单数据”包。
我是这样做的:Fragment of my code
“file”变量来自类型“fs.readStream”,指向文件位置。
我得到的回应是:

{
"id": 284,
"content": null,
"inbox_id": 2,
"conversation_id": 54,
"message_type": 1,
"content_type": "text",
"content_attributes": {},
"created_at": 1659106073,
"private": false,
"source_id": null,
"sender": {
"id": 1,
"name": "John",
"available_name": "John",
"avatar_url": "https://www.gravatar.com/avatar/0d722ac7bc3b3c92c030d0da9690d981?d=404",
"type": "user",
"availability_status": "online",
"thumbnail": "https://www.gravatar.com/avatar/0d722ac7bc3b3c92c030d0da9690d981?d=404"
  }
}

此外,我已经尝试了“文件”变量作为缓冲区,但响应是相同的。
我做错了什么?
谢谢你!

ncecgwcz

ncecgwcz1#

讽刺的是,我也遇到过同样的问题,最后我不得不使用Axios成功地将表单发布到Chatwoot(自托管)上,这是我正在使用的工作代码。虽然我没有在您的代码中看到任何错误,但我希望这段代码能有所帮助,因为这是我在一天的修补后开始工作的唯一方法。

var form = new FormData();
form.append("message_type", "incoming");
form.append("attachments[]", fs.createReadStream(filePath));

const formHeaders = form.getHeaders();

axios
  .post(
    baseUrl +
      "accounts/" +
      accountId +
      "/conversations/" +
      conversation.id +
      "/messages",
    form,
    {
      headers: {
        ...formHeaders,
        api_access_token: apiKey,
      },
    }
  )
  .then(function (response) {
    console.debug("Axios Post Attachment to Chatwoot Response", response);
    // resolve(response);
  })
  .catch(function (error) {
    console.error("Axios Post Attachment Error", error);
    // reject(error);
  });

相关问题