You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
carbon/handlers/twitter.js

31 lines
1003 B
JavaScript

const Twitter = require('twitter')
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
})
const uploadImage = (data) => client.post('media/upload', { media_data: data })
const uploadTweet = (media) => client.post('statuses/update', { status: 'carbon copy', media_ids: media.media_id_string })
const extractImageUrl = (response) => response.entities.media[0].display_url
const respondSuccess = (res, url) => res.json({ url })
const respondFail = (res, err) => {
console.error(`Error: ${err.message || err}`)
res.status(500).send()
}
module.exports = (req, res) => {
if (!req.body.data) {
return res.status(400).send()
}
uploadImage(req.body.data)
.then(uploadTweet)
.then(extractImageUrl)
.then(respondSuccess.bind(null, res))
.catch(respondFail.bind(null, res))
}