Chatwoot - Send attachments - NodeJS

608 views Asked by At

I need help with something... I'm trying to send files through the ChatWoot API but I'm not able to do it...

I'm using NestJS, a NodeJS framework.

I make the requests using "HttpService" from "@nestjs/axios" package.

FormData uses the "form-data" package.

I am doing this: Fragment of my code

The "file" variable is from type "fs.ReadStream", pointing to file location.

And I get this response:

{
"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"
  }
}

Also, i've tried with "file" variable as Buffer, but the response was the same.

What am I doing wrong?

Thanks!!

1

There are 1 answers

1
Unreal4Real On BEST ANSWER

I have also faced this same issue ironically and in the end I had to use Axios to get a successful form post to Chatwoot (self-hosted), here is the working code I am using. While I did not see any error in your code I hope this snippet helps as it was the only way I got to work after a day of tinkering.

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);
  });