Last active
April 21, 2020 15:18
-
-
Save richwednesday/f5572b6e74f2c50d57392bf5c0b94c7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request-promise'); | |
const environment = "staging"; | |
const t_config = { | |
oauth: { | |
consumer_key: process.env.TWITTER_CONSUMER_KEY, | |
consumer_secret: process.env.TWITTER_CONSUMER_SECRET, | |
token: process.env.TWITTER_ACCESS_TOKEN, | |
token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET | |
} | |
} | |
/** | |
* Retieves user ID for access tokens in config | |
* and adds it to twitter config object | |
*/ | |
function get_user_id() { | |
// get current user info | |
request.get({ | |
url: 'https://api.twitter.com/1.1/account/verify_credentials.json', | |
oauth: t_config.oauth | |
}, (error, response, body) => { | |
if (error) { | |
console.log('Error retreiving user data.') | |
console.log(error) | |
return; | |
} | |
// console.log(body) | |
let user_id = JSON.parse(body).id_str | |
t_config.user_id = user_id | |
}) | |
} | |
get_user_id() | |
async function send_text_message(id, text) { | |
try { | |
const res = await request.post({ | |
url: 'https://api.twitter.com/1.1/direct_messages/events/new.json', | |
oauth: twitter.oauth, | |
json: true, | |
headers: { | |
'content-type': 'application/json' | |
}, | |
body: { | |
event: { | |
type: 'message_create', | |
message_create: { | |
target: { | |
recipient_id: id | |
}, | |
message_data: { | |
text | |
} | |
} | |
} | |
} | |
}) | |
console.log(res); | |
return res; | |
} | |
catch (error) { | |
console.log(error) | |
return null; | |
} | |
} | |
async function get_webhook() { | |
try { | |
const res = await request.get({ | |
url: `https://api.twitter.com/1.1/account_activity/all/${environment}/webhooks.json`, | |
oauth: twitter.oauth | |
}) | |
console.log(res) | |
} | |
catch(error) { | |
console.log(error) | |
} | |
} | |
async function create_webhook(url) { | |
try { | |
const res = await request.post({ | |
url: `https://api.twitter.com/1.1/account_activity/all/${environment}/webhooks.json`, | |
oauth: twitter.oauth, | |
headers: { | |
'Content-type': 'application/x-www-form-urlencoded' | |
}, | |
form: { | |
url | |
} | |
}) | |
console.log(res) | |
} | |
catch(error) { | |
console.log(error) | |
} | |
} | |
async function delete_webhook(id) { | |
try { | |
const res = await request.delete({ | |
url: `https://api.twitter.com/1.1/account_activity/all/${environment}/webhooks/${id}.json`, | |
oauth: twitter.oauth | |
}) | |
console.log(res) | |
} | |
catch(error) { | |
console.log(error) | |
} | |
} | |
async function add_subscription() { | |
try { | |
const res = await request.post({ | |
url: `https://api.twitter.com/1.1/account_activity/all/${environment}/subscriptions.json`, | |
oauth: twitter.oauth | |
}, (error, response, body) => { | |
if (response.statusCode == 204) { | |
console.log(body) | |
console.log('Subscription added.') | |
} else { | |
console.log(body) | |
console.log('User has not authorized your app.') | |
} | |
}) | |
console.log(res) | |
} | |
catch (error) { | |
console.log(error) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment