Skip to content

Commit 685a057

Browse files
committed
SNS email notifications
1 parent 5370503 commit 685a057

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

.ebextensions/env.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
option_settings:
2+
aws:elasticbeanstalk:customoption:
3+
NotificationEmail: [email protected]
24
aws:elasticbeanstalk:application:environment:
35
AWS_REGION: '`{"Ref" : "AWS::Region"}`'
6+
NOTIFICATION_TOPIC: '`{"Ref" : "NotificationTopic"}`'

.ebextensions/sns-topic.config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Resources:
2+
NotificationTopic:
3+
Type: AWS::SNS::Topic
4+
Properties:
5+
Subscription:
6+
- Endpoint:
7+
Fn::GetOptionSetting: {DefaultValue: [email protected], OptionName: NotificationEmail}
8+
Protocol: email

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030
compile("org.springframework.boot:spring-boot-starter-web")
3131
testCompile("org.springframework.boot:spring-boot-starter-test")
3232
compile("com.amazonaws:aws-java-sdk-dynamodb")
33+
compile("com.amazonaws:aws-java-sdk-sns")
3334
testCompile("junit:junit:4.11")
3435
compile("com.fasterxml.jackson.core:jackson-databind:2.8.4")
3536
}

src/main/java/scorekeep/MoveFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public Move newMove(String sessionId, String gameId, String userId, String moveT
5555
} catch ( ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new RulesException(rulesName); }
5656
// save new game state
5757
State newState = new State(stateId, sessionId, gameId, newStateText, newTurn);
58+
// send notification on game end
59+
if ( newStateText.startsWith("A") || newStateText.startsWith("B")) {
60+
Utils.sendNotification("Scorekeep game completed", "Winner: " + userId);
61+
}
5862
// register state and move id to game
5963
gameController.setGameMove(sessionId, gameId, moveId);
6064
gameController.setGameState(sessionId, gameId, stateId);

src/main/java/scorekeep/Utils.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package scorekeep;
2+
3+
import com.amazonaws.regions.Regions;
4+
import com.amazonaws.services.sns.AmazonSNS;
5+
import com.amazonaws.services.sns.AmazonSNSClientBuilder;
6+
import com.amazonaws.services.sns.model.PublishRequest;
7+
import com.amazonaws.services.sns.model.PublishResult;
8+
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
public class Utils {
13+
private static final Logger logger = LoggerFactory.getLogger("scorekeep.Utils");
14+
private static AmazonSNS snsclient = AmazonSNSClientBuilder.standard()
15+
.withRegion(Regions.fromName(System.getenv("AWS_REGION")))
16+
.build();
17+
/*
18+
* Send a notification email.
19+
*/
20+
public static void sendNotification(String subject, String body) {
21+
String topicarn = System.getenv("NOTIFICATION_TOPIC");
22+
PublishRequest publishRequest = new PublishRequest(topicarn, body, subject);
23+
PublishResult publishResult = snsclient.publish(publishRequest);
24+
logger.info("Email sent: " + publishResult.getMessageId());
25+
}
26+
}

0 commit comments

Comments
 (0)