Skip to content

Instantly share code, notes, and snippets.

@jcipriano
Created December 4, 2017 23:19
Show Gist options
  • Save jcipriano/990ede732411113667cb5844828dbd50 to your computer and use it in GitHub Desktop.
Save jcipriano/990ede732411113667cb5844828dbd50 to your computer and use it in GitHub Desktop.
Prompts a user to share via Tweet compose from a Direct Message with Node.js.
var nconf = require('nconf')
var request = require('request')
// load config
nconf.file({ file: 'config.json' }).env()
// twitter authentication
var twitter_oauth = {
consumer_key: nconf.get('TWITTER_CONSUMER_KEY'),
consumer_secret: nconf.get('TWITTER_CONSUMER_SECRET'),
token: nconf.get('TWITTER_ACCESS_TOKEN'),
token_secret: nconf.get('TWITTER_ACCESS_TOKEN_SECRET')
}
// direct message request body
var dm_params = {
"event": {
"type": "message_create",
"message_create": {
"target": {
"recipient_id": "4534871",
},
"message_data": {
"text": "Hello",
"ctas": [
{
"type": "web_url",
"label": "Share this",
"url": "https://twitter.com/intent/tweet?text=hello"
}
]
}
}
}
}
// request options
var request_options = {
url: 'https://api.twitter.com/1.1/direct_messages/events/new.json',
oauth: twitter_oauth,
json: true,
headers: {
'content-type': 'application/json'
},
body: dm_params
}
// POST request to send Direct Message
request.post(request_options, function (error, response, body) {
console.log(body)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment