Skip to content

Instantly share code, notes, and snippets.

@mjg123
Created June 5, 2019 12:52
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mjg123/438436cea8f9f05678b93550c06fbf2e to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>lol.gilliard</groupId>
<artifactId>jbcn19-twilio-prizedraw</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.release>11</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<version>7.37.4</version>
</dependency>
</dependencies>
</project>
package prizedraw;
import com.twilio.Twilio;
import com.twilio.base.ResourceSet;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
import org.joda.time.DateTime;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import static java.util.stream.Collectors.toList;
public class TwilioRaffle {
private static final DateTime DRAW_START = new DateTime(2019,5,27,9,0);
private static final DateTime DRAW_END = new DateTime(2019,5,28,16,30);
private static final int NUMBER_OF_WINNERS = 3;
private static final PhoneNumber CONTEST_NUMBER = new PhoneNumber(System.getenv("CONTEST_NUMBER"));
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" +
"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.";
public static void main(String[] args) {
Twilio.init(System.getenv("ACCOUNT_SID"), System.getenv("AUTH_TOKEN"));
ResourceSet<Message> allMessages = Message.reader().setTo(CONTEST_NUMBER).read();
StreamSupport.stream(allMessages.spliterator(), false) // Create a java.util.stream.Stream
.filter( m -> m.getDateSent().isAfter(DRAW_START)) // Filter out entries which were too early...
.filter( m -> m.getDateSent().isBefore(DRAW_END)) // ...or too late.
.map(Message::getFrom) // Take the FromNumber from each message.
.distinct() // Remove duplicates, then...
.collect(Collectors.collectingAndThen(toList(), // ...collect into a list so that...
list -> {
Collections.shuffle(list); // ...we can shuffle the order.
return list.stream();
}))
.limit(NUMBER_OF_WINNERS) // Make sure we don't select too many winners
.forEach( winner -> {
Message.creator(
winner, CONTEST_NUMBER,
CONGRATULATIONS_YOU_WON_MESSAGE).create(); // Send a message to all the winners!
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment