What is Twilio?
Let me give you a brief introduction to Twilio. In short, Twilio provides a cloud API for voice and SMS communications that leverages existing web development skills, resources and skills. Twilio offers a pay as you go, affordable no contract plan for your business to make and receive calls and SMS messages. They use your existing web development skills, resources and infrastructure to improve on your marketing campaigns.
What can you do with Twilio Cloud Connector?
Let’s say you want to send SMS messages to your customers to let them know about a new product you are releasing or special discounts you want to offer them. Most likely the customer data will be stored in a database, for this example I will use a Mongo database (if you don’t know about Mongo, just think of it as any other database). How much code or time you think you need to do this? Less than you think:
So we will be using two connectors actually: Mongo DB and Twilio. You can combine any number of connectors to fit your needs, check our available connectors section often because new ones are being released frequently!
The example is pretty straightforward, let’s go step by step.
First we declare our Twilio credentials and the Mongo DB configuration settings. (See the MongoDB connector.) These settings are necessary to establish the connection with Twilio server and your Mongo database server respectively. You need to declare them only once and they will be used for all the connections.
So far so good but I want to send SMS messages! Ok hold on, here it is:
A Mule flow is created to retrieve the customer records from the database and for each one of them call Twilio connector to send a SMS message with a sample text on it. Don’t know what a Mule flow is? This link will give you an idea. Basically, a Mule flow is a mechanism that enables orchestration of services using the sophisticated message flow capabilities of Mule ESB.
Now that we somehow defined what a Mule flow is and what it’s used for in the example, let me explain each line inside of it.
This is a simple one. Using Mongo connector we obtain all the data from a collection called ‘clients’. We could have added a search criteria as well to narrow down the results.
This tag sends each member of the clients collection to the next message processor as separate messages. Check this link for more information on routing messages.
Finally, for each customer we call Twilio connector to send a SMS message. In the attribute “from” you need to put an SMS enabled Twilio phone number. In the “to” attribute we pick the phone number from the payload which is filled with each customer information by the collection splitter. Likewise, to create the body of the text message we use the name of the customer just to make it more personalized.
Running this example is as simple as typing this URL into your favorite browser http://localhost:9090/send-sms
Want to try out Twilio Cloud Connector?
So, what if you want to use the cloud connector in your Mule app? Really easy, first add the following snippet to your Maven POM:
then add the connector as a dependency:
and finally in you declare the namespace for Twilio connector in your flow:
If you want to downlaod the source code for this connector check the Github repository.
This post only shows one of the many cool things you can do with Twilio connector. Besides sending SMS messages you can query meta-data about your account, phone numbers, calls, text messages, and recordings and do fancy things like initiate outbound calls. For more information check the Twilio connector page.
If you have further questions you can post a question in the forum.
Follow me on Twitter @federecio
No related posts.