Skip to content

Instantly share code, notes, and snippets.

@matteason
Created June 28, 2022 16:32
Show Gist options
  • Save matteason/a49b78816997be3c7cc81f717b54a169 to your computer and use it in GitHub Desktop.
Save matteason/a49b78816997be3c7cc81f717b54a169 to your computer and use it in GitHub Desktop.
const Twitter = require('twitter');
const client = new Twitter({
consumer_key: 'XXXXXXXXXXXXXXXX',
consumer_secret: 'XXXXXXXXXXXXXXXXXXXXX',
access_token_key: 'XXXXXXXXXXXXXXXXXXXX',
access_token_secret: 'XXXXXXXXXXXXXXXXXXXXXXX'
});
let lastTimelineTweetId;
(async function() {
await getLastBotTweetId();
setInterval(async () => {
await getTweets();
}, 10*1000);
}());
async function getLastBotTweetId() {
const params = {screen_name: 'gaadbot'};
client.get('statuses/user_timeline', params, async function(error, tweets, response) {
if (!error && tweets.length > 0) {
lastTimelineTweetId = tweets[0].id_str;
console.log(`Setting last tweet ID to ${tweets[0].id_str} on startup`);
await getTweets();
} else if(tweets.length === 0) {
lastTimelineTweetId = "1";
await getTweets();
}
else {
console.log('Failed to startup, could not get last bot tweet id')
}
});
}
async function getTweets() {
let params = {
q: '#a11y OR #accessibility OR #NAAW OR #gaad OR #gaad2022 OR #globalaccessibilityawarenessday OR #NationalAccessAbilityWeek OR #NationalAccessibilityWeek OR #deafblindawarenessweek OR #deafblind OR #deafblindawareness OR #helenkellerday OR #helenkeller',
result_type: 'recent',
count: 100,
tweet_mode: 'extended',
trim_user: 'false',
include_entities: 'false',
exclude_replies: 'false',
include_rts: 'false',
since_id: lastTimelineTweetId
};
client.get('search/tweets', params, function(error, tweets, response) {
console.log(`[${(new Date).toString()}] Getting tweets since ${lastTimelineTweetId}`)
if (!error) {
getFullTweets(tweets.statuses.map(s => s.id_str));
} else {
console.log('---------ERROR----------');
console.log(error);
}
});
}
function getFullTweets(ids) {
let params = {
id: ids.join(','),
result_type: 'recent',
tweet_mode: 'extended',
trim_user: 'false',
include_entities: 'true',
include_ext_alt_text: 'true',
};
client.get('statuses/lookup', params, function(error, tweets, response) {
if (!error) {
console.log(`Got ${tweets.length} tweets to analyse`)
if(tweets.length > 0) {
lastTimelineTweetId = tweets[0].id_str;
}
tweets.forEach((t) => analyseTweet(t));
} else {
console.log('---------ERROR----------');
console.log(error);
}
});
}
async function analyseTweet(tweet) {
if(tweet.full_text.startsWith("RT @")) {
return;
}
if(tweet.hasOwnProperty('extended_entities')) {
const images = tweet.extended_entities.media.filter(m => ['photo','animated_gif'].includes(m.type));
const imagesWithoutAlt = tweet.extended_entities.media.filter(m => ['photo','animated_gif'].includes(m.type) && m.ext_alt_text === null);
if(imagesWithoutAlt.length > 0 && imagesWithoutAlt.length === images.length) {
console.log('found tweet with no alt')
await tweetReply(tweet, imagesWithoutAlt.length);
}
}
}
async function tweetReply(originalTweet, badTweetCount) {
console.log(`sending a reply to ${originalTweet.user.name}`)
let name = originalTweet.user.name;
if(name.length > 26) {
name = name.substring(0,23) + '...'
}
const params = {
status: `🤖 Hi ${name}! It looks like you're interested in accessibility. If you add alt text to your images, they'll be accessible to blind, partially sighted and neurodivergent people who use screen reader software.\n\nHow to add alt text & FAQs: https://twitter.com/GAADBot/status/1531211677563232257`,
attachment_url: `https://twitter.com/${originalTweet.user.screen_name}/status/${originalTweet.id_str}`
}
try {
return await client.post('statuses/update', params)
} catch(err) {
console.error(err);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment