SHARE
TWEET

Untitled

a guest Jun 16th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package prizedraw;
  2.  
  3. import com.twilio.Twilio;
  4. import com.twilio.base.ResourceSet;
  5. import com.twilio.rest.api.v2010.account.Message;
  6. import com.twilio.type.PhoneNumber;
  7. import org.joda.time.DateTime;
  8.  
  9. import java.util.Collections;
  10. import java.util.stream.Collectors;
  11. import java.util.stream.StreamSupport;
  12.  
  13. import static java.util.stream.Collectors.toList;
  14.  
  15. public class TwilioRaffle {
  16.  
  17.     private static final DateTime DRAW_START = new DateTime(2019,5,27,9,0);
  18.     private static final DateTime DRAW_END   = new DateTime(2019,5,28,16,30);
  19.  
  20.     private static final int NUMBER_OF_WINNERS = 3;
  21.     private static final PhoneNumber CONTEST_NUMBER = new PhoneNumber(System.getenv("CONTEST_NUMBER"));
  22.  
  23.     private static final String CONGRATULATIONS_YOU_WON_MESSAGE = "\uD83C\uDF89 Congratulations - you won a prize in the Twilio Prize Draw at JBcnConf \uD83C\uDF89\n\n" +
  24.         "Head to the Twilio booth and show us this message to collect your prize \uD83C\uDF8A - we will give them out in the order you arrive so don't delay.";
  25.  
  26.  
  27.     public static void main(String[] args) {
  28.  
  29.         Twilio.init(System.getenv("ACCOUNT_SID"), System.getenv("AUTH_TOKEN"));
  30.         ResourceSet<Message> allMessages = Message.reader().setTo(CONTEST_NUMBER).read();
  31.  
  32.         StreamSupport.stream(allMessages.spliterator(), false)           // Create a java.util.stream.Stream
  33.             .filter( m -> m.getDateSent().isAfter(DRAW_START))           // Filter out entries which were too early...
  34.             .filter( m -> m.getDateSent().isBefore(DRAW_END))            // ...or too late.
  35.             .map(Message::getFrom)                                       // Take the FromNumber from each message.
  36.             .distinct()                                                  // Remove duplicates, then...
  37.             .collect(Collectors.collectingAndThen(toList(),              // ...collect into a list so that...
  38.                 list -> {
  39.                     Collections.shuffle(list);                           // ...we can shuffle the order.
  40.                     return list.stream();
  41.                 }))
  42.             .limit(NUMBER_OF_WINNERS)                                    // Make sure we don't select too many winners
  43.             .forEach( winner -> {
  44.                 Message.creator(
  45.                     winner, CONTEST_NUMBER,
  46.                     CONGRATULATIONS_YOU_WON_MESSAGE).create();           // Send a message to all the winners!
  47.             });
  48.     }
  49. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top