September 23, 2019
Using ColdFusion to send a one off SMS Message using AWS SNS (in five lines of code!)
Comments
(21)
September 23, 2019
Using ColdFusion to send a one off SMS Message using AWS SNS (in five lines of code!)
Newbie 49 posts
Followers: 42 people
(21)

This can certainly be chalked up to be a “this is why I love ColdFusion” posts.

First my obligatory disclaimer;  I’m not an AWS guru.  I’m not a Java developer per se.  Most of what I’ve learned has been through brute force, hack and slash, trial and error development.  Think of my coding style like a drunken baby and accept that this may or may not be the “right way” to accomplish this task… but it’s the way I stumbled upon.

I’m working with a client who sells travel packages to customers.  Once an agent builds a package for a customer, they email them a link where the customer can complete their purchase.  They wanted to add a text messaging feature that sends an SMS text message to their mobile device.

Amazon Web Services includes a service called Simple Notification Service, or SNS.  SNS will do much, much, MUCH more than send simple SMS text messages, but the task in my hands is to send an SMS… so I’m going to limit  the scope of its use to doing just that.

ColdFusion runs on top of Java.  ColdFusion is, essentially a Java pre-parser.  In fact, the end result of ColdFusion processing a CFM page is Java byte code that does all of the processing for your ColdFusion pages.  As such, there’s a world of extensibility available to you in the Java world that you can leverage easily using ColdFusion.

Likewise, AWS has a Java Development Kit available for download.  Between the availability of the SDK for AWS and the extensibility of ColdFusion to be able to use JAR files, this becomes a very simple task.

Setting everything up

There are some steps needed to get this up and running on ColdFusion, but they’re pretty straightforward.

  1. Create an AWS account.
  2. Create (or make sure you have) an AWS User who has privileges to use SNS.
  3. Obtain that users Access and Secret keys for using AWS.
  4. Download the AWS SDK.
  5. Copy the appropriate JAR files into your ColdFusion installation.
  6. Restart the ColdFusion service.

Specifically relating to number 5 in this list, you need the following JAR files from the SDK Download:

  • aws-java-sdk-1.11.xxx.jar
  • jackson-annotations-2.6.0.jar
  • jackson-core-2.6.7.jar
  • jackson-databind-2.6.7.1.jar
  • joda-time-2.8.1.jar

The AWS SDK .jar is located in the lib directory, and the others are in the third-party directory.  Copy these files to your {cf-root}/cfusion/lib directory.

Once the JAR files are in your ColdFusion installation, and the service has been restarted, here’s the code needed to send an SMS through AWS SNS, in its entirety:

<cfscript>
   awsCredentials = createObject('java','com.amazonaws.auth.BasicAWSCredentials').init([YOUR AWS ACCESS KEY], [YOUR AWS SECRET KEY]);
   awsStaticCredentialsProvider = createObject('java','com.amazonaws.auth.AWSStaticCredentialsProvider').init(awsCredentials);
   snsClient = createObject('java', 'com.amazonaws.services.sns.AmazonSNSClientBuilder').standard().withCredentials(awsStaticCredentialsProvider).withRegion([YOUR AWS REGION THAT SUPPORTS SMS]).build();
   publishRequest = createObject('java', 'com.amazonaws.services.sns.model.PublishRequest').init().withPhoneNumber('+1XXX5551212').withMessage('This is my Message');
   Result = snsClient.publish(publishRequest);
</cfscript>

And sure enough.  This arrived on my phone:

AWS SNS Result

BOOM! Text messages via AWS SNS in just five lines of code.

Making it Better

Now that I have a proof-of-concept, there’s a lot of things I can do to make this better.  I could make the AWS Credentials variable an application scoped variable that gets created on application start.  Heck the first three lines of my proof-of-concept could be cached in the application.  I could also write a function that I pass the message and the phone number and it just takes care of it for me.  What I have, however, is enough to prove it can work.

Alternatives

Some other ways I considered taking care of this task:

  • Twilio.com has an API for sending SMS messages that’s REST based.  I’m sure there are many other services that offer something similar.
  • Almost every mobile provider has an email to SMS gateway, so as long as you know the provider, you can send an email using cfmail and the mobile provider

Credit where Credit is Due

Brian Klass.  I’ve seen him present at CF Summit several times, and his blog has great articles on how to use AWS with ColdFusion for a multitude of services.  Pay special attention to the following articles:

Enjoy!

21 Comments
Aug 17, 2022
Aug 17, 2022

Hi

David Byers
Thank you for this tut. I was wondering if it’s possible to send an SMS or notification on a map to a marker location(lat, lng).? Thank you Rogers@rogerslimosdotcom   like so Cell Phones, SMS, Twilio, Pusher, ColdFusion, And Google Maps == Fun (bennadel.com)

Like
()
Jan 4, 2021
Jan 4, 2021

Great article for US  developers, “Almost every mobile provider has an email to SMS gateway

Might want to add ‘in the USA’, most other country providers do not support this.

You might also want to point to the AWS-CFML component in your alternatives section which would reduce the lines of code also 😉 

jcberquist/aws-cfml: Lucee/ColdFusion library for interacting with AWS API’s (github.com)

Like
()
Nov 23, 2020
Nov 23, 2020

David, thank you for your post.  I was able to get it working.  I’m trying to add message attributes to the message such as “SenderId” but I’m confused as to how to get there.  As I understand it, I need to build a message attributes object and pass that in to the “PublishRequest” method but I’m unsure how to build those objects.  I understand the java but not sure how to translate that into calls using coldfusion.  Can you help?

Like
()
(1)
Add Comment