-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathDB.py
More file actions
176 lines (143 loc) · 5.95 KB
/
DB.py
File metadata and controls
176 lines (143 loc) · 5.95 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
Lambda function that decodes and writes data from your portfolio website to your DynamoDB database.
Author: Explore Data Science Academy.
Note:
---------------------------------------------------------------------
The contents of this file should be added to a AWS Lambda function
created as part of the EDSA Cloud-Computing Predict.
For further guidance around this process, see the README instruction
file which sits at the root of this repo.
---------------------------------------------------------------------
"""
# Lambda dependencies
import boto3 # Python AWS SDK
import json # Used for handling API-based data.
import base64 # Needed to decode the incoming POST
import random
import uuid
from botocore.exceptions import ClientError
#CREATE_RAW_PATH = "/contactme"
def lambda_handler(event, context):
if event["rawPath"]== "/contactme":
# Perform JSON data decoding
body_enc = json.loads(event['body'])
body_enc = event['body']
dec_dict = json.loads(base64.b64decode(body_enc))
table.put_item(Item={'ResponsesID': rid, # <--- Insert the correct variable
'Name': dec_dict['name'], # <--- Insert the correct variable
'Email': dec_dict['email'], # <--- Insert the correct variable
'Cell': dec_dict['phone'], # <--- Insert the correct variable
'Message': dec_dict['message'] # <--- Insert the correct variable
})
# --- Write to dynamodb ---
# ** Create a variable that can take a random value between 1 and 1 000 000 000.
# This variable will be used as our key value i.e the ResponsesID and should be of type integer.
# It is important to note that the ResponseID i.e. the rid variable, should take
# on a unique value to prevent errors when writing to DynamoDB. **
# --- Insert your code here ---
rid =random.randint(1,1000000)
# rid = None # <--- Replace this value with your code.
# -----------------------------
# ** Instantiate the DynamoDB service with the help of the boto3 library **
# --- Insert your code here ---
dynamodb = boto3.resource('dynamodb')#,region_name='eu-west-1') # <--- Replace this value with your code.
# -----------------------------
# Instantiate the table. Remember pass the name of the DynamoDB table created in step 4
table = dynamodb.Table('PredictDB')
# ** Write the responses to the table using the put_item method. **
# Complete the below code so that the appropriate
# incoming data is sent to the matching column in your DynamoDB table
# --- Insert your code here ---
"""db_response = table.put_item(Item={'ResponsesID': rid, # <--- Insert the correct variable
'Name': dec_dict['name'], # <--- Insert the correct variable
'Email': dec_dict['email'], # <--- Insert the correct variable
'Cell': dec_dict['phone'], # <--- Insert the correct variable
'Message': dec_dict['message'] # <--- Insert the correct variable
})
# -----------------------------
"""
#email_text = 'Hello world'
# ** SES Functionality **
# Replace sender@example.com with your "From" address.
# This address must be verified with Amazon SES.
# --- Insert your code here ---
""""SENDER = 'ntseleta@gmail.com'
ses.send_mail(
Source='ntseleta@gmail.com',
Destination={
'ToAddresses': [
'ntselet@hotmail.com',
]
},
Message={
'Subject': {
'Data': 'Predict',
'Charset': 'UTF-8'
},
'Body': {
'Text': {
'Data': email_text,
'Charset': 'UTF-8'
},
}
},
)
# -----------------------------
# Replace recipient@example.com with a "To" address. If your account
# is still in the sandbox, this address must be verified.
# --- Insert your code here ---
RECIPIENT = 'ntseleta@gmail.com'
# -----------------------------
# The subject line for the email.
# --- DO NOT MODIFY THIS CODE ---
SUBJECT = f"Data Science Portfolio Project Website - Hello {dec_dict['name']}"
# -------------------------------
# The email body for recipients with non-HTML email clients
BODY_TEXT = (email_text)
# The character encoding for the email.
CHARSET = "UTF-8"
# Create a new SES service resource
client = boto3.client('ses')
# Try to send the email.
try:
# Provide the contents of the email.
ses_response = client.send_email(
Destination={
'ToAddresses': [
RECIPIENT,
# 'edsa.predicts@explore-ai.net', # <--- Uncomment this line once you have successfully tested your predict end-to-end
],
},
Message={
'Body': {
'Text': {
'Charset': CHARSET,
'Data': BODY_TEXT,
},
},
'Subject': {
'Charset': CHARSET,
'Data': SUBJECT,
},
},
Source=SENDER,
)
# Display an error if something goes wrong.
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:"),
print(ses_response['MessageId'])
# ** Create a response object to inform the website
# that the workflow executed successfully. **
lambda_response = {
'statusCode': 200,
'body': json.dumps({
'Name': dec_dict['Name'],
'Email': dec_dict['Email'],
'Cell': dec_dict['Phone_number'],
'Message': dec_dict['Message'],
'DB_response': db_response
})
}"""
return lambda_response