-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
45 lines (36 loc) · 1.24 KB
/
app.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
40
41
42
43
44
45
import json
import boto3
import base64
import json
import tweepy
from tweepy import OAuthHandler
from botocore.exceptions import ClientError
print('Loading function')
def lambda_handler(event, context):
print("Received event: " + json.dumps(event))
send_tweet(event["body"])
return ("Lamba Handler Execution Completed")
def get_secret(key):
secret_name = "lambda-secrets"
region_name = "us-east-1"
# Create a Secrets Manager client
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name=region_name
)
try:
get_secret_value_response = client.get_secret_value(SecretId=secret_name)
secret_string=json.loads(get_secret_value_response['SecretString'])
return(secret_string[key])
except ClientError as e:
print(e)
def send_tweet(body):
consumer_key = get_secret('consumer_key ')
consumer_secret = get_secret('consumer_secret ')
access_token = get_secret('access_token')
access_token_secret = get_secret('access_token_secret ')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
api.update_status(status=body)