TaCode Tuesdays: 8 Simple Ways You Can Use SMS And Voice (Series 3 of 4)

Posted by Pedram Mohammadi on July 19, 2016

tacode-tues-blog-wk1.jpg

Welcome back to TaCode Tuesdays! This is the only place you can find snippets of code for use in your very own text/voice apps, along with a weekly dose of taco puns. I’m a developer here at Zang and not only am I a big fan of tacos (if that wasn’t already apparent), I’m also a fan of open source. My goal is to share a new app idea each week that you're free to use “as is” or modify and use as the basis for your next app—no coding ability required.

Last week I continued our new series called “8 Simple Ways You Can Use SMS And Voice,” which is, of course, part of our “TaCode Tuesdays” feature. In case you missed it, check out last week’s post for an outline of two different apps, and if you’d like to learn how to get started on Zang, take a look at our very first post.

This week I’ll share the code for two apps: one for SMS (text message) reminders, and another for mobile number verification.

Same deal as last week: Each bit of code will be presented in three different coding languages. If you know a particular language, then the code is there for you to use and expand upon. If you’re not familiar with any of these languages, then no problem - each example comes with a “Helper Library” so you’ll be able to make sense of the code that we’re providing.

This week I’m going to share the code for two apps: one for recording and transcribing phone calls, and another for creating a professional-sounding IVR (interactive voice response) menu.

And there’s more: Each bit of code will be presented in three different coding languages. If you know a particular language, then the code is there for you to use and expand upon. If you’re not familiar with any of these languages, then no problem - each example comes with a “Helper Library” so you’ll be able to make sense of the code that we’re providing.

Let’s Taco ’Bout the Apps

[#1] SMS reminder

Everybody texts nowadays - it’s the new normal! If you run a business and you need to remind a client or customer of something, good luck trying to remind them via email or a phone call. The app outlined below will enable you to send customers SMS (or text) reminder messages, for instance, if they have an appointment scheduled for tomorrow afternoon, or if there’s an important product launch or event coming up. Practical and useful!

Here’s how the app works. You’ll find the code (PHP, Python and Ruby) examples below:

  1. The customer receives a text message notification, based on a date that you define... pretty simple.
PHP Python Ruby
require_once 'library/TelApi.php';
// Always use singleton design pattern
$telapi = TelApi::getInstance();
// Set TelAPI credentials
$telapi -> setOptions(array(
  'account_sid' => 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'auth_token'  => 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
));
$dateToRemind = 'DESIRED DATE HERE';
if ($dateToRemind == date('Y-m-d'))
{
  $sms_message = $telapi->create('sms_messages', array(
// Make sure the 'From" number is one of your Zang numbers
  'From' => '5555555555',
  'To' => '5555555555',
  'Body' => 'This is a reminder of your upcoming appointment.'
  ));
}
else
{
echo "It's not time to remind them yet.";
}
from telapi import rest
from datetime import datetime
 
account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
client = rest.Client(account_sid, auth_token)
account = client.accounts[client.account_sid]
 
if dateToRemind == datetime.now():
sms_message = account.sms_messages.create(
# Make sure the 'From" number is one of your TelAPI DIDs
from_number='5555555555'
to_number='5555555555'
body='This is a reminder of your upcoming appointment.'
)
else:
print "It's not time to remind them yet."
def reminder
 
require 'telapi'
 
Telapi.config do |config|
config.account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
config.auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
end
 
if dateToRemind == Time.now
# Send SMS via TelAPI's REST API
# Make sure the 'From" number is one of your TelAPI DIDs
Telapi::Message.create(number_to_send_to,
number_to_send_from,
'This is a reminder of your upcoming appointment.' )
else
puts "It's not time to remind them yet."
end
end
 Download PHP helper  Download Python helper  Download Ruby helper

 

Before I move on to the next app, here’s your taco tip for the week:

If you're going for authentic tacos, use 100 percent corn tortillas, but if you're making large tacos (because why not?) you'll need a tortilla with some wheat flour. (The gluten makes it more pliable and holds everything together better.)


[#2] Mobile Number Validation

These days, many marketers prefer using SMS over email to send out personalized offers or information to their customers. But are you sure the numbers on your list are valid mobile numbers? Successful marketing campaigns rely on having accurate information, so it's important to verify that your database  contains only mobile numbers capable of receiving SMS. That’s where this app comes in handy.

Here’s how it works - you’ll find the code (PHP, Python and Ruby) below:

  1. The app checks a number and returns if it's 'Mobile' or 'Not mobile' ... super easy
PHP Python Ruby
require_once 'library/TelApi.php';
 
// Always use singleton design pattern
$telapi = TelApi::getInstance();
 
// Set TelAPI credentials
$telapi -> setOptions(array(
'account_sid' => 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'auth_token' => 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
));
 
// Make a request to TelAPI's Carrier Lookup
$carrier_resource = $telapi->get('carrier', array(
'PhoneNumber' => '15555555555'
));
 
// Assign the IsMobile boolean value to the is_mobile variable
$is_mobile = $_GET['IsMobile'];
 
if($is_mobile == "True")
{
echo "This number is a mobile number. SMS enabled.";
}
else
{
echo "This number is not a mobile number. Not SMS enabled.";
}
from flask import Flask, request
from telapi import rest
 
account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
client = rest.Client(account_sid, auth_token)
account = client.accounts[client.account_sid]
 
@app.route('/call/check-mobile')
def check_mobile():
# Make a call to TelAPI's REST endpoint for Carrier Lookups
carrier_details = account.carrier.create(
'phone_number' = '5555555555'
)
 
# Assign the IsMobile request param to the is_mobile variable
is_mobile = request.args.get("IsMobile")
 
if is_mobile == "True":
print "This number is a mobile number. SMS enabled."
else:
print "This number is not a mobile number. Not SMS enabled."
require 'telapi'
 
Telapi.config do |config|
config.account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
config.auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
end
 
# Make a request to TelAPI's Carrier Lookup
Telapi::Carrier.lookup(phone_number)
 
# Assign the IsMobile boolean value to the is_mobile variable
is_mobile = params["IsMobile"]
 
if is_mobile == "True"
puts "This number is a mobile number. SMS enabled."
else
puts "This number is not a mobile number. Not SMS enabled."
end
end
 Download PHP helper  Download Python helper  Download Ruby helper

 

If you have any thoughts about the apps or just want to share a taco tip of your own, you can comment below. Be sure to check in again next week for the next installment of TaCode Tuesdays. If you want a reminder, sign up to get notifications of new blog posts. In part four of this series I’ll be sharing code for multiple apps once again - don't miss it!

Topics: Communication Apps, Ideas, TaCode Tuesday, cPaaS