Last active
December 28, 2015 04:18
-
-
Save ktoraskartwilio/7441027 to your computer and use it in GitHub Desktop.
Message Sender Rotator.
Randomly picks a phone number to send the message "From"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.message.BasicNameValuePair; | |
import com.twilio.sdk.TwilioRestClient; | |
import com.twilio.sdk.TwilioRestException; | |
import com.twilio.sdk.resource.instance.Account; | |
import com.twilio.sdk.resource.instance.Message; | |
import com.twilio.sdk.resource.factory.MessageFactory; | |
public class MessageRotator { | |
/* Find your sid and token at twilio.com/user/account */ | |
public static final String ACCOUNT_SID = "AC123"; | |
public static final String AUTH_TOKEN = ""; | |
public static String[] phoneNumberList = { "+14159351000", "+14159351001", | |
"+14159351002", "+14159351003", "+14159351004", "+14159351005", | |
"+14159351006" }; | |
public static void main(String[] args) throws TwilioRestException { | |
int sendMessagesCount = 0; | |
TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); | |
// sending upto 200 messages | |
while (sendMessagesCount < 200) { | |
// get the phone number to send message "From" | |
String fromNumber = getFromNumber(); | |
Account account = client.getAccount(); | |
MessageFactory messageFactory = (MessageFactory) account.getMessageFactory(); | |
List<NameValuePair> params = new ArrayList<NameValuePair>(); | |
params.add(new BasicNameValuePair("To", "+14159352345")); // Replace with a valid phone number for your account. | |
params.add(new BasicNameValuePair("From", fromNumber)); // Replace with a valid phone number for your account. | |
params.add(new BasicNameValuePair("Body", "Where's Wallace?")); | |
Message sms = messageFactory.create(params); | |
System.out.println(sms.getSid()); | |
sendMessagesCount++; | |
} | |
} | |
/** | |
* Randomly returns a phone number | |
* from the phone number list | |
* | |
* @return phone number | |
*/ | |
public static String getFromNumber() { | |
Random randomGenarator = new Random(); | |
return phoneNumberList[randomGenarator.nextInt(phoneNumberList.length)]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment