Skip to content

Instantly share code, notes, and snippets.

@raspberrycoulis
Created March 9, 2017 11:18
Show Gist options
  • Save raspberrycoulis/f8e2b648479aa779074d1baccb235a35 to your computer and use it in GitHub Desktop.
Save raspberrycoulis/f8e2b648479aa779074d1baccb235a35 to your computer and use it in GitHub Desktop.
Python script to check your Twitter followers and send you a Slack notification of how many this is.
#!/usr/bin/python
import tweepy
import urllib2
import time
# For Twitter: Add the relevant keys, tokens and secrets from your Twitter app made here: https://apps.twitter.com/
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
# Variables - configure the bits below to get your script working.
wait = 12600 # Time (in seconds) between checks. Default is 12600 seconds (210 minutes / 3.5 hours)
style = "#1da1f2" # Colour for the message - default is Twitter Bird blue
userid = '' # The Twitter user you want to track the followers of (without the @)
handle = '' # Tweak this to display the userid in a nicer format - i.e. "Raspberry Coulis" instead of "raspberrycoulis"
# Slack incoming webhook URL
webhook = ''
# Tweepy API - do not change
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
follows = api.get_user(userid)
# The function that does the magic - checks your Twitter user for the number of followers then sends this data to Slack to notify you.
def followers():
while True:
fans = str(follows.followers_count)
data = '{"attachments":[{"fallback":"'+handle+' has '+fans+' followers.","pretext":"'+handle+' has '+fans+' followers.","color":"'+style+'","fields":[{"title":"Twitter Fans","value":"'+handle+' has '+fans+' followers.","short":false}]}]}'
slack = urllib2.Request(webhook, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(slack)
f.close()
time.sleep(wait)
followers()
@actuino
Copy link

actuino commented Mar 9, 2017

HI,
@mentions are more volatile and would be nice to track, too.
Nice indicator that something is going on.

Every 15 minutes for near real-time alerts, or if you want to go easy on the API, just twice a day.
Then you can send the info and compare with a (floating?) average rate of the same day of week.
History could be tracked on a physical display, then :)

Maybe some other online service would serve as interface and expose an API with historical statistics? (I didn't check)

I did some twitter automation before, some with IFTTT, but found out that @mentions (and actions in reaction to @mentions) were pretty sensitive on the twitter side and may get your account blocked (temporarily however), so be careful.

@raspberrycoulis
Copy link
Author

Thanks @actuino,

I had thought about that, but felt that Twitter will notify you of any mentions etc., via the app or site anyway. However, keeping track of your followers was not as quick - you can see them from your profile, but I'm working on growing my company's Twitter account and knowing how many followers we have is useful.

Although I do like the idea of tracking the information on a physical display!

@raspberrycoulis
Copy link
Author

@alexellis, I've take on board your feedback about a configuration file and have updated the repository accordingly. If you fancy taking a look and seeing if this flows better, than that would be great: raspberrypi-socialmedia-bot

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