Seattle ColdFusion User Group Logo image
Seattle ColdFusion User Group Logo image
call us
206-852-6022
CALL US ABOUT MEETINGS MEMBERS JOBS PRESENTATIONS RESOURCES CONTACT

Sending SMS Text Messages Using Twilio and ColdFusion


Technologies Used

  1. Adobe ColdFusion
  2. Twilio SMS Service
  3. jQuery
  4. Bootstrap
  5. Fort Awesome (https://www.fortawesome.com - for form icons)

Prerequisities

  1. Create a Twilio Account at https://www.twilio.com/try-twilio
  2. Use the free phone number provided (good for 15 days - as of 6/23/2022) or buy a phone number
  3. Record Your Account SID (username) and Auth Token (password) from your Twilio Account Dashboard

Twilio SMS Endpoint

https://api.twilio.com/2010-04-01/Accounts/{Account SID}/Messages.json

Variables:

  • Application.TwilioSMSResource = https://api.twilio.com/2010-04-01/Accounts/{Your Twilio Account SID}/Messages.json
  • Application.TwilioUserName = {Your Twilio Account SID}
  • Application.TwilioPassword = {Your Twilio Auth Token}
  • Application.TwilioStatusCallback = The calling URL from your application - example: https://www.seattlecfug.org/demos/twilioSMS
  • Application.TwilioFrom = {Your Twilio Phone Number}

The Form Code

				
<form role="form" name="frmTwilioSMSScriptBased" id="frmTwilioSMSScriptBased" class="mt-4" novalidate autocomplete="off">						
	<div class="form-row">
		<div class="form-group col">
			<label for="fName" id="fnameLabel" class="formLabel">first name</label>
			<div class="input-group">
			  <span class="input-group-addon"><i class="faw faw-user-tie"></i></span>
			  <input type="text" name="fname" id="fname" class="form-control" placeholder="first name">
			</div>

			<div class="formError" id="fnameError">Please enter your first name (up to 21 letters, spaces, apostrophes and dashes allowed)</div>
		</div>
	</div>
	<div class="form-row">
		<div class="form-group col">
			<label for="lname" id="lnameLabel" class="formLabel">last name</label>
			<div class="input-group">
			  <span class="input-group-addon"><i class="faw faw-user-tie"></i></span>
			  <input type="text" name="lname" id="lname" class="form-control" placeholder="last name">
			</div>

			<div class="formError" id="lnameError">Please enter your last name (up to 21 letters, spaces, apostrophes and dashes allowed)</div>
		</div>
	</div>
	<div class="form-row">
		<div class="form-group col">
			<label for="phone" id="phoneLabel" class="formLabel">phone</label>
			<div class="input-group">
				<span class="input-group-addon">i class="faw faw-phone-glyphicon"</i></span>
				<input type="text" class="form-control" placeholder="phone" aria-label="Phone Number" name="phone" id="phone">
			</div>

			<div class="formError" id="phoneError">Please enter your phone number</div>
		</div>	
	</div>

	<div class="form-row">
		<div class="form-group col">
			<label for="message" id="messageLabel" class="formLabel">message</label>
			<textarea class="form-control" name="message" placeholder="your message"</textarea>
			<div class="formError" id="messageError">Please enter your message</div>
		</div>	
	</div>
	<div class="form-row">
		<div class="form-group col" id="frmMessage"></div>
	</div>
	<div class="form-row">
		<div class="form-group col">
			<button class="btn btn-primary" type="button" id="btnSubmitSMS"><i class="faw faw-send">Request Information</button>
		</div>
	</div>
</form>
				
			

Code to process the form

			
<cftry>
	<cfscript>
		if (NOT IsDefined("Form.FName") OR NOT IsDefined("Form.LName") OR NOT IsDefined("Form.Phone") OR NOT IsDefined("Form.Message") OR NOT IsValid("regex",Form.FName,"^[a-zA-Z' -]{1,21}$") OR NOT IsValid("regex",Form.LName,"^[a-zA-Z' -]{1,22}$") OR NOT IsValid("telephone",Form.Phone)) {
			abort;
		}
		VARIABLES.tmi = {};
		VARIABLES.tmi.fromPhone = Application.TwilioFrom;
		VARIABLES.tmi.toPhone = Form.Phone;
		VARIABLES.tmi.message = 'Message from ' & GetSafeHTML(Form.FName) & ' ' & GetSafeHTML(Form.LName) & ': ' & GetSafeHTML(Form.Message);
		VARIABLES.tmi.TwilioSMSResource = Application.TwilioSMSResource;
		VARIABLES.tmi.TwilioUserName = Application.TwilioUserName;
		VARIABLES.tmi.TwilioPassword = Application.TwilioPassword;
		VARIABLES.tmi.StatusCallback = Application.TwilioStatusCallback;
		VARIABLES.post = Application.TwilioSMSService.sendSMSTextCFScript(VARIABLES.tmi);

		if (VARIABLES.post.statusCode EQ '201 Created') {
			writeOutput(1);
		}
		else {
			writeOutput(VARIABLES.post.statusCode);
		}
	</cfscript>
	<cfcatch type="Any">
		<cfdump var="#cfcatch#">
	</cfcatch>
</cftry>
			
			

Code to submit the SMS Text Message to Twilio (cfscript):

			
<cfcomponent>
	<cfscript>
		public struct function sendSMSTextCFScript(required struct tmi) { 
			cfhttp(method="POST", charset="utf-8", url="#ARGUMENTS.tmi.twilioSMSResource#", username="#ARGUMENTS.tmi.twilioUserName#", password="#ARGUMENTS.tmi.twilioPassword#", result="status") {
				cfhttpparam(name="From", type="formfield", value="#ARGUMENTS.tmi.fromPhone#");
				cfhttpparam(name="To", type="formfield", value="#ARGUMENTS.tmi.toPhone#");
				cfhttpparam(name="Body", type="formfield", value="#ARGUMENTS.tmi.message#");
				cfhttpparam(name="StatusCallback", type="formfield", value="#ARGUMENTS.tmi.statusCallback#");
			}

			return status; 
		}	
	</cfscript>
</cfcomponent>
			
			

Code to submit the SMS Text Message to Twilio (tag-based):

Credit for this code can be attributed to Ben Nadel's post at https://www.bennadel.com/blog/1966-sending-sms-text-messages-from-twilio-using-coldfusion.htm. His example accounted for the majority of this sample code. Thank you Ben!
			
<cfcomponent>
	<cffunction name="sendSMSText" access="public" returntype="struct" hint="This method is used to send a SMS message using Twilio">
		<cfargument name="tmi" type="struct" required="yes" hint="This argument expects a structure of information to include in a text message">
		<--- Credit for this code can be attributed to Ben Nadel's post at https://www.bennadel.com/blog/1966-sending-sms-text-messages-from-twilio-using-coldfusion.htm. His example accounted for the majority of this sample code. Thank you Ben! --->
		<cfhttp
			result="status"
			method="post"
			url="#ARGUMENTS.tmi.twilioSMSResource#"
			username="#ARGUMENTS.tmi.twilioUsername#"
			password="#ARGUMENTS.tmi.twilioPassword#">

			
			<cfhttpparam
				type="formfield"
				name="From"
				value="#ARGUMENTS.tmi.fromPhone#"
				/>

			
			<cfhttpparam
				type="formfield"
				name="To"
				value="#ARGUMENTS.tmi.toPhone#"
				/>

			
			<cfhttpparam
				type="formfield"
				name="Body"
				value="#ARGUMENTS.tmi.Message#"
				/>

			<cfhttpparam
				type="formfield"
				name="StatusCallback"
				value="#ARGUMENTS.tmi.StatusCallback#"
				/>
		</cfhttp>
		
		<cfreturn status>
	</cffunction>
</cfcomponent>
			
			

Response from Twilio:

Twilio SMS Response

The Demo

Please enter your first name (up to 21 letters, spaces, apostrophes and dashes allowed)
Please enter your last name (up to 21 letters, spaces, apostrophes and dashes allowed)
Please enter your phone number
Please enter your message