Thursday, May 16, 2013

Using Twilio to answer phone calls


In a previous post, I wrote about how to use Twilio to send an SMS message. This time, I'm going to show you how you can use Twilio in conjunction with a web app to answer phone calls made to your Twilio phone number.

First, you will want to have a Twilio developer account. If you did not create one last time, go ahead and create one now.

The Twilio dashboard provides a place for you enter an address to send HTTP POST requests. Using the App.HandleSpecialURL event handler, you can send these requests to a web application running on a publicly available server.

To get started, you need to create a web application. In the App object, go to the HandleSpecialURL event handler. This event handler has one parameter: Request As WebRequest. You can use this parameter to handle the request sent by Twilio and to send information back to Twilio.

Twilio uses something they call TwiML (Twilio Markup Language), which is essentially XML with Twilio-specific commands.

In this case, the special command you want to use is the Say command. This command will speak the provided text to the person that called the number. It is wrapped in a Response, so some simple TwiML looks like this:

<Response>
  <Say>This call was answered by my web application!"</Say>
</Response>

Going back to the HandleSpecialURL event handler, you just need to return the TwiML as part of the request. This is done using the Print method:

If Request.Path = "twilio" Then
  // Use TwiML to say something
  Dim now As New Date
  Request.Print("<Response>" + EndOfLine)
  Request.Print("<Say>This call was answered by my web application!</Say>" + EndOfLine)
  Request.Print("<Say>The time is " + Now.LongTime + "</Say>" + EndOfLine)
  Request.Print("</Response>")
   
  Return True
End If

Notice that I have not created a web page. That's because this web app won't use one as its only purpose is to handle direct requests using HandleSpecial URL.

Give this app a name (TwilioCall), built it as a standalone app (or CGI if you prefer) for your deployment platform. 

Now you need to configure Twilio to use this web app.  Go back to your Twilio Dashboard and navigate to Dev Tools and select TWIML apps. Click "Create TWiML App" to create a new app.



This opens a screen to specify information about your web app:



In the "Friendly Name" field just give your app a name such as TwilioCall. In the Voice section, you specify the URL for your uploaded web app in the "Request URL" field. The URL of your app will be something like this:

http://www.example.com:8080/special/twilio

With all this in place, you can now call your Twilio phone number. Twilio will then send a request to your web app which will respond by sending back a request to speak some text and the current time.

You can find more information about TwiML here: https://www.twilio.com/docs/api/twiml

An example project demonstrating this will be included with Xojo and is also available here.

Wednesday, May 15, 2013

Calling a SOAP web service

SOAP web services, although not as common as they once were, are still readily available on the web. And they can be incredibly easy to use with your apps.

Here is a quick example of how you would use a SOAP web service to get the conversion rate between currencies using the SOAP web service described here:

http://www.webservicex.net/CurrencyConvertor.asmx

The above page describes the method call that is available to you and also provides information about the various currencies available for conversion.

Click the "ConversionRate" method to get more information on how you would call this. This opens a new page (scroll down) with a way to test the web service and a list of the various ways that it can be called.

In the SOAP 1.1 section, you can see that the function is called "ConversionRate" and that it takes two parameters: FromCurrency and ToCurrency, both of which are simply a string containing the currency code.

The last thing to determine is the actual URL to use. Often, web services use the "WSDL" query string to fetch the Web Service Description Language for the service. In the case of this web service, you will use this URL:

http://www.webservicex.net/CurrencyConvertor.asmx?WSDL

With this information, you can use the SOAPMethod class to call the method.

To get started, create a new project and add to the default Window these controls:

  •  two TextFields to provide the from and to currency codes (FromCurrencyField and ToCurrencyField)
  • a PushButton to do the conversion (ConvertButton)
  • a TextField to display the results (ConversionRateField)

Now you can put this code in the Action event handler of the PushButton:


  Dim sm As New SOAPMethod("http://www.webservicex.net/CurrencyConvertor.asmx?WSDL")
  
  Dim conversionRate As String
  conversionRate = sm.ConversionRate(FromCurrencyField.Text, ToCurrencyField.Text)
  
  ConversionRateField.Text = conversionRate


Run the app an enter two currency codes (USD and EUR) and then press the button to get the conversion rate (0.7761 as of today).
There are many public web services that are available for you to use with your apps. Here are two sources of many web services:

http://www.webservicex.net

http://www.xmethods.com

Keep in mind that like anything on the web, web services can go offline from time to time so be sure to check for an error condition or invalid result from any web service call.

You can download the sample project (which will be included with Xojo) here.

Tuesday, May 14, 2013

Connecting to Oracle and Microsoft SQL Server

Some of the most common questions I get from new users relate to using databases with Real Studio. We've got lots of tips and tricks on how to connect in our Documentation and videos too.  In addition to having it's own RealSQLDatabase for single user applications, Real Studio supports database server connectivity for Oracle, ODBC, Postgre, MySQL, Microsoft SQL (Windows only).

One thing to watch our for when connecting to Oracle or Microsoft SQL Server is to make sure that you are using 32 Bit versions of Oracle Instant Client or Native Client Installer for MSSQL instead of the 64 Bit versions. Real Studio currently build 32 Bit applications and cannot use the 64 Bit libraries.

Once Real Studio builds 64 Bit apps you will be able to use the 64 Bit libraries. You can read more about the schedule for 64 Bit in this post.


Friday, May 10, 2013

Using Twilio to send an SMS message



I've been asked from time to time if it is possible to send an SMS message from your application. SMS stands for Short Message Service and is also commonly referred to as a Text Message or just a Text. SMS messages can contain up to 160 characters. To send someone an SMS, you just need their mobile phone number.

So is it possible to send an SMS message from your apps? As is the case with most things, the answer is "Yes, if you know how". And often many of these things can be accomplished using a web service of some kind.

One of the easiest ways to interface with telephones, including SMS, is using a web service called Twilio.

With Twilio, you can do all kinds of things related to telephones, including making and receiving phone calls and text messages. Twilio is not a free service, but you can get a free developer account with a credit to test it out.

To start, create a free developer account at Twilio.com. I'll wait for you to come back...

Twilio has a lot of documentation on how to use their various services. To send an SMS message, you want to take a look at their SMS REST API which is here:


On this page you will see buttons with examples for a variety of languages. You can just click XML to see how to do this using XML and curl, which you can easily incorporate into your app.

When Xojo is released, it will include a sample project that demonstrates how to do this using just a few lines of a code and a handful of controls.

Sample Application

When you run the app, you enter your Twilio Account SID and AuthCode, your Twilio phone number, the phone number you want to send the SMS to and your message. You can get the Account SID and AuthCode on the main Dashboard page:

Twilio Dashboard Page

The app uses curl (from a Shell) to send the SMS so it will only work on OS X and Linux. The code is entirely contained (all 6 lines of it) in the Action event handler of SendButton.

This code takes what was entered in the window and creates the curl command (as described on the Twilio page) and then runs the command using a Shell. The XML results are displayed after the message is sent.

This is the message as it appears on my iPhone:
SMS Message appearing on iPhone

You can also download the sample project from here.