Skip to content

Instantly share code, notes, and snippets.

@redavis22
Forked from ranman/message_followers.py
Created February 5, 2017 05:56
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 redavis22/78a33393d213c1ee05fcea4bd414e568 to your computer and use it in GitHub Desktop.
Save redavis22/78a33393d213c1ee05fcea4bd414e568 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