-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSendEmailFunction.cs
More file actions
107 lines (97 loc) · 3.72 KB
/
SendEmailFunction.cs
File metadata and controls
107 lines (97 loc) · 3.72 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
/**
* This is a sample Lambda function that sends an email on click of a
* button. It requires these SES permissions.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ses:GetIdentityVerificationAttributes",
"ses:SendEmail",
"ses:VerifyEmailIdentity"
],
"Resource": "*"
}
]
}
*
* The following JSON template shows what is sent as the payload:
{
"serialNumber": "GXXXXXXXXXXXXXXXXX",
"batteryVoltage": "xxmV",
"clickType": "SINGLE" | "DOUBLE" | "LONG"
}
*
* A "LONG" clickType is sent if the first press lasts longer than 1.5 seconds.
* "SINGLE" and "DOUBLE" clickType payloads are sent for short clicks.
*
* For more documentation, follow the link below.
* http://docs.aws.amazon.com/iot/latest/developerguide/iot-lambda-rule.html
*/
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;
using Aws.Lambda.Models;
using Newtonsoft.Json;
namespace Aws.Lambda
{
public class SendEmailFunction
{
private readonly AmazonSimpleEmailServiceClient _ses = new AmazonSimpleEmailServiceClient();
private const string RecipientEnvironmentKey = "Recipient";
private const string VerificationEmailWarning = "Verification email sent. Please verify it.";
[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
public async Task ButtonHandler(IotButtonEvent buttonEvent, ILambdaContext context)
{
var email = Environment.GetEnvironmentVariable(RecipientEnvironmentKey);
var payload = JsonConvert.SerializeObject(buttonEvent);
Console.WriteLine($"Received event: {payload}");
var emailStatus = await CheckEmailAsync(email);
if (!emailStatus.IsVerified)
{
Console.WriteLine($"Failed to check email: {email}. {emailStatus.ErrorMessage}");
return;
}
var subject = $"Hello from your IoT Button {buttonEvent.SerialNumber}";
var body = $"Hello from your IoT Button {buttonEvent.SerialNumber}. Here is the full event: {payload}.";
var request = new SendEmailRequest
{
Source = email,
Destination = new Destination(new List<string> { email }),
Message = new Message(new Content(subject), new Body(new Content(body)))
};
await _ses.SendEmailAsync(request);
}
private async Task<EmailStatus> SendVerificationAsync(string email)
{
var request = new VerifyEmailIdentityRequest
{
EmailAddress = email
};
await _ses.VerifyEmailIdentityAsync(request);
return new EmailStatus(false, VerificationEmailWarning);
}
private async Task<EmailStatus> CheckEmailAsync(string email)
{
var request = new GetIdentityVerificationAttributesRequest
{
Identities = new List<string> { email }
};
var response = await _ses.GetIdentityVerificationAttributesAsync(request);
var attributes = response.VerificationAttributes;
IdentityVerificationAttributes verificationAttributes;
if (attributes.TryGetValue(email, out verificationAttributes))
{
if (verificationAttributes.VerificationStatus == VerificationStatus.Success)
{
return new EmailStatus(true, string.Empty);
}
}
return await SendVerificationAsync(email);
}
}
}