Skip to content

Instantly share code, notes, and snippets.

@raspberrycoulis
Created March 10, 2017 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raspberrycoulis/5863ba6076b96508c4fa0893e233c1ab to your computer and use it in GitHub Desktop.
Save raspberrycoulis/5863ba6076b96508c4fa0893e233c1ab to your computer and use it in GitHub Desktop.
Raspberry Pi Twitter Follower Bot Python script
#!/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 postToSlack():
# 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)
postToSlack()
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment