Skip to content

Instantly share code, notes, and snippets.

@ranman
Created October 24, 2016 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ranman/a72905eabf321cc25da59adc390b2f77 to your computer and use it in GitHub Desktop.
Save ranman/a72905eabf321cc25da59adc390b2f77 to your computer and use it in GitHub Desktop.

Setup

  1. Create a dynamodb table with a "follower" index (numeric)

Operation

  1. Create a lambda function
import twitter
import boto3
import time
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('twitter_followers')
api = twitter.Api(
consumer_key='',
consumer_secret='',
access_token_key='',
access_token_secret=''
)
direct_message = """\
Hello! Thanks for following me! Let me know if I can help you out with anything AWS related. You can shoot me a message at randhunt@amazon.com or on twitter.
"""
def lambda_handler(event, context):
followers = set(api.GetFollowerIDs())
ddb_followers = table.scan(ProjectionExpression="follower")["Items"]
old_followers = set([int(x['follower']) for x in ddb_followers])
timestamp = int(time.time())
new_followers = followers - old_followers
for new_follower in new_followers:
table.put_item(Item={'follower': new_follower, 'timestamp': timestamp})
api.PostDirectMessage(direct_message, user_id=new_follower)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment