Last active
December 31, 2015 22:09
-
-
Save ktoraskartwilio/8051575 to your computer and use it in GitHub Desktop.
Message alert notifications generation code.
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
package com.twilio.demo; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Iterator; | |
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.IncomingPhoneNumber; | |
import com.twilio.sdk.resource.instance.Message; | |
import com.twilio.sdk.resource.list.IncomingPhoneNumberList; | |
import com.twilio.sdk.resource.factory.MessageFactory; | |
public class MessageAlerts { | |
/* Find your sid and token at twilio.com/user/account */ | |
public static final String ACCOUNT_SID = System.getenv("ACCOUNT_SID"); | |
public static final String AUTH_TOKEN = System.getenv("AUTH_TOKEN"); | |
// callerIdList: Obtained from the REST API | |
public static List<IncomingPhoneNumber> callerIdList; | |
// toPhoneNumberList: Alert Recipient phone number resource | |
public static List<String> toPhoneNumberList = Arrays.asList(new String[] {""}); | |
// Message Body | |
public static String messageBody = "Your appointment reminder"; | |
public static void main(String[] args) throws TwilioRestException { | |
TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); | |
Account account = client.getAccount(); | |
// List of CallerId's for this account | |
callerIdList = getCallerIds(client); | |
// Iterate through the Alert Recipient phone numbers | |
Iterator<String> toPhoneNumberItr = toPhoneNumberList.iterator(); | |
while (toPhoneNumberItr.hasNext()) { | |
// Randomly selected phone number from callerIdList | |
String fromNumber = getFromNumber(); | |
// Alert Recipient phone number | |
String toNumber = toPhoneNumberItr.next(); | |
// Send out alert message | |
sendMessage(fromNumber, toNumber, account); | |
} | |
} | |
/** | |
* Creates and sends an outgoing message | |
* | |
* @param fromNumber | |
* @param toNumber | |
* @param account | |
*/ | |
public static void sendMessage(String fromNumber, String toNumber, Account account) { | |
MessageFactory messageFactory = (MessageFactory) account.getMessageFactory(); | |
List<NameValuePair> params = new ArrayList<NameValuePair>(); | |
params.add(new BasicNameValuePair("To", toNumber)); | |
params.add(new BasicNameValuePair("From", fromNumber)); | |
params.add(new BasicNameValuePair("Body", messageBody)); | |
try { | |
Message sms = messageFactory.create(params); | |
System.out.println(sms.getSid()); | |
} catch (TwilioRestException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Iterates over the IncomingPhoneNumberList | |
* Returns a List of IncomingPhoneNumber objects | |
* | |
* @param client | |
* @return List<IncomingPhoneNumber> | |
*/ | |
public static List<IncomingPhoneNumber> getCallerIds(TwilioRestClient client) { | |
IncomingPhoneNumberList callerIds = client.getAccount().getIncomingPhoneNumbers(); | |
List<IncomingPhoneNumber> callerIdList = new ArrayList<IncomingPhoneNumber>(); | |
Iterator<IncomingPhoneNumber> callerIdItr = callerIds.iterator(); | |
while(callerIdItr.hasNext()) { | |
callerIdList.add(callerIdItr.next()); | |
} | |
return callerIdList; | |
} | |
/** | |
* Randomly returns a phone number | |
* from the phone number list | |
* | |
* @return phone number | |
*/ | |
public static String getFromNumber() { | |
Random randomGenarator = new Random(); | |
return callerIdList.get(randomGenarator.nextInt(callerIdList.size())).getPhoneNumber(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment