PDF Print E-mail

SMS and Ribbit


One of the newest and coolest features of the new API is the ability to send SMS messages from a Flex application. 

How great would it be to add a simple application to your Facebook page that allows your friends to send a text message to your phone? Well you can with Ribbit’s v2.5 API. It's  simple to set up in your application, and this how-to guide will step you through the process. 

Adding Ribbit to your Application

Note: The following steps (1 and 2) are the same for all Ribbit Flex tutorials. They can be skipped or shortened if they were already completed for another tutorial (e.g. copying and renaming a previously created Flex project would allow you to jump to the next section).
 
Step 1. Download the SWC
 
The first thing you need to do is download the latest SWC from Ribbit at http://developer.ribbit.com. You will need a developer account to download the latest SWC and save it to your computer.
 
Note: There are also some great sample applications that show the full range of capabilities for the Ribbit API.
 
Step 2. Install the SWC
 
Open Flex Builder and either create a new project or open an existing project. On the project folder, right click and select Properties. This will bring up the project properties dialog box where you will select the Flex Build Path. Next, click the Library Path tab to bring up your location. Click the Add SWC button to add the Ribbit SWC to the location you saved from the Ribbit developer Website (see example below).

Import SWC















Connecting to Ribbit with a Login

Step 1. Instantiate Ribbit in Flex
 
First, we need to instantiate a Ribbit object in our project to access the methods and events available to us in the API. We do this by adding a new namespace in the Application root tag.

<mx:Application xmlns:mx.... xmlns:ribbit=”com.ribbit.api” /> 

Next, we add the Ribbit object to the project. RibbitServices is the parent class to all the methods and event classes. This allows us to import classes and add Ribbit methods and events.

<ribbit:RibbitServices id=”request” /> 
 
Step 2. Login to Ribbit
 
To use Ribbit services, you will need to have your application login to an account. To do this you will need to use the AuthenticationManager login method in the API. The login method contains a username, password, devID, and appID. Below is an example of the login method.

request.login(username, password, devID, appID);
 
Once you have logged in you can listen for the logged in event that is served back from the server. We will be using Event Listeners on the AuthenticationManager for Authentication events.

request.authenticationManager.addEventListener(AuthenticationEvent.LOGGED_IN, handlefunction); 

Adding SMS to your Application

Now that we are connected to Ribbit and have logged into our account, we can send SMS messages. Sending a message is as simple as adding a few lines of code to your application.
 
 
First, add three text boxes to allow the user to enter a mobile number, a subject, and the SMS message. Also, add a button with a click function attached to it to send off the SMS message. You can pass this to an internal function to do some error checking, like making sure the mobile number is a certain length, or you can place the Ribbit method on the button click function.

For the sendSMSMessage method located in the MessageManager, the parameters that get passed in are mobile number, message, and subject.
 
request.messageManager.sendSmsMessage( mobileNumber, message, subject);
 
That is all that is needed to send an SMS message to a mobile number.

There is an event that gets sent back to the application when the message has left the server and it's on its way to the mobile phone. The event is in the MessageEvent class and is called MESSAGE_SENT.

request.addEventListener(MessageEvent.MESSAGE_SENT, handleMessageSent);
 
Once the message has been sent, you are ready to send as many messages as you would like. As you can see, it is very simple and easy to add SMS functionality to your application using Ribbit.
 
Last Updated ( Thursday, 23 April 2009 10:56 )