Looking for the chatbot template? It's now here.

File uploads

Send and receive files across chat platforms.

Send files

Attach files to messages using the files property:

lib/bot.ts
const reportBuffer = Buffer.from("PDF content");

await thread.post({
  markdown: "Here's the report you requested:",
  files: [
    {
      data: reportBuffer,
      filename: "report.pdf",
      mimeType: "application/pdf",
    },
  ],
});

Multiple files

lib/bot.ts
await thread.post({
  markdown: "Attached are the images:",
  files: [
    { data: image1, filename: "screenshot1.png" },
    { data: image2, filename: "screenshot2.png" },
  ],
});

Files without text

lib/bot.ts
await thread.post({
  markdown: "",
  files: [{ data: buffer, filename: "document.xlsx" }],
});

Receive files

Access attachments from incoming messages:

lib/bot.ts
bot.onSubscribedMessage(async (thread, message) => {
  for (const attachment of message.attachments ?? []) {
    console.log(`File: ${attachment.name}, Type: ${attachment.mimeType}`);

    if (attachment.fetchData) {
      const data = await attachment.fetchData();
      console.log(`Downloaded ${data.length} bytes`);
    }
  }
});

Attachment properties

PropertyTypeDescription
typestringAttachment type (e.g., "image", "file")
urlstring (optional)Public URL
namestring (optional)Filename
mimeTypestring (optional)MIME type
sizenumber (optional)File size in bytes
widthnumber (optional)Image width
heightnumber (optional)Image height
fetchData() => Promise<Buffer> (optional)Download the file data

On this page

GitHubEdit this page on GitHub