Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Let's test Amazon SNS (Simple Notification Service)
- // 1. Sign in to AWS Console and create new SNS App
- // 2. Specify AWS region (e.g. EU-CENTRAL-1 (Frankfurt))
- // 3. Create topic (e.g. "Student-Alerts", display name: "StudAlerts")
- // Amazon will generate new ARN (Amazon Resource Name) for your topic. e.g.
- arn:aws:sns:eu-central-1:760693603791:Student-Alerts
- // 4. Specify 1 or more subscriptions, e.g. email address "example@yandex.ru"
- // Amazon will send a confirmation to your e-mail, you need to confirm the subscription
- // 5. [Troubleshooting] aside from following confirmation URL in your e-mail, it seems that you also need to click
- // on 'Confirm subscription' in SNS Console and copy-paste that URL to get it work.
- // 6. Add AWS credintials in your ENV variables (please note that it may be done via source code as well):
- export AWS_ACCESS_KEY_ID=BVSAJ7UKCRP53EXTFQ2A
- export AWS_SECRET_ACCESS_KEY=4hg17YxGUyqpFIRTa/k2hwvco8QwqxT/KVH0HwdB
- // 7. Preliminary work is done! Let's write a code:
- // build.sbt:
- libraryDependencies += "com.lightbend.akka" %% "akka-stream-alpakka-sns" % "0.18"
- // SnsStudents.scala:
- import javax.inject.Inject
- import scala.concurrent.Future
- import akka.Done
- import akka.stream.Materializer
- import akka.stream.alpakka.sns.scaladsl.SnsPublisher
- import akka.stream.scaladsl.Source
- import com.amazonaws.regions.Regions
- import com.amazonaws.services.sns.{AmazonSNSAsync, AmazonSNSAsyncClientBuilder}
- class SnsStudents @Inject() (implicit mat: Materializer) {
- protected lazy implicit val snsClient: AmazonSNSAsync = AmazonSNSAsyncClientBuilder
- .standard()
- //.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("a", "b"))) // you can also use ENV vars
- .withRegion(Regions.EU_CENTRAL_1) // may be specified here or via ENV variables
- .build()
- def pushNotification(msg: String): Future[Done] = {
- Source.single(msg).runWith(SnsPublisher.sink("arn:aws:sns:eu-central-1:760693603791:Student-Alerts"))
- }
- }
- // TestController.scala:
- import javax.inject.Inject
- import scala.concurrent.{ExecutionContext, Future}
- import play.api.libs.json._
- import play.api.mvc._
- import services._
- class MitrakovController @Inject()(
- cc: ControllerComponents,
- snsStudents: SnsStudents)
- (implicit ec: ExecutionContext) extends AbstractController(cc) {
- // add to routes: POST /push controllers.TestController.pushNotification
- def pushNotification: Action[JsValue] = Action(parse.json).async { request =>
- (request.body \ "text").asOpt[String] match {
- case Some(msg) =>
- snsStudents.pushNotification(msg) map { done =>
- Ok(s"Result is: $done")
- } recover { case ex =>
- BadRequest(s"Error: $ex")
- }
- case None => Future.successful(BadRequest("Incorrect Json: 'text' attribute expected"))
- }
- }
- }
- // 8. Now open curl, Postman or any other tool you wish and run:
- http://localhost:9000/push with json body: {"text": "We're gonna make that energy!"}
- // Email output: We're gonna make that energy!
- // --
- // If you wish to stop receiving notifications from this topic, please click or visit the link below to unsubscribe:
- // https://sns.eu-central-1.amazonaws.com/unsubscribe.html?SubscriptionArn=arn:aws:sns:eu-central-1:760693603791:Student-Alerts:2df1740c-c7b6-4643-9b1b-ea32c891cdbf&Endpoint=tom-trix@ya.ru
- // Please do not reply directly to this email. If you have any questions or comments regarding this email, please contact us at https://aws.amazon.com/support
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement