Skip to content

Commit

Permalink
Add twitter image upload
Browse files Browse the repository at this point in the history
  • Loading branch information
starsinmypockets committed May 3, 2021
1 parent 62488c1 commit 1ef8ea1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
34 changes: 29 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,44 @@ const {
const CONTRACT_EVENTS_ARRAY = CONTRACT_EVENTS.split(',')
const Web3 = require('web3')
const Twitter = require('twitter')
const restClient = require('node-rest-client-promise').Client();
const restClient = require('node-rest-client-promise').Client()
const axios = require('axios')
const web3 = new Web3(new Web3.providers.WebsocketProvider(WSURL))

const twitterClient = new Twitter({
consumer_key: TWITTER_API_KEY,
consumer_secret: TWITTER_API_SECRET_KEY,
access_token_key: TWITTER_ACCESS_TOKEN,
access_token_secret: TWITTER_ACCESS_TOKEN_SECRET
})

function postToTwitter(event) {
const msg = eval('`'+ TWITTER_MESSAGE_TEMPLATE + '`')
return twitterClient.post('statuses/update', {status: msg}, function(error, tweet, response) {
async function postToTwitter(event) {
const msg = eval('`'+ TWITTER_MESSAGE_TEMPLATE + '`') // can replace this with message template
let opts = { status: msg }

// If you always use an image you can use `if (true)` here or remove conditional
if (TWITTER_MESSAGE_TEMPLATE.indexOf('metadata.data.image') >= 0) {
const img_url = event.metadata.data.image

// get png binary data
const result = await axios.request({
responseType: 'arraybuffer',
url: img_url,
method: 'get',
headers: {
'Content-Type': 'image/png', // if the image type changes this too
},
})

const data = result.data
const uploadResult = await twitterClient.post('media/upload', { media: data})
const media_id = uploadResult.media_id
opts = { status: msg, media_ids: media_id }
}

return twitterClient.post('statuses/update', opts, function(error, tweet, response) {
if (error) return console.log(JSON.stringify(error))
});
})
}

async function getContractAbi() {
Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1",
"dotenv": "^8.2.0",
"node-rest-client-promise": "^3.1.1",
"twitter": "^1.7.1",
Expand Down

0 comments on commit 1ef8ea1

Please sign in to comment.