Skip to content

Instantly share code, notes, and snippets.

@rfilmyer
Created August 18, 2016 00:31
Show Gist options
  • Save rfilmyer/bb3ea005014efd63bed4f709aaace776 to your computer and use it in GitHub Desktop.
Save rfilmyer/bb3ea005014efd63bed4f709aaace776 to your computer and use it in GitHub Desktop.
Amazon Lambda function to post to Slack when Donald Trump tweets "Sad!" at something
'use strict';
const https = require('https');
const url = require('url');
const sadCheckVersion = '0.1.0';
/**
* Event options:
* twitterKey: the Twitter Application Key to be used
* twitterSecret: the Twitter Application Secret to be used
* slackURL: the URL for the Slack webhook
* minutesLookback: How far back to check for a "Sad!" (Currently not implemented)
* alwaysTrue(optional): for testing, to make sure that the bot works, always says that the last tweet is "Sad!".
* Will fire even if a tweet was not posted in the last minutesLookback minutes
*
*/
const isSad = (tweetText) => tweetText.toLowerCase().includes("sad");
const encodeTwitterCredentials = (key, secret) =>
new Buffer(
encodeURIComponent(key) + ':' + encodeURIComponent(secret)
).toString('base64');
exports.handler = (event, context, callback) => {
const twAuthReq = https.request({
method: "POST",
hostname: "api.twitter.com",
path: "/oauth2/token",
port: 443,
headers: {
"Authorization": "Basic " + encodeTwitterCredentials(event.twitterKey, event.twitterSecret),
"User-Agent": "sadCheck " + sadCheckVersion,
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
}
}, (authRes) => {
let authJSON = '';
authRes.on('data', (chunk) => authJSON += chunk);
authRes.on('end', () => { // nest other 2 calls in here- oh god damn it.
//console.log("Authenticated to Twitter");
//console.log(authJSON);
const auth = JSON.parse(authJSON);
const token = auth.access_token;
const tweetReq = https.request({
method: "GET",
hostname: "api.twitter.com",
port: 443,
path: "/1.1/statuses/user_timeline.json?screen_name=realDonaldTrump&count=1",
headers: {
"Authorization": "Bearer " + token,
"User-Agent": "sadCheck " + sadCheckVersion
}
}, (tweetRes) => {
//console.log('Getting last tweet...');
let body = '';
tweetRes.setEncoding('utf8');
tweetRes.on('data', (chunk) => body += chunk);
tweetRes.on('end', () => {
//console.log('Successfully processed HTTPS response');
const tweets = JSON.parse(body);
const tweet = tweets[0];
if (isSad(tweet.text) || event.alwaysTrue) {
//console.log("Sad!");
const parsedURL = url.parse(event.slackURL);
//console.log(parsedURL.hostname + parsedURL.path);
const slackReq = https.request({
method: "POST",
port: 443,
hostname: parsedURL.hostname,
path: parsedURL.path
}, (slackRes) => {
let slackBody = '';
slackRes.on('data', (chunk) => body += chunk);
slackRes.on('end', () => {
//console.log(slackBody);
callback(null, "Sad!:" + tweet.text);});
});
const sadAlert = `Donald Trump just called something \"Sad!\" on Twitter: <https://twitter.com/realDonaldTrump/status/${tweet.id_str}>`;
//console.log(sadAlert);
slackReq.write(JSON.stringify({text: sadAlert, unfurl_links: true}));
slackReq.on('error', callback);
slackReq.end();
} else {
callback(null, "Not Sad.");
}
//callback(null, body);
});
}
);
tweetReq.on('error', callback);
tweetReq.end();
})
}
);
twAuthReq.write("grant_type=client_credentials");
twAuthReq.on('error', callback);
twAuthReq.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment