Slack - File Upload
@sergio
code:
data:privatelast updated:3 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 1,000,000+ developers using the Pipedream platform
steps.
trigger
HTTP API
Deploy to generate unique URL
This workflow runs on Pipedream's servers and is triggered by HTTP / Webhook requests.
steps.
preparefiletoupload
auth
to use OAuth tokens and API keys in code via theauths object
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps) => {
1
2
3
4
5
6
7
8
9
10
11
}
12
const axios = require('axios')

const config = {
  method: 'GET',
  url: 'https://res.cloudinary.com/da5lyi17v/image/upload/v1570819140/sample.jpg',
  responseType: 'arraybuffer',
}

const resp = await axios(config)
this.screenshot = resp.data.toString('base64');
steps.
slack_file_upload
Uploads or creates a file.
auth
(auths.slack)
params
Optional
code
async (params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
}
45
//See the API docs here: https://api.slack.com/methods/files.upload
//See a workflow example of this action: https://pipedream.com/@sergio/slack-file-upload-p_OKC2vx/edit

if (params.content) {
  const data = {
    channels: params.channel,
    content: params.content,
    filename: params.filename,
    filetype: params.filetype,
    initial_comment: params.initial_comment,
    thread_ts: params.thread_ts,
    title: params.title
  }
  const config = {
    url: `https://slack.com/api/files.upload`,
    headers: {
      Authorization: `Bearer ${auths.slack.oauth_access_token}`,
      "Content-Type": "application/x-www-form-urlencoded",
    },
    params: data,
  }
  return await require("@pipedreamhq/platform").axios(this, config)
}
else {
  const streamifier = require("streamifier")
  const { WebClient } = require('@slack/web-api');
  const web = new WebClient(auths.slack.oauth_access_token)
  const buffer = Buffer.from(params.file_contents, "base64");
  const file = streamifier.createReadStream(buffer);

  await web.files.upload(
    {
      channels: params.channel,
      filename: params.filename,
      file,
      filetype: params.filetype,
      initial_comment: params.initial_comment,
      thread_ts: params.thread_ts,
      title: params.title
    }
  );

}