-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmessage_followers.py
39 lines (28 loc) · 1.05 KB
/
message_followers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from base64 import b64decode
import os
import time
import boto3
import twitter
dynamodb = boto3.resource('dynamodb')
kms = boto3.client('kms')
table = dynamodb.Table(os.getenv('DDB_TABLE'))
def decrypt(var):
"""Helper function to decrypt our environment variables"""
return kms.decrypt(CipherTextBlob=b64decode(var))['Plaintext']
# TODO: enable KMS encryption/decryption
api = twitter.Api(
os.getenv('CONSUMER_KEY'),
os.getenv('CONSUMER_SECRET'),
os.getenv('ACCESS_TOKEN_KEY'),
os.getenv('ACCESS_TOKEN_SECRET')
)
direct_message = os.getenv('MESSAGE')
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)