From 985b06dbe3d81bcc520c10741a715ce5f5e1a0c3 Mon Sep 17 00:00:00 2001 From: shawn-speaks Date: Sat, 17 Sep 2016 17:46:53 -0400 Subject: [PATCH 1/4] temp save --- src/nyc/c4q/ramonaharrison/Bot.java | 22 +- src/nyc/c4q/ramonaharrison/Main.java | 36 ++- .../c4q/ramonaharrison/model/Attachment.java | 159 +++++++++-- src/nyc/c4q/ramonaharrison/model/Field.java | 38 +++ .../c4q/ramonaharrison/model/GiphyClass.java | 246 ++++++++++++++++++ src/nyc/c4q/ramonaharrison/model/Profile.java | 106 ++++++++ src/nyc/c4q/ramonaharrison/model/User.java | 127 +++++++-- src/nyc/c4q/ramonaharrison/network/Slack.java | 84 +++++- .../network/response/Response.java | 1 + 9 files changed, 775 insertions(+), 44 deletions(-) create mode 100644 src/nyc/c4q/ramonaharrison/model/Field.java create mode 100644 src/nyc/c4q/ramonaharrison/model/GiphyClass.java create mode 100644 src/nyc/c4q/ramonaharrison/model/Profile.java diff --git a/src/nyc/c4q/ramonaharrison/Bot.java b/src/nyc/c4q/ramonaharrison/Bot.java index 6a52dd4..3b464e9 100644 --- a/src/nyc/c4q/ramonaharrison/Bot.java +++ b/src/nyc/c4q/ramonaharrison/Bot.java @@ -2,6 +2,7 @@ import nyc.c4q.ramonaharrison.model.Channel; import nyc.c4q.ramonaharrison.model.Message; +import nyc.c4q.ramonaharrison.model.User; import nyc.c4q.ramonaharrison.network.*; import nyc.c4q.ramonaharrison.network.response.*; @@ -15,6 +16,8 @@ public class Bot { // TODO: implement your bot logic! + public String botName = "messybot"; + public Bot() { @@ -25,7 +28,7 @@ public Bot() { */ public void testApi() { Response apiTest = Slack.testApi(); - System.out.println("API OK: " +apiTest.isOk() + "\n"); + System.out.println("API OK: " + apiTest.isOk() + "\n"); } /** @@ -69,6 +72,23 @@ public void listMessages(String channelId) { } } + public void readMessages(String channelId){ + ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); + + if (listMessagesResponse.isOk()) { + List messages = listMessagesResponse.getMessages(); + + System.out.println("\nMessages: "); + +// for (Message message : messages) { /* Searches ALL messages in channel for string */ +// +// if(message.getText().equalsIgnoreCase(botName /* text you want to search for here*/)) { +// sendMessageToBotsChannel("https://www.hallaminternet.com/assets/https.jpg"); +// } + } + } + + /** * Sample method: sends a plain text message to the #bots channel. Prints a message indicating success or failure. * diff --git a/src/nyc/c4q/ramonaharrison/Main.java b/src/nyc/c4q/ramonaharrison/Main.java index 933e92b..979de3b 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -1,24 +1,42 @@ package nyc.c4q.ramonaharrison; +import java.util.*; +import nyc.c4q.ramonaharrison.model.Message; import nyc.c4q.ramonaharrison.network.Slack; +import sun.misc.resources.Messages; + +import java.util.List; public class Main { public static void main(String[] args) { + Bot aBot = new Bot(); - Bot myBot = new Bot(); + aBot.testApi(); + aBot.readMessages("C2BLV9LV6"); + Slack.getHolidaysForToday(); + +// Calendar cal = Calendar.getInstance(); +// int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); +// +// String dayOfMonthStr = String.valueOf(dayOfMonth); +// System.out.println(dayOfMonth); +// +// +// Calendar cal = Calendar.getInstance(); +// int currentYear = cal.get(Calendar.YEAR); +// +// String year = String.valueOf(currentYear); +// System.out.println(year); - myBot.testApi(); - myBot.listChannels(); - myBot.listMessages(Slack.BOTS_CHANNEL_ID); - // Post "Hello, world!" to the #bots channel - //myBot.sendMessage("Hello, world!"); - // Post a pineapple photo to the #bots channel - //myBot.sendMessage("http://weknowyourdreams.com/images/pineapple/pineapple-07.jpg"); + + // Post a pineapple photo to the #bots channel +// myBot.sendMessage("); +// myBot.sendMessage("someString"); } -} \ No newline at end of file +} diff --git a/src/nyc/c4q/ramonaharrison/model/Attachment.java b/src/nyc/c4q/ramonaharrison/model/Attachment.java index 8b9d1ef..fbe1889 100644 --- a/src/nyc/c4q/ramonaharrison/model/Attachment.java +++ b/src/nyc/c4q/ramonaharrison/model/Attachment.java @@ -1,7 +1,11 @@ package nyc.c4q.ramonaharrison.model; +import org.json.simple.JSONArray; import org.json.simple.JSONObject; +import java.util.ArrayList; +import java.util.List; + /** * Created by Ramona Harrison * on 8/26/16 @@ -13,27 +17,148 @@ public class Attachment { - // TODO: implement private fields for each of the following attachment JSON keys: - // "fallback" - // "color" - // "pretext" - // "author_name" - // "author_link" - // "author_icon" - // "title" - // "title_link" - // "text" - // "fields" - // "image_url" - // "thumb_url" - // "footer" - // "footer_icon" - // "ts" + private String fallback; + private String pretext; + private String authorName; + private String authorLink; + private String authorIcon; + private String title; + private String titleLink; + private String text; + private List fields; + private String imageUrl; + private String thumbUrl; + private String footer; + private String footerIcon; + private long ts; public Attachment(JSONObject json) { // TODO: parse an attachment from the incoming json + + if(json.containsKey("fallback")){ + this.fallback = (String) json.get("fallback"); + } + + if(json.containsKey("pretext")){ + this.pretext = (String) json.get("pretext"); + } + + if(json.containsKey("authorName")){ + this.authorName = (String) json.get("authorName"); + } + + if(json.containsKey("authorLink")){ + this.authorLink = (String) json.get("authorLink"); + } + + if(json.containsKey("authorIcon")){ + this.authorIcon = (String) json.get("authorIcon"); + } + + if(json.containsKey("authorIcon")){ + this.authorIcon = (String) json.get("author"); + } + + if(json.containsKey("title")){ + this.title = (String) json.get("title"); + } + + if (json.containsKey("titleLink")) { + this.titleLink = (String) json.get("titleLink"); + } + + if(json.containsKey("text")){ + this.text = (String) json.get("text"); + } + + if(json.containsKey("fields")){ + JSONArray jsonFields = (JSONArray) json.get("fields"); + this.fields = new ArrayList(); + for (int i = 0; i < jsonFields.size(); i++){ + Field field = new Field((JSONObject) jsonFields.get(i)); + this.fields.add(field); + } + } + + if(json.containsKey("imageUrl")){ + this.imageUrl = (String) json.get("imageUrl"); + } + + if(json.containsKey("thumbUrl")){ + this.thumbUrl = (String) json.get("thumbUrl"); + } + + if(json.containsKey("footer")){ + this.footer = (String) json.get("footer"); + } + + if(json.containsKey("footerIcon")){ + this.footerIcon = (String) json.get("footerIcon"); + } + + if (json.containsKey("ts")){ + this.ts = (long) json.get("ts"); + } + + } + + // oTODO add getters to access private fields + + public String getFallback() { + return fallback; + } + + public String getPretext() { + return pretext; + } + + public String getAuthorName() { + return authorName; + } + + public String getAuthorLink() { + return authorLink; + } + + public String getAuthorIcon() { + return authorIcon; + } + + public String getTitle() { + return title; + } + + public String getTitleLink() { + return titleLink; + } + + public String getText() { + return text; + } + + public List getFields() { + return fields; + } + + public String getImageUrl() { + return imageUrl; + } + + public String getThumbUrl() { + return thumbUrl; + } + + public String getFooter() { + return footer; + } + + public String getFooterIcon() { + return footerIcon; + } + + public long getTs() { + return ts; } - // TODO add getters to access private fields } diff --git a/src/nyc/c4q/ramonaharrison/model/Field.java b/src/nyc/c4q/ramonaharrison/model/Field.java new file mode 100644 index 0000000..d6e20f7 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Field.java @@ -0,0 +1,38 @@ +package nyc.c4q.ramonaharrison.model; + +import org.json.simple.JSONObject; + +/** + * Created by shawnspeaks on 9/11/16. + */ +public class Field { + private String title; + private String value; + private boolean isShort; + + public Field(JSONObject json) { + if (json.containsKey("title")) { + this.title = (String) json.get("title"); + } + + if (json.containsKey("value")) { + this.value = (String) json.get("value"); + } + + if (json.containsKey("isShort")){ + this.isShort = (boolean) json.get("isShort"); + } + + } + public String getTitle() { + return title; + } + + public String getValue() { + return value; + } + + public boolean isShort() { + return isShort; + } +} diff --git a/src/nyc/c4q/ramonaharrison/model/GiphyClass.java b/src/nyc/c4q/ramonaharrison/model/GiphyClass.java new file mode 100644 index 0000000..5bf3e40 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/GiphyClass.java @@ -0,0 +1,246 @@ +import org.json.simple.JSONObject; + + +/** + { + "data": { + type: "gif", + id: "Ggjwvmqktuvf2", + url: "http://giphy.com/gifs/american-psycho-christian-bale-Ggjwvmqktuvf2", + image_original_url: "http://s3.amazonaws.com/giphygifs/media/Ggjwvmqktuvf2/giphy.gif", + image_url: "http://s3.amazonaws.com/giphygifs/media/Ggjwvmqktuvf2/giphy.gif", + image_mp4_url: "http://s3.amazonaws.com/giphygifs/media/Ggjwvmqktuvf2/giphy.mp4", + image_frames: "11", + image_width: "500", + image_height: "256", + fixed_height_downsampled_url: "http://s3.amazonaws.com/giphygifs/media/Ggjwvmqktuvf2/200_d.gif", + fixed_height_downsampled_width: "391", + fixed_height_downsampled_height: "200", + fixed_width_downsampled_url: "http://s3.amazonaws.com/giphygifs/media/Ggjwvmqktuvf2/200w_d.gif", + fixed_width_downsampled_width: "200", + fixed_width_downsampled_height: "102", + fixed_height_small_url: "http://s3.amazonaws.com/giphygifs/media/Ggjwvmqktuvf2/100.gif", + fixed_height_small_still_url: "http://s3.amazonaws.com/giphygifs/media/Ggjwvmqktuvf2/100_s.gif", + fixed_height_small_width: "195", + fixed_height_small_height: "100", + fixed_width_small_url: "http://s3.amazonaws.com/giphygifs/media/Ggjwvmqktuvf2/100w.gif", + fixed_width_small_still_url: "http://s3.amazonaws.com/giphygifs/media/Ggjwvmqktuvf2/100w_s.gif", + fixed_width_small_width: "100", + fixed_width_small_height: "51" + }, + "meta": { + "status": 200, + "msg": "OK" + } + } + */ +public class GiphyClass { + + private String type; + private String id; + private String url; + private String image_url; + private String image_mp4_url; + private String image_frames; + private String image_width; + private String image_height; + private String fixed_height_downsampled_url; + private String fixed_height_downsampled_width; + private String fixed_height_downsampled_height; + private String fixed_width_downsampled_url; + private String fixed_width_downsampled_width; + private String fixed_width_downsampled_height; + private String fixed_height_small_url; + private String fixed_height_small_still_url; + private String fixed_height_small_width; + private String fixed_height_small_height; + private String fixed_width_small_url; + private String fixed_width_small_still_url; + private String fixed_width_small_width; + private String fixed_width_small_height; + + public GiphyData(JSONObject json) { + // parse data from the incoming json + + if (json.containsKey("type")){ + this.type = (String) json.get("type"); + } + + if (json.containsKey("id")) { + this.id = (String) json.get("id"); + } + + if (json.containsKey("url")) { + this.url = (String) json.get("url"); + } + + if (json.containsKey("image_url")) { + this.image_mp4_url = (String) json.get("image_url"); + } + + if (json.containsKey("image_mp4_url")) { + this.image_url = (String) json.get("image_mp4_url"); + } + + if (json.containsKey("image_frames")) { + this.image_frames = (String) json.get("image_frames"); + } + + if (json.containsKey("image_width")) { + this.image_width = (String) json.get("image_width"); + } + + if (json.containsKey("image_height")) { + this.image_height = (String) json.get("image_height"); + } + + if (json.containsKey("fixed_height_downsampled_url")) { + this.fixed_height_downsampled_url = (String) json.get("fixed_height_downsampled_url"); + } + + if (json.containsKey("fixed_height_downsampled_width")) { + this.fixed_height_downsampled_width = (String) json.get("fixed_height_downsampled_width"); + } + + if (json.containsKey("fixed_height_downsampled_height")) { + this.fixed_height_downsampled_height = (String) json.get("fixed_height_downsampled_height"); + } + + if (json.containsKey("fixed_width_downsampled_url")) { + this.fixed_width_downsampled_url = (String) json.get("fixed_width_downsampled_url"); + } + + if (json.containsKey("fixed_width_downsampled_width")) { + this.fixed_width_downsampled_width = (String) json.get("fixed_width_downsampled_width"); + } + + if (json.containsKey("fixed_width_downsampled_height")) { + this.fixed_width_downsampled_height = (String) json.get("fixed_width_downsampled_height"); + } + + if (json.containsKey("fixed_height_small_url")) { + this.fixed_height_small_url = (String) json.get("fixed_height_small_url"); + } + + if (json.containsKey("fixed_height_small_still_url")) { + this.fixed_height_small_still_url = (String) json.get("fixed_height_small_still_url"); + } + + if (json.containsKey("fixed_height_small_width")) { + this.fixed_height_small_width = (String) json.get("fixed_height_small_width"); + } + + if (json.containsKey("fixed_height_small_height")) { + this.fixed_height_small_height = (String) json.get("fixed_height_small_height"); + } + + if (json.containsKey("fixed_width_small_url")) { + this.fixed_width_small_url = (String) json.get("fixed_width_small_url"); + } + + if (json.containsKey("fixed_width_small_still_url")) { + this.fixed_width_small_still_url = (String) json.get("fixed_width_small_still_url"); + } + + if (json.containsKey("fixed_width_small_width")) { + this.fixed_width_small_width = (String) json.get("fixed_width_small_width"); + } + + if (json.containsKey("fixed_width_small_height")) { + this.fixed_width_small_height = (String) json.get("fixed_width_small_height"); + } + } + + + // getters to access private fields + + public String getType() { + return type; + } + + public String getId() { + return id; + } + + public String getUrl() { + return this.url; + } + + public String getImage_url() { + return image_url; + } + + public String getImage_mp4_url() { + return image_mp4_url; + } + + public String getImage_frames() { + return image_frames; + } + + public String getImage_width() { + return image_width; + } + + public String getImage_height() { + return image_height; + } + + public String getFixed_height_downsampled_url() { + return fixed_height_downsampled_url; + } + + public String getFixed_height_downsampled_width() { + return fixed_height_downsampled_width; + } + + public String getFixed_height_downsampled_height() { + return fixed_height_downsampled_height; + } + + public String getFixed_width_downsampled_url() { + return fixed_width_downsampled_url; + } + + public String getFixed_width_downsampled_width() { + return fixed_width_downsampled_width; + } + + public String getFixed_width_downsampled_height() { + return fixed_width_downsampled_height; + } + + public String getFixed_height_small_url() { + return fixed_height_small_url; + } + + public String getFixed_height_small_still_url() { + return fixed_height_small_still_url; + } + + public String getFixed_height_small_width() { + return fixed_height_small_width; + } + + public String getFixed_height_small_height() { + return fixed_height_small_height; + } + + public String getFixed_width_small_url() { + return fixed_width_small_url; + } + + public String getFixed_width_small_still_url() { + return fixed_width_small_still_url; + } + + public String getFixed_width_small_width() { + return fixed_width_small_width; + } + + public String getFixed_width_small_height() { + return fixed_width_small_height; + } + + +} \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/model/Profile.java b/src/nyc/c4q/ramonaharrison/model/Profile.java new file mode 100644 index 0000000..3035649 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Profile.java @@ -0,0 +1,106 @@ +package nyc.c4q.ramonaharrison.model; + +import org.json.simple.JSONObject; + +/** + * Created by shawnspeaks on 9/11/16. + */ +public class Profile { + private String firstName; + private String lastName; + private String realName; + private String email; + private String skype; + private String image24; + private String image32; + private String image48; + private String image72; + private String image192; + private String image512; + + + public Profile(JSONObject json) { + if (json.containsKey("firstName")) { + this.firstName = (String) json.get("firstName"); + } + + if (json.containsKey("lastName")) { + this.lastName = (String) json.get("lastName"); + } + + if (json.containsKey("realName")) { + this.realName = (String) json.get("realName"); + } + + if (json.containsKey("email")) { + this.email = (String) json.get("email"); + } + if (json.containsKey("skype")) { + this.skype = (String) json.get("skype"); + } + if (json.containsKey("image24")) { + this.image24 = (String) json.get("image24"); + } + if (json.containsKey("image32")) { + this.image32 = (String) json.get("image32"); + } + if (json.containsKey("image48")) { + this.image48 = (String) json.get("image48"); + } + if (json.containsKey("image72")) { + this.image72 = (String) json.get("image72"); + } + if (json.containsKey("image192")) { + this.image192 = (String) json.get("image192"); + } + if (json.containsKey("image512")) { + this.image512 = (String) json.get("image512"); + } + + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public String getRealName() { + return realName; + } + + public String getEmail() { + return email; + } + + public String getSkype() { + return skype; + } + + public String getImage24() { + return image24; + } + + public String getImage32() { + return image32; + } + + public String getImage48() { + return image48; + } + + public String getImage72() { + return image72; + } + + public String getImage192() { + return image192; + } + + public String getImage512() { + return image512; + } + +} diff --git a/src/nyc/c4q/ramonaharrison/model/User.java b/src/nyc/c4q/ramonaharrison/model/User.java index bc309eb..33a611a 100644 --- a/src/nyc/c4q/ramonaharrison/model/User.java +++ b/src/nyc/c4q/ramonaharrison/model/User.java @@ -14,23 +14,120 @@ public class User { // TODO: implement private fields for each of the following user JSON keys: - // "id" - // "name" - // "deleted" - // "color" - // "profile" - // "is_admin" - // "is_owner" - // "is_primary_owner" - // "is_restricted" - // "is_ultra_restricted" - // "has_2fa" - // "two_factor_type" - // "has_files" + + private String id; + private String name; + private boolean deleted; + private String color; + private boolean isAdmin; + private boolean isOwner; + private String twoFactorType; + private Profile profile; + private boolean isPrimaryOwner; + private boolean isRestricted; + private boolean isUltraRestricted; + private boolean has2Fa; + private boolean hasFiles; + public User(JSONObject json) { - // TODO: parse a user from the incoming json + // oTODO: parse a user from the incoming json + if (json.containsKey("id")){ + this.id = (String) json.get("id"); + } + if (json.containsKey("name")){ + this.name = (String) json.get("name"); + } + if (json.containsKey("deleted")){ + this.deleted = (boolean) json.get("deleted"); + } + if (json.containsKey("color")){ + this.color = (String) json.get("color"); + } + if (json.containsKey("isAdmin")){ + this.isAdmin = (boolean) json.get("isAdmin"); + } + if (json.containsKey("isOwner")){ + this.isOwner = (boolean) json.get("isOwner"); + } + if (json.containsKey("twoFactorType")){ + this.twoFactorType = (String) json.get("twoFactorType"); + } + if (json.containsKey(profile)){ + this.profile = (Profile) json.get(profile); + } + if (json.containsKey("isPrimaryOwner")){ + this.isPrimaryOwner = (boolean) json.get("isPrimaryOwner"); + } + if (json.containsKey("isRestricted")){ + this.isRestricted = (boolean) json.get("isRestricted"); + } + if (json.containsKey("isUltraRestricted")){ + this.isUltraRestricted = (boolean) json.get("isUltraRestricted"); + } + if (json.containsKey("has2Fa")){ + this.has2Fa = (boolean) json.get("has2Fa"); + } + if (json.containsKey("hasFiles")){ + this.hasFiles = (boolean) json.get("hasFiles"); + } + + } + + // oTODO add getters to access private fields + + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public boolean isDeleted() { + return deleted; + } + + public String getColor() { + return color; + } + + public boolean isAdmin() { + return isAdmin; + } + + public boolean isOwner() { + return isOwner; + } + + public String getTwoFactorType() { + return twoFactorType; + } + + public Profile getProfile() { + return profile; + } + + public boolean isPrimaryOwner() { + return isPrimaryOwner; + } + + public boolean isRestricted() { + return isRestricted; + } + + public boolean isUltraRestricted() { + return isUltraRestricted; + } + + public boolean isHas2Fa() { + return has2Fa; + } + + public boolean isHasFiles() { + return hasFiles; } - // TODO add getters to access private fields + } diff --git a/src/nyc/c4q/ramonaharrison/network/Slack.java b/src/nyc/c4q/ramonaharrison/network/Slack.java index 87521bd..c42a89d 100644 --- a/src/nyc/c4q/ramonaharrison/network/Slack.java +++ b/src/nyc/c4q/ramonaharrison/network/Slack.java @@ -1,13 +1,19 @@ package nyc.c4q.ramonaharrison.network; +import nyc.c4q.ramonaharrison.Bot; import nyc.c4q.ramonaharrison.model.Attachment; +import nyc.c4q.ramonaharrison.model.Message; import nyc.c4q.ramonaharrison.network.response.*; import nyc.c4q.ramonaharrison.util.Token; +import org.json.simple.JSONArray; import org.json.simple.JSONObject; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.GregorianCalendar; import java.util.List; /** @@ -27,8 +33,34 @@ public class Slack { private static final String ENDPOINT_LIST_MESSAGES = "channels.history"; private static final String ENDPOINT_POST_MESSAGE = "chat.postMessage"; private static final String ENDPOINT_DELETE_MESSAGE = "chat.delete"; + public static final String BOTS_CHANNEL_ID = "C2BLV9LV6"; + // static strings for Holiday API. + public static final String HOLIDAY_BASE_URL = "https://holidayapi.com"; + public static final String HOLIDAY_ENDPOINT = "/v1/holidays"; + public static final String HOLIDAY_API_KEY = "51638001-7212-488d-bfa5-9d0e6e7a8ef3"; + //Strings For Holiday ++ Giphy! + + + + + public boolean solved; + + + public static String getMonth(){ + GregorianCalendar date = new GregorianCalendar(); + return "&month=" + Integer.toString((date.get(Calendar.MONTH)+1)); + } + + public static String getYear(){ + GregorianCalendar date = new GregorianCalendar(); + return "&year=" + Integer.toString((date.get(Calendar.YEAR))); + } + + public static String getDay(){ + GregorianCalendar date = new GregorianCalendar(); + return "&day=" + Integer.toString((date.get(Calendar.DATE))); + } - public static final String BOTS_CHANNEL_ID = "C2ABKERFT"; /** * Static method to test the Slack API. @@ -41,8 +73,50 @@ public static Response testApi() { JSONObject object = HTTPS.get(testUrl); return new Response(object); + + + } +// + public static void getHolidaysForToday(){ + ListMessagesResponse listMessagesResponse = Slack.listMessages(BOTS_CHANNEL_ID); + + if(listMessagesResponse.isOk()) { + List messages = listMessagesResponse.getMessages(); + + + do{ + if(/*messages.get(0).getText().contains("messybot") && messages.get(0).getText().contains("holiday"*/ true){ + URL holidayURL = HTTPS.stringToURL(HOLIDAY_BASE_URL + HOLIDAY_ENDPOINT + "?country=us&key=" + HOLIDAY_API_KEY + getYear() + getMonth() + getDay() + "&upcoming=true"); + System.out.println(holidayURL); + + JSONObject holidayJson = HTTPS.get(holidayURL); + + if (holidayJson.containsKey("holidays")) { + JSONArray array = (JSONArray) holidayJson.get("holidays"); + + JSONObject nextHoliday = (JSONObject) array.get(0); + + String holidayName = (String) nextHoliday.get("name"); + + String holidayDate = (String) nextHoliday.get("date"); + + sendMessage(holidayDate); + sendMessage(holidayName); + + } + } + }while(false); + } } + + public static void giphyTesting (){ + + } + + + + /** * Static method to list all public channels on the Slack team. * @@ -86,7 +160,6 @@ public static SendMessageResponse sendMessage(String messageText) { return new SendMessageResponse(HTTPS.get(sendMessageUrl)); } - /** * Static method to send a message with one or more attachments to the #bots channel. * @@ -97,6 +170,9 @@ public static SendMessageResponse sendMessage(String messageText) { public static SendMessageResponse sendMessageWithAttachments(String messageText, List attachments) { // TODO (optional): implement this method! See https://api.slack.com/docs/message-attachments + + + throw new RuntimeException("Method not implemented!"); } @@ -111,4 +187,8 @@ public static DeleteMessageResponse deleteMessage(String messageTs) { return new DeleteMessageResponse(HTTPS.get(deleteMessageUrl)); } + + + + } diff --git a/src/nyc/c4q/ramonaharrison/network/response/Response.java b/src/nyc/c4q/ramonaharrison/network/response/Response.java index 20f66ff..4af69db 100644 --- a/src/nyc/c4q/ramonaharrison/network/response/Response.java +++ b/src/nyc/c4q/ramonaharrison/network/response/Response.java @@ -33,4 +33,5 @@ public boolean isOk() { public String getError() { return error; } + } From 2b1ae032efae35050fc94c97ee59e2aa8506dfa1 Mon Sep 17 00:00:00 2001 From: shawn-speaks Date: Sat, 17 Sep 2016 20:56:36 -0400 Subject: [PATCH 2/4] Deadline commit ~Shawn --- src/nyc/c4q/ramonaharrison/Main.java | 7 +- .../c4q/ramonaharrison/model/GiphyClass.java | 6 +- src/nyc/c4q/ramonaharrison/network/Slack.java | 78 ++++++++++++++++--- 3 files changed, 74 insertions(+), 17 deletions(-) diff --git a/src/nyc/c4q/ramonaharrison/Main.java b/src/nyc/c4q/ramonaharrison/Main.java index 979de3b..d0c17e5 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -12,10 +12,11 @@ public class Main { public static void main(String[] args) { Bot aBot = new Bot(); - aBot.testApi(); - aBot.readMessages("C2BLV9LV6"); + aBot.testApi(); + aBot.readMessages("C2BLV9LV6"); Slack.getHolidaysForToday(); - +// aBot.sendMessageToBotsChannel(Slack.giphySearch()); +// Slack.guessingGame(); // Calendar cal = Calendar.getInstance(); // int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); // diff --git a/src/nyc/c4q/ramonaharrison/model/GiphyClass.java b/src/nyc/c4q/ramonaharrison/model/GiphyClass.java index 5bf3e40..7f436cb 100644 --- a/src/nyc/c4q/ramonaharrison/model/GiphyClass.java +++ b/src/nyc/c4q/ramonaharrison/model/GiphyClass.java @@ -1,5 +1,6 @@ -import org.json.simple.JSONObject; +package nyc.c4q.ramonaharrison.model; +import org.json.simple.JSONObject; /** { @@ -34,6 +35,7 @@ } } */ + public class GiphyClass { private String type; @@ -59,7 +61,7 @@ public class GiphyClass { private String fixed_width_small_width; private String fixed_width_small_height; - public GiphyData(JSONObject json) { + public GiphyClass(JSONObject json) { // parse data from the incoming json if (json.containsKey("type")){ diff --git a/src/nyc/c4q/ramonaharrison/network/Slack.java b/src/nyc/c4q/ramonaharrison/network/Slack.java index c42a89d..f23d4e5 100644 --- a/src/nyc/c4q/ramonaharrison/network/Slack.java +++ b/src/nyc/c4q/ramonaharrison/network/Slack.java @@ -16,6 +16,8 @@ import java.util.GregorianCalendar; import java.util.List; +import static com.sun.org.apache.xml.internal.serializer.utils.Utils.messages; + /** * Created by Ramona Harrison * on 8/26/16 @@ -41,9 +43,18 @@ public class Slack { //Strings For Holiday ++ Giphy! + public static String holidayName; + public static String holidayDate; + + + private static final String GIPHY_BASE_URL = "https://api.giphy.com/"; + private static final String GIPHY_ENDPOINT_TEST = "v1/gifs/"; + private static final String API = "dc6zaTOxFJmzC"; + private static final String UNFURL_MEDIA = "true"; - public boolean solved; + + public boolean isSolved; public static String getMonth(){ @@ -74,8 +85,37 @@ public static Response testApi() { return new Response(object); + } + + public static String giphySearch() { + + ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ADPS5MK"); + List messages = listMessagesResponse.getMessages(); + +// +// String query = message.getText(); +// String substr = "+", regex = "\\s"; +// query = query.replaceAll(regex, substr); + String temp = holidayName; + holidayName=holidayName.replaceAll("\\s",""); + + + URL giphyURL = HTTPS.stringToURL(GIPHY_BASE_URL + GIPHY_ENDPOINT_TEST + "random?" + "api_key=" + API + "&tag=" + holidayName); + JSONObject giphyJson = HTTPS.get(giphyURL); + holidayName = temp; + if (giphyJson.containsKey("data")) { + + JSONObject myObj = (JSONObject) giphyJson.get("data"); + String giphyString = myObj.get("fixed_height_downsampled_url").toString(); + System.out.println(giphyString); + return giphyString; + }else + return " "; } + + + // public static void getHolidaysForToday(){ ListMessagesResponse listMessagesResponse = Slack.listMessages(BOTS_CHANNEL_ID); @@ -83,10 +123,9 @@ public static void getHolidaysForToday(){ if(listMessagesResponse.isOk()) { List messages = listMessagesResponse.getMessages(); - do{ - if(/*messages.get(0).getText().contains("messybot") && messages.get(0).getText().contains("holiday"*/ true){ - URL holidayURL = HTTPS.stringToURL(HOLIDAY_BASE_URL + HOLIDAY_ENDPOINT + "?country=us&key=" + HOLIDAY_API_KEY + getYear() + getMonth() + getDay() + "&upcoming=true"); + if(messages.get(0).getText().contains("messybot") && messages.get(0).getText().contains("holiday")){ + URL holidayURL = HTTPS.stringToURL(HOLIDAY_BASE_URL + HOLIDAY_ENDPOINT + "?country=us&key=" + HOLIDAY_API_KEY + getMonth() + getYear() + getDay() + "&upcoming=true"); System.out.println(holidayURL); JSONObject holidayJson = HTTPS.get(holidayURL); @@ -96,22 +135,37 @@ public static void getHolidaysForToday(){ JSONObject nextHoliday = (JSONObject) array.get(0); - String holidayName = (String) nextHoliday.get("name"); - - String holidayDate = (String) nextHoliday.get("date"); - - sendMessage(holidayDate); - sendMessage(holidayName); + holidayName = (String) nextHoliday.get("name"); + holidayDate = (String) nextHoliday.get("date"); } } }while(false); } + System.out.println(holidayName); + System.out.println(holidayName); } - public static void giphyTesting (){ - + public static void guessingGame () { + boolean isSolved = false; + ListMessagesResponse listMessagesResponse = Slack.listMessages(BOTS_CHANNEL_ID); + List messages = listMessagesResponse.getMessages(); + + if (listMessagesResponse.isOk()) { + while (!isSolved){ + + if (messages.get(0).getText().toString().equals(holidayName)) { + isSolved = true; + sendMessage("Correct! The next Holiday is " + holidayName + ". " + holidayName + " is on " + holidayDate); + } else if (messages.get(0).getText().equalsIgnoreCase("give up")){ + isSolved = true; + sendMessage("The next Holiday is + " + holidayName + " . It lands on " + holidayDate + "."); + }else if (messages.get(0).getText().equalsIgnoreCase("hint")) { + giphySearch(); + } + } + } } From 18f45d3cef3c7ce10b23e6b8bfc91416106bdc66 Mon Sep 17 00:00:00 2001 From: shawn-speaks Date: Sat, 17 Sep 2016 20:57:09 -0400 Subject: [PATCH 3/4] Deadline commit ~Shawn --- src/nyc/c4q/ramonaharrison/network/Slack.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nyc/c4q/ramonaharrison/network/Slack.java b/src/nyc/c4q/ramonaharrison/network/Slack.java index f23d4e5..7389de7 100644 --- a/src/nyc/c4q/ramonaharrison/network/Slack.java +++ b/src/nyc/c4q/ramonaharrison/network/Slack.java @@ -125,7 +125,7 @@ public static void getHolidaysForToday(){ do{ if(messages.get(0).getText().contains("messybot") && messages.get(0).getText().contains("holiday")){ - URL holidayURL = HTTPS.stringToURL(HOLIDAY_BASE_URL + HOLIDAY_ENDPOINT + "?country=us&key=" + HOLIDAY_API_KEY + getMonth() + getYear() + getDay() + "&upcoming=true"); + URL holidayURL = HTTPS.stringToURL(HOLIDAY_BASE_URL + HOLIDAY_ENDPOINT + "?country=us&key=" + HOLIDAY_API_KEY + getYear() + getMonth() + getDay() + "&upcoming=true"); System.out.println(holidayURL); JSONObject holidayJson = HTTPS.get(holidayURL); From f7e762cc6654f8fbe1de2d2b2ee2c64d6887ab66 Mon Sep 17 00:00:00 2001 From: shawn-speaks Date: Sun, 18 Sep 2016 14:19:36 -0400 Subject: [PATCH 4/4] Shawn's final commit --- src/nyc/c4q/ramonaharrison/Bot.java | 2 +- src/nyc/c4q/ramonaharrison/Main.java | 32 +++++++------------ src/nyc/c4q/ramonaharrison/network/Slack.java | 32 +++++++++++-------- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/nyc/c4q/ramonaharrison/Bot.java b/src/nyc/c4q/ramonaharrison/Bot.java index 3b464e9..0819bb9 100644 --- a/src/nyc/c4q/ramonaharrison/Bot.java +++ b/src/nyc/c4q/ramonaharrison/Bot.java @@ -95,7 +95,7 @@ public void readMessages(String channelId){ * @param text message text. */ public void sendMessageToBotsChannel(String text) { - SendMessageResponse sendMessageResponse = Slack.sendMessage(text); + SendMessageResponse sendMessageResponse = Slack.sendMessage(text); if (sendMessageResponse.isOk()) { System.out.println("Message sent successfully!"); diff --git a/src/nyc/c4q/ramonaharrison/Main.java b/src/nyc/c4q/ramonaharrison/Main.java index d0c17e5..c6c66fe 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -10,26 +10,18 @@ public class Main { public static void main(String[] args) { - Bot aBot = new Bot(); - - aBot.testApi(); - aBot.readMessages("C2BLV9LV6"); - Slack.getHolidaysForToday(); -// aBot.sendMessageToBotsChannel(Slack.giphySearch()); -// Slack.guessingGame(); -// Calendar cal = Calendar.getInstance(); -// int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); -// -// String dayOfMonthStr = String.valueOf(dayOfMonth); -// System.out.println(dayOfMonth); -// -// -// Calendar cal = Calendar.getInstance(); -// int currentYear = cal.get(Calendar.YEAR); -// -// String year = String.valueOf(currentYear); -// System.out.println(year); - + while(true) { + Bot aBot = new Bot(); +// aBot.listChannels(); +// aBot.testApi(); +// aBot.readMessages("C2BLV9LV6"); + Slack.getHolidaysForToday(); + if (Slack.giphySearch() != null) { + aBot.sendMessageToBotsChannel("You have entered the guessing game command! Type 'hint' for another gif related to the next holiday, or 'give up' for the answer. Good luck!"); + aBot.sendMessageToBotsChannel(Slack.giphySearch()); + } + Slack.guessingGame(); + } diff --git a/src/nyc/c4q/ramonaharrison/network/Slack.java b/src/nyc/c4q/ramonaharrison/network/Slack.java index 7389de7..10fb7bc 100644 --- a/src/nyc/c4q/ramonaharrison/network/Slack.java +++ b/src/nyc/c4q/ramonaharrison/network/Slack.java @@ -35,7 +35,7 @@ public class Slack { private static final String ENDPOINT_LIST_MESSAGES = "channels.history"; private static final String ENDPOINT_POST_MESSAGE = "chat.postMessage"; private static final String ENDPOINT_DELETE_MESSAGE = "chat.delete"; - public static final String BOTS_CHANNEL_ID = "C2BLV9LV6"; + public static final String BOTS_CHANNEL_ID = "C2ADPS5MK"; // static strings for Holiday API. public static final String HOLIDAY_BASE_URL = "https://holidayapi.com"; public static final String HOLIDAY_ENDPOINT = "/v1/holidays"; @@ -50,7 +50,7 @@ public class Slack { private static final String GIPHY_BASE_URL = "https://api.giphy.com/"; private static final String GIPHY_ENDPOINT_TEST = "v1/gifs/"; private static final String API = "dc6zaTOxFJmzC"; - private static final String UNFURL_MEDIA = "true"; +// private static final String UNFURL_MEDIA = "true"; @@ -97,8 +97,10 @@ public static String giphySearch() { // String substr = "+", regex = "\\s"; // query = query.replaceAll(regex, substr); String temp = holidayName; - holidayName=holidayName.replaceAll("\\s",""); - + if (holidayName!=null) { + holidayName = holidayName.replaceAll("\\s", ""); + }else + return null; URL giphyURL = HTTPS.stringToURL(GIPHY_BASE_URL + GIPHY_ENDPOINT_TEST + "random?" + "api_key=" + API + "&tag=" + holidayName); @@ -124,9 +126,8 @@ public static void getHolidaysForToday(){ List messages = listMessagesResponse.getMessages(); do{ - if(messages.get(0).getText().contains("messybot") && messages.get(0).getText().contains("holiday")){ + if(messages.get(0).getText().equalsIgnoreCase("!holiday")){ URL holidayURL = HTTPS.stringToURL(HOLIDAY_BASE_URL + HOLIDAY_ENDPOINT + "?country=us&key=" + HOLIDAY_API_KEY + getYear() + getMonth() + getDay() + "&upcoming=true"); - System.out.println(holidayURL); JSONObject holidayJson = HTTPS.get(holidayURL); @@ -140,30 +141,35 @@ public static void getHolidaysForToday(){ holidayDate = (String) nextHoliday.get("date"); } } + }while(false); } - System.out.println(holidayName); - System.out.println(holidayName); + } public static void guessingGame () { - boolean isSolved = false; + boolean isSolved; + if(holidayName==null){ + isSolved=true; + }else + isSolved=false; + ListMessagesResponse listMessagesResponse = Slack.listMessages(BOTS_CHANNEL_ID); List messages = listMessagesResponse.getMessages(); - if (listMessagesResponse.isOk()) { while (!isSolved){ - - if (messages.get(0).getText().toString().equals(holidayName)) { + if (messages.get(0).getText().toString().equalsIgnoreCase(holidayName)) { isSolved = true; sendMessage("Correct! The next Holiday is " + holidayName + ". " + holidayName + " is on " + holidayDate); } else if (messages.get(0).getText().equalsIgnoreCase("give up")){ isSolved = true; sendMessage("The next Holiday is + " + holidayName + " . It lands on " + holidayDate + "."); }else if (messages.get(0).getText().equalsIgnoreCase("hint")) { - giphySearch(); + sendMessage(giphySearch()); } + ListMessagesResponse listMessages1Response = Slack.listMessages(BOTS_CHANNEL_ID); + messages = listMessages1Response.getMessages(); } } }