Skip to content

Instantly share code, notes, and snippets.

@jcipriano
Last active May 9, 2019 00:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcipriano/4820fe05152769cc87d4322897755707 to your computer and use it in GitHub Desktop.
Save jcipriano/4820fe05152769cc87d4322897755707 to your computer and use it in GitHub Desktop.
Creates a Twitter webhook config
var request = require('request')
// twitter authentication
var twitter_oauth = {
consumer_key: 'TWITTER_CONSUMER_KEY',
consumer_secret: 'TWITTER_CONSUMER_SECRET',
token: 'TWITTER_ACCESS_TOKEN',
token_secret: 'TWITTER_ACCESS_TOKEN_SECRET'
}
var WEBHOOK_ID = 'your-webhook-id'
// request options
var request_options = {
url: 'https://api.twitter.com/1.1/account_activity/webhooks/' + WEBHOOK_ID + '/subscriptions.json',
oauth: twitter_oauth
}
// POST request to create webhook config
request.post(request_options, function (error, response, body) {
if (response.statusCode == 204) {
console.log('Subscription added.')
} else {
console.log('User has not authorized your app.')
}
})
var request = require('request')
// twitter authentication
var twitter_oauth = {
consumer_key: 'TWITTER_CONSUMER_KEY',
consumer_secret: 'TWITTER_CONSUMER_SECRET',
token: 'TWITTER_ACCESS_TOKEN',
token_secret: 'TWITTER_ACCESS_TOKEN_SECRET'
}
var WEBHOOK_URL = 'https://your-webhook-url'
// request options
var request_options = {
url: 'https://api.twitter.com/1.1/account_activity/webhooks.json',
oauth: twitter_oauth,
headers: {
'Content-type': 'application/x-www-form-urlencoded'
},
form: {
url: WEBHOOK_URL
}
}
// POST request to create webhook config
request.post(request_options, function (error, response, body) {
console.log(body)
})
@jcipriano
Copy link
Author

jcipriano commented Jun 14, 2017

  1. npm install request
  2. Edit and run create-webhook-config.js. Note returned webhook ID.
  3. Edit and run add-subscription.js.

More info in the docs:
https://dev.twitter.com/rest/direct-messages
https://dev.twitter.com/webhooks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment