Upload media to Twitter, reference media in tweet
@dylan
code:
data:privatelast updated:4 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 800,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.
upload_media
auth
to use OAuth tokens and API keys in code via theauths object
(auths.twitter)
params
URL
https://img.stackshare.io/service/11428/HzP2Yhq8_400x400.jpg
string ·params.url
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, 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
const axios = require("axios")

// Data we need to make the Twitter API request
const oauthSignerUri = auths.twitter.oauth_signer_uri
const token = {
  key: auths.twitter.oauth_access_token,
  secret: auths.twitter.oauth_refresh_token,
}
const signConfig = {
  token,
  oauthSignerUri
}

// Download image, base64 encode it, then upload the media to Twitter
const imageResponse = await axios({
    url: params.url,
    method: "GET",
    responseType: "arraybuffer"
})
const media_data = Buffer.from(imageResponse.data, 'binary').toString('base64')

// First, upload the media to Twitter
// https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload
const mediaUploadRequest = {
  data: '',
  method: 'POST',
  url: "https://upload.twitter.com/1.1/media/upload.json",
  params: {
    media_data
  },
}

// We want media_id_string
this.uploadMediaResponse = (await require("@pipedreamhq/platform").axios(this, mediaUploadRequest, signConfig))
this.mediaIdString = this.uploadMediaResponse.media_id_string
steps.
post_tweet
Post a tweet.
auth
(auths.twitter)
params
Status
 
string ·params.status
Media ids
{{steps.upload_media.mediaIdString}}
string ·params.media_ids
Optional
code
async (params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
}
19
const axios = require('axios')
const oauthSignerUri = auths.twitter.oauth_signer_uri
const {status, in_reply_to_status_id, auto_populate_reply_metadata, exclude_reply_user_ids, attachment_url, media_ids, possibly_sensitive, lat, long, place_id, display_coordinates, trim_user, enable_dmcommands, fail_dmcommands, card_uri} = params
const config = {
    url: `https://api.twitter.com/1.1/statuses/update.json`,
    method: 'POST',
    params,
}
const token = {
    key: auths.twitter.oauth_access_token,
    secret: auths.twitter.oauth_refresh_token,
}
const signConfig = {
  token,
  oauthSignerUri
}
return await require("@pipedreamhq/platform").axios(this, config, signConfig)