Mass SMS broadcast in Java with Twilio

Summary

In this blog post we look at how to send SMS messages to a list of phone numbers with Twilio's API. I wanted to do it with Amazon SNS at first, however, it has no receiving and call forwarding capabilities like Twilio.

If you do not have time and have already configured your account you can have a look at the source here: https://github.com/Lougarou/smstwilio

Overview

1. Setup Account
2. Setup Phone number & Call Forwarding
3. Setup Messaging Service & Alphanumeric Name
4. Using the API in JAVA
5. Status Report on the campaign

Twilio Account

Once you sign up you will be brought to a console dashboard like the following or may be the buy phone number dashboard(I don't remember anymore but it's covered below): 


3 important things to note here is your Account SID and AUTH TOKEN which you will use when calling the API. And the Balance which will be deducted for every outgoing and incoming sms/calls.
In this tutorial we will only be dealing with receiving and redirecting calls. The minimum deposit is 20$

Phone Number

Next you need a phone number. There is a small menu to the left that be opened by clicking on the button. Depending on where you want to send SMS check the billing and create a phone number accordingly.  After you are done click on the phone number to get more settings associated to it.

Setting Up Call Forwarding

So may be you are doing mass SMS for marketing purposes but what happens when a customer tries to call the number from which he/she has received the SMS. Instead of missing out on a customer query let's setup a call forwarding so that the customer gets redirected to your support team or yourself.

Select TwiML and then click on the + sign. Then write the following and replace with your phone number. 
If you want to know more about TwiML check here https://www.twilio.com/docs/api/twiml

Setting Up Messaging Service

Go the menu -> Programmable SMS -> Messaging Services.

Create a new messaging service and note your Service ID which will also be used when calling the API. Next configure your Alpha Sender ID which will appear as the name when customer receives the SMS.

Using The API In JAVA

The full source code is available here: https://github.com/Lougarou/smstwilio
In our case we are using Maven for project management. Using Maven you can import all the required Twilio API libraries using https://mvnrepository.com/artifact/com.twilio.sdk/twilio/7.1.0
You can take a look at the pom.xml if you are confused. The source code by itself is simple and everything is packed into one class. We are using https://mvnrepository.com/artifact/commons-cli/commons-cli/1.4 for the command line interface. 

Using the Twilio API is extremely simple. You first need to initialise a Twilio object:
Twilio.init(account_sid, auth_token);
After which you can send an SMS as follows:
Message message = Message.creator(new PhoneNumber(to), serviceId, body).create();

You can compile the project by going into the directory and running mvn install This will generate a jar file in the target directory that you can use as follows:

java -jar sms-1.0-jar-with-dependencies.jar -f "\\filename.txt" -m "message here"-a "account_sid " -t "auth_token" -s "serviceId"
Filename is just one number per line in E.164 number formatting to whom you want to send the message.

Status Reporting

Cool thing about Twilio is that after you have run your SMS Campaign. You can go to the dashboard->programmable SMS->view all message logs and export to CSV to see which messages have been delivered, to whom and which ones have failed. 

Final Words

We only cover sending SMS in this blog post. If you want to see incoming SMS you can do that via the Dashboard or you can host a server which reads it.
We hope that you were able to follow all the steps and that this blog post was helpful.





Comments

  1. Nice tutorial..
    did you ever came across FreeSwitch?
    wanted to implement the same kind of project but using FreeSwitch instead, since im already using it for telephony.

    ReplyDelete
    Replies
    1. Thank you!
      No this is the first time that I am hearing about it but thank you for sharing!
      Twilio starts to cost a lot when running large campaigns(Just used it to promote JCI Curepipe's Run The Talk trail) So FreeSwitch is sounding awesome! Will definitely look into it as well :)

      Delete
  2. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular Posts