SpringMVC Banner

Twilio Service

Hello Everyone,

In this blog, I am going to explain how to integrate Twilio SMS Service in spring boot application. Twilio is a   plugin for integration with Twilio SMS Gateway API. Twilio provides following services like Voice, SIP, Client, SMS,   MMS and Short Codes etc.

For using Twilio SMS Plugin you have to first Sign Up with the Twilio. It provides a trial account in which you can only send SMS to verified numbers in your account. For using full service you have to upgrade your account.

Note −You can learn more about Twilio at www.twilio.com.

Firstly we have to add the following dependency in the pom.xml file.

Sending SMS

To send the SMS, we need to provide a from-number and to-number to . so after sign up on Twilio, it provides a number, which is free for sending the SMS to any number.

Now that we have the Twilio SDK, let’s make our Controller file a little more interesting. We want to send outgoing SMS messages from our application to our phone.

First we need to import the parts of the Twilio library that we are going to make use of. At the top of the file, add the following highlighted lines right below the rest of the import statements:

To use Twilio we need to authenticate by using some account credentials. We will do this by defining a few constants. Add the following highlighted lines to GreetingController.java:

. Be sure to use your actual Twilio credentials in the strings above. TWILIO_NUMBER should be set to a Twilio     phone number.

. Below your greeting method we will add a new method called sendSMS. This method will be called from inside of our greeting method and will be used to create and send the outgoing text message.

Place the following code below your greeting method:

Lastly we need to call this function from our greeting method. Modify the method so that it looks like the following.

Restart your server and call the api  http://localhost:8080/. The number you specified in the “To” parameter should receive an SMS with the message “Hello, World! Spring boot is so simple”.

. Hope this blog will helpful for you.