Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.
Showing posts with label twilio. Show all posts
Showing posts with label twilio. Show all posts

2013/08/21

Caller ID, Time and Temp with Twilio

The other day, my wife got a new phone, replacing her shattered iPhone 4 with a new LG Optimus. The problem is, by rotation, the upgrade was for me, so the new phone was to have my phone number. They moved it to her phone number, but that left me disconnected from the cell network. At least, I knew at the time I was disconnected, but I was less than sure that her phone was using her number.

My thought was, "It would be nice to be able to call someplace and have it tell you what number your phone is, so we could be sure." So, I wrote something.


This uses Twilio, which is essentially an API into IP telephony. I've used Twilio before, and have two projects on GitHub, Call_Me and SMS_Me, that are generally unidirectional messaging tools, but I haven't done much interactivity with the tool.

It gives the following output, which Twilio then runs through text-to-speech.


The time is currently 4 37 pm

The temperature in West Lafayette is 83 degrees Fahrenheit.

Your phone number is 1 2 3. 4 5 6. 7 8 9 0.

A few notes on the formatting first. I separate the numbers of the phone number (and that is not a real number) so that it says the number with an expected cadence. If I just put 1234567890, it'd say one billion, two hundred twenty three million, five hundred sixty-seven thousand, eight hundred and ninety. If I just broke it up to 123 456 7890, it'd say one hundred twenty three, four hundred fifty six, seven thousand eight hundred ninety. To break up the numbers so it just says the digits, you need to put the spaces. To break it up so the area code and exchange and line numbers are distinct, you need the punctuation.

Times are weird, too. If the time is 4:37, just having the time as 4 37 works, but if the time is 4:05 and broken into 4 05 instead, it'd read it as four five. This is why I use digit letter-"o" digit for the time.

I say lots of bad things about XML and prefer to avoid it when possible, but sometimes, like here, it's just that easy to handle. Kudos to XML::LibXML and NOAA for making that part reasonable and good.

Now that I have this working, I'm curious about doing other things. I can see myself adding to this, checking to see if From is my number, and if so, opening up choices like my whole Quantified Self stuff, the home automation I want to do more of, etc. I'm not sure I like the TwiML module, and might move that over to Template Toolkit, but that remains to be seen.

2012/02/14

Simple Telephony Tool with Twilo

As long-time readers of this blog might know, I'm a command-line guy, and for any given technology I start to play with, I like to have a command-line tool. And once Scott W. pointed me to Twilio, I had to go that way.

The good thing about that methodology is that it takes the part that you want to find out and combines it with stuff you pretty much know well. Because there's a lot of PHP in the Twilio documentation, I went with PHP for the project this weekend, but now that I had a moment to take a step back, I stepped to my preferred methods for this, and came up with call_me.pl.

#!/usr/bin/perl

# A simple command-line tool for making a telephone call

use 5.010 ;
use strict ;
use Carp ;
use IO::Interactive qw{ interactive } ;
use URI::Escape ;
use WWW::Twilio::API ;
use YAML qw{ LoadFile } ;

my $twilio_conf = get_twilio_conf() ;

my $status = join ' ', @ARGV ;
if ( length $status < 1 ) {
    while ( <stdin> ) {
        $status .= $_ ;
        }
    chomp $status ;
    }

if ( length $status < 1 ) {
    say { interactive } 'No content' ;
    say { interactive } length $status ;
    exit ;
    }

my $twilio = WWW::Twilio::API->new(
    AccountSid => $twilio_conf->account_sid ,
    AuthToken  => $twilio_conf->auth_token ,
    ) ;

my $url = $twilio_conf->url . uri_escape( $status ) ;

my $from     = $twilio_conf->from ;
my $to       = $twilio_conf->to ;
my $response = $twilio->POST(
    'Calls',
    From => $from,
    To   => $to,
    Url  => $url,
    ) ;

say { interactive } $status ;

exit ;

#========= ========= ========= ========= ========= ========= =========

sub get_twilio_conf{
    my  $twilio_conf = $ENV{HOME} . '/.twilio.conf' ;
    my  $config ;
    if ( defined $twilio_conf && -f $twilio_conf ) {
        $config = LoadFile( $twilio_conf ) ;
        }
    else {
        croak 'No configuration file' ;
        }
    return $config ;
    }

Now, for a few notes of explanation.

First, the modules. I use Carp for error handling, IO::Interactive to allow output when used interactively, but hide it when put into a batch or crontab. URI::Escape is used to turn the message into something query-string-ready, because the way Twilio calls work is that this call interface points Twilio to a web page that handles the actual interaction of the call. And, I'm trying YAML so I don't have to parse the configuration file myself.

To make a call, you need an auth token and account SID from Twilio, plus you need a number. You can use the sandbox number, but then you need to put in a code to make the call work, so you need to get a number from Twilio, too. I put the message URL and the number to call into the YAML, too, because I wanted to have the same connect-it-all-via-pipes method I use for twitter.pl and facebook.pl to be at work here. If your use case is calling all the server guys if there's a power outage to the server room, you will want to pull the number from somewhere else.

As I mentioned, you need a web-available TwiML application to define the interaction. In my case, it looks like this:

<?php
    header( "content-type: text/xml" ) ;
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ;
    ?>
<Response>
    <Pause length="3" />
    <Say language="en" voice="woman">
       <?php echo $_GET['message'] ; ?>
    </Say>
</Response>

As mentioned, I did the work over the weekend overwhelmingly in PHP, and this is very much based on that work.

Twilio has an XML format that determines how phone conversations are handled, with enough hooks that I can easily see how you could implement a voicemail system, an extensive phone tree, or a song-of-the-day setup. I don't have that here. Right now, what I'm seeing is a tool to find my phone when I don't have another one to call it, or a way to have my phone tell me when a long-running process ends, or even a for-me wake-up call setup. Twilio also can do SMS, which is what most of my coding this weekend was around. SMS works over phone, not IP, so it should still be able to tell me things when I have my 3G shut down to save battery life. So, as a user and not a business, I guess I'm starting to see some practical uses for  this stuff.

I will have this up on Github before too long. If you have ideas about what you could do with Twilio, I would be glad to hear it.