Skip to content

Instantly share code, notes, and snippets.

@raspberrycoulis
Created March 9, 2017 11:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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()
@alexellis
Copy link

    slack = urllib2.Request(webhook, data, {'Content-Type': 'application/json'})
    f = urllib2.urlopen(slack)

You really need to respond to and cope with errors from the webhook post. Twitter will eventually rate-limit you too, so make sure you can handle that.

@alexellis
Copy link

I know you were after coding feedback.

Instead of:

+consumer_key = ''
+consumer_secret = ''
+access_token = ''
+access_token_secret = ''

Look at how to read environmental variables - or extract this to a config file like I've done with phototimer and scrollphat:

https://github.com/alexellis/phototimer

Then make the config file part of the .gitignore and give a sample version that is checked in.

@alexellis
Copy link

# Slack incoming webhook URL

webhook = ''

Maybe you should add a comment about how to get/create a slack URL. Link to the docs page you used.

@raspberrycoulis
Copy link
Author

Thanks @alexellis, will take a look at the configuration variables you linked to. I'm going to add more info to the GitHub repository in the README.md for Slack webhooks etc., so will include a link to that as a comment in the relevant part in the code too. I've changed the timing for the check to every 3.5 hours - the idea being that businesses could use this to check their social media following (in fact, my company is now using this to check our new Twitter account for followers and we use Slack, so this was something I put together with this in mind initially), but I'll look into the API limits for this type of call.

@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