From 1d05c15351fbf93e9420523d1efce3bd189ca0d3 Mon Sep 17 00:00:00 2001 From: briandennis Date: Fri, 8 Sep 2017 13:37:22 -0500 Subject: [PATCH] add twitter handler --- handlers/twitter.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 handlers/twitter.js diff --git a/handlers/twitter.js b/handlers/twitter.js new file mode 100644 index 0000000..1bd641f --- /dev/null +++ b/handlers/twitter.js @@ -0,0 +1,30 @@ +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 = (stuff) => console.log(stuff.entities.media[0].media_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)) +} \ No newline at end of file