From 51743d68dfd98f499118dfd34b722baaf63cb189 Mon Sep 17 00:00:00 2001 From: Cat Wong Date: Sat, 17 Sep 2016 17:26:54 -0400 Subject: [PATCH 1/8] first commit --- src/nyc/c4q/ramonaharrison/Bot.java | 268 ++++++++++++++---- src/nyc/c4q/ramonaharrison/Main.java | 34 ++- .../c4q/ramonaharrison/model/Attachment.java | 150 +++++++++- src/nyc/c4q/ramonaharrison/model/Field.java | 25 ++ .../c4q/ramonaharrison/model/GiphyData.java | 248 ++++++++++++++++ .../c4q/ramonaharrison/model/GiphyMeta.java | 43 +++ src/nyc/c4q/ramonaharrison/model/Message.java | 1 + src/nyc/c4q/ramonaharrison/model/Profile.java | 70 +++++ src/nyc/c4q/ramonaharrison/model/User.java | 130 ++++++++- src/nyc/c4q/ramonaharrison/network/Giphy.java | 56 ++++ src/nyc/c4q/ramonaharrison/network/Memes.java | 24 ++ src/nyc/c4q/ramonaharrison/network/Slack.java | 58 +++- .../network/UrbanDictionary.java | 41 +++ .../network/response/GiphyResponse.java | 48 ++++ .../response/ListMessagesResponse.java | 12 +- .../network/response/Response.java | 4 + .../response/SearchMessagesResponse.java | 42 +++ .../network/response/UserInfoResponse.java | 38 +++ 18 files changed, 1226 insertions(+), 66 deletions(-) create mode 100644 src/nyc/c4q/ramonaharrison/model/Field.java create mode 100644 src/nyc/c4q/ramonaharrison/model/GiphyData.java create mode 100644 src/nyc/c4q/ramonaharrison/model/GiphyMeta.java create mode 100644 src/nyc/c4q/ramonaharrison/model/Profile.java create mode 100644 src/nyc/c4q/ramonaharrison/network/Giphy.java create mode 100644 src/nyc/c4q/ramonaharrison/network/Memes.java create mode 100644 src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java create mode 100644 src/nyc/c4q/ramonaharrison/network/response/GiphyResponse.java create mode 100644 src/nyc/c4q/ramonaharrison/network/response/SearchMessagesResponse.java create mode 100644 src/nyc/c4q/ramonaharrison/network/response/UserInfoResponse.java diff --git a/src/nyc/c4q/ramonaharrison/Bot.java b/src/nyc/c4q/ramonaharrison/Bot.java index 6a52dd4..1761a89 100644 --- a/src/nyc/c4q/ramonaharrison/Bot.java +++ b/src/nyc/c4q/ramonaharrison/Bot.java @@ -1,50 +1,38 @@ package nyc.c4q.ramonaharrison; import nyc.c4q.ramonaharrison.model.Channel; +import nyc.c4q.ramonaharrison.model.GiphyData; import nyc.c4q.ramonaharrison.model.Message; + +import nyc.c4q.ramonaharrison.model.User; import nyc.c4q.ramonaharrison.network.*; import nyc.c4q.ramonaharrison.network.response.*; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import java.net.MalformedURLException; +import java.net.URL; import java.util.List; - -/** - * Created by Ramona Harrison - * on 8/26/16 - * - */ +import java.util.Random; +import java.util.regex.Pattern; public class Bot { - // TODO: implement your bot logic! + + boolean checkGreeting = false; public Bot() { } - /** - * Sample method: tests the Slack API. Prints a message indicating success or failure. - */ - public void testApi() { - Response apiTest = Slack.testApi(); - System.out.println("API OK: " +apiTest.isOk() + "\n"); + public void addReaction() { + Response reaction = Slack.addReaction(); + System.out.println(reaction); } - /** - * Sample method: prints all public AccessCode3-3 channel names and ids. Prints an error message on failure. - */ - public void listChannels() { - ListChannelsResponse listChannelsResponse = Slack.listChannels(); - - if (listChannelsResponse.isOk()) { - List channels = listChannelsResponse.getChannels(); - - System.out.println("\nChannels: "); - for (Channel channel : channels) { - System.out.println("name: " + channel.getName() + ", id:" + channel.getId()); - } - } else { - System.err.print("Error listing channels: " + listChannelsResponse.getError()); - } - } +// public void userInfo() { +// UserInfoResponse user = Slack.userInfo("U23CZAXQS"); +// System.out.println(user); +// } /** * Sample method: prints up to the last 100 messages from a given channel. Prints an error message on failure. @@ -57,45 +45,221 @@ public void listMessages(String channelId) { if (listMessagesResponse.isOk()) { List messages = listMessagesResponse.getMessages(); - System.out.println("\nMessages: "); for (Message message : messages) { System.out.println(); System.out.println("Timestamp: " + message.getTs()); System.out.println("Message: " + message.getText()); + System.out.println("User: " + message.getUser()); } } else { System.err.print("Error listing messages: " + listMessagesResponse.getError()); } } - /** - * Sample method: sends a plain text message to the #bots channel. Prints a message indicating success or failure. - * - * @param text message text. - */ - public void sendMessageToBotsChannel(String text) { - SendMessageResponse sendMessageResponse = Slack.sendMessage(text); + public void respondGreeting(String channelId) { + ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); + + if (listMessagesResponse.isOk()) { + List messages = listMessagesResponse.getMessages(); - if (sendMessageResponse.isOk()) { - System.out.println("Message sent successfully!"); + Slack myBot = new Slack(); + + for (Message message : messages) { + + String whatDidYouSay = message.getText(); + + if (whatDidYouSay.contains("@U2ADRJVK9") || whatDidYouSay.contains("messybot")) { + myBot.sendMessage("Hello " + message.getUser().toString()); + myBot.sendMessage("I am " + Slack.getUSERNAME()); + myBot.sendMessage(":poop::poop::poop:"); + myBot.sendMessage("Give me a word and I'll find it for you."); + } + } } else { - System.err.print("Error sending message: " + sendMessageResponse.getError()); + System.err.print("Error listing messages: " + listMessagesResponse.getError()); } } - /** - * Sample method: deletes a message from the #bots channel. Prints a message indicating success or failure. - * - * @param messageTs unique timestamp of the message to be deleted. - */ - public void deleteMessageInBotsChannel(String messageTs) { - DeleteMessageResponse deleteMessageResponse = Slack.deleteMessage(messageTs); + public void respondAll(String channelId) { + ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); + + if (listMessagesResponse.isOk()) { + List messages = listMessagesResponse.getMessages(); + + Slack myBot = new Slack(); + UrbanDictionary urbanDictionary = new UrbanDictionary(); + Memes memes = new Memes(); + Giphy giphy = new Giphy(); - if (deleteMessageResponse.isOk()) { - System.out.println("Message deleted successfully!"); + for (Message message : messages) { + + String word = message.getText(); + +// while (word != null) { + if (word == word) { + URL slangSearch = urbanDictionary.wordSearch(); + slangSearch.toString(); + + URL memeSearch = memes.memeSearch(); + memeSearch.toString(); + + String giphySearch = giphy.giphySearch(); + + myBot.sendMessage(slangSearch.toString()); + myBot.sendMessage(memeSearch.toString()); + myBot.sendMessage(giphySearch); +// } + } + } } else { - System.err.print("Error sending message: " + deleteMessageResponse.getError()); + System.err.print("Error listing messages: " + listMessagesResponse.getError()); + } + } + + + public void respondDictionary(String channelId) { + ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); + + if (listMessagesResponse.isOk()) { + List messages = listMessagesResponse.getMessages(); + + Slack myBot = new Slack(); + UrbanDictionary urbanDictionary = new UrbanDictionary(); + + for (Message message : messages) { + + String word = message.getText(); + String user = message.getUser(); + + if (word == word) { + URL slangSearch = urbanDictionary.wordSearch(); + slangSearch.toString(); + + myBot.sendMessage(slangSearch.toString()); + } + } + } else { + System.err.print("Error listing messages: " + listMessagesResponse.getError()); + } + } + + public void respondMeme(String channelId) { + ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); + + if (listMessagesResponse.isOk()) { + List messages = listMessagesResponse.getMessages(); + + Slack myBot = new Slack(); + Memes memes = new Memes(); + + for (Message message : messages) { + + String aMeme = message.getText(); + if (aMeme == aMeme) { + URL memeSearch = memes.memeSearch(); + memeSearch.toString(); + myBot.sendMessage(memeSearch.toString()); + } + } + } else { + System.err.print("Error listing messages: " + listMessagesResponse.getError()); + } + } + + public void respondGiphy(String channelId) { + ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); + + if (listMessagesResponse.isOk()) { + List messages = listMessagesResponse.getMessages(); + + Slack myBot = new Slack(); + Giphy giphy = new Giphy(); + + for (Message message : messages) { + String aGiphy = message.getText(); + + if (aGiphy == aGiphy) { + String giphySearch = giphy.giphySearch(); + myBot.sendMessage(giphySearch.toString()); + } + } + + } else { + System.err.print("Error listing messages: " + listMessagesResponse.getError()); + } + } + /** + * Sample method: tests the Slack API. Prints a message indicating success or failure. + */ + public void testApi () { + Response apiTest = Slack.testApi(); + System.out.println("API OK: " + apiTest.isOk() + "\n"); + } + + /** + * Sample method: prints all public AccessCode3-3 channel names and ids. Prints an error message on failure. + */ + public void listChannels () { + ListChannelsResponse listChannelsResponse = Slack.listChannels(); + + if (listChannelsResponse.isOk()) { + List channels = listChannelsResponse.getChannels(); + + System.out.println("\nChannels: "); + for (Channel channel : channels) { + System.out.println("name: " + channel.getName() + ", id:" + channel.getId()); + } + } else { + System.err.print("Error listing channels: " + listChannelsResponse.getError()); + } + } + + /** + * Sample method: sends a plain text message to the #bots channel. Prints a message indicating success or failure. + * + * @param text message text. + */ + public void sendMessageToBotsChannel (String text){ + SendMessageResponse sendMessageResponse = Slack.sendMessage(text); + + if (sendMessageResponse.isOk()) { + System.out.println("Message sent successfully!"); + } else { + System.err.print("Error sending message: " + sendMessageResponse.getError()); + } + } + + /** + * Sample method: deletes a message from the #bots channel. Prints a message indicating success or failure. + * + * @param messageTs unique timestamp of the message to be deleted. + */ + public void deleteMessageInBotsChannel (String messageTs){ + DeleteMessageResponse deleteMessageResponse = Slack.deleteMessage(messageTs); + + if (deleteMessageResponse.isOk()) { + System.out.println("Message deleted successfully!"); + } else { + System.err.print("Error sending message: " + deleteMessageResponse.getError()); + } + } + + public void searchMessages (String channelId, String query){ + SearchMessagesResponse querySearch = Slack.searchMessages(channelId, query); + + if (querySearch.isOk()) { + List messages = querySearch.getMessages(); + System.out.println("You searched for: " + querySearch); + + for (Message queryWord : messages) { + System.out.println(); + System.out.println("Timestamp: " + queryWord.getTs()); + System.out.println("Message: " + queryWord.getText()); + System.out.println("User: " + queryWord.getUser()); + } + } else { + System.err.print("Error listing messages: " + querySearch.getError()); + } } } -} diff --git a/src/nyc/c4q/ramonaharrison/Main.java b/src/nyc/c4q/ramonaharrison/Main.java index 933e92b..ef14469 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -1,24 +1,44 @@ package nyc.c4q.ramonaharrison; +import nyc.c4q.ramonaharrison.network.Giphy; +import nyc.c4q.ramonaharrison.network.UrbanDictionary; import nyc.c4q.ramonaharrison.network.Slack; + public class Main { - public static void main(String[] args) { + public static void main(String[] args){ Bot myBot = new Bot(); - - myBot.testApi(); + // myBot.testApi(); +// myBot.deleteMessageInBotsChannel("1474042536.000733"); myBot.listChannels(); + myBot.listMessages(Slack.BOTS_CHANNEL_ID); + + myBot.addReaction(); + + myBot.respondGreeting("C2ADPS5MK"); + + myBot.addReaction(); myBot.listMessages(Slack.BOTS_CHANNEL_ID); - // Post "Hello, world!" to the #bots channel - //myBot.sendMessage("Hello, world!"); +// myBot.respondAll("C2ADPS5MK"); +// +// myBot.addReaction(); +// +// myBot.listMessages(Slack.BOTS_CHANNEL_ID); + +// myBot.respondDictionary("C2ADPS5MK"); + myBot.respondMeme("C2ADPS5MK"); +// myBot.respondGiphy("C2ADPS5MK"); + +// myBot.sendMessageToBotsChannel(":poop: messybot will be under maintenance."); - // Post a pineapple photo to the #bots channel - //myBot.sendMessage("http://weknowyourdreams.com/images/pineapple/pineapple-07.jpg"); + // non working method-calls. NEED TO REVISIT OR DELETE +// myBot.respondMeme("C2ADPS5MK"); +// myBot.searchMessages(Slack.BOTS_CHANNEL_ID, "messybot"); } } \ 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..8cda3f3 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 @@ -30,10 +34,152 @@ public class Attachment { // "footer_icon" // "ts" + private String fallback; + private String color; + private String pretext; + private String author_name; + private String author_link; + private String author_icon; + private String title; + private String title_link; + private String text; + private List fields; + private String imageUrl; + private String thumbUrl; + private String footer; + private String footer_icon; + private String ts; + public Attachment(JSONObject json) { - // TODO: parse an attachment from the incoming json + + + // parse a user from the incoming json + + if (json.containsKey("fallback")) { + this.fallback = (String) json.get("fallback"); + } + + if (json.containsKey("color")) { + this.color = (String) json.get("color"); + } + + if (json.containsKey("pretext")) { + this.pretext = (String) json.get("pretext"); + } + + if (json.containsKey("author_name")) { + this.author_name = (String) json.get("author_name"); + } + + if (json.containsKey("author_link")) { + this.author_link = (String) json.get("author_name"); + } + + if (json.containsKey("author_icon")) { + this.author_icon = (String) json.get("author_icon"); + } + + if (json.containsKey("title")) { + this.title = (String) json.get("title"); + } + + if (json.containsKey("title_link")) { + this.title_link = (String) json.get("title_link"); + } + + if (json.containsKey("text")) { + this.text = (String) json.get("title"); + } + + 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("footer_icon")) { + this.footer_icon = (String) json.get("footer_icon"); + } + + 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("ts")) { + this.ts = (String) json.get("ts"); + } + } + // getters to access private fields + + public String getFallback() { + return fallback; + } + + public String getColor() { + return color; + } + + public String getPretext() { + return pretext; + } + + public String getAuthor_name() { + return author_name; + } + + public String getAuthor_link() { + return author_link; + } + + public String getAuthor_icon() { + return author_icon; + } + + public String getTitle() { + return title; + } + + public String getTitle_link() { + return title_link; + } + + 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; } - // TODO add getters to access private fields + public String getFooterIcon() { + return footer_icon; + } + + public String getTs() { + return ts; + } } diff --git a/src/nyc/c4q/ramonaharrison/model/Field.java b/src/nyc/c4q/ramonaharrison/model/Field.java new file mode 100644 index 0000000..6cae253 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Field.java @@ -0,0 +1,25 @@ +package nyc.c4q.ramonaharrison.model; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +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("short"); + } + } +} diff --git a/src/nyc/c4q/ramonaharrison/model/GiphyData.java b/src/nyc/c4q/ramonaharrison/model/GiphyData.java new file mode 100644 index 0000000..8c4a9ec --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/GiphyData.java @@ -0,0 +1,248 @@ +package nyc.c4q.ramonaharrison.model; + +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 GiphyData { + + 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; + } + + +} diff --git a/src/nyc/c4q/ramonaharrison/model/GiphyMeta.java b/src/nyc/c4q/ramonaharrison/model/GiphyMeta.java new file mode 100644 index 0000000..f03cf9d --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/GiphyMeta.java @@ -0,0 +1,43 @@ +package nyc.c4q.ramonaharrison.model; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +/** + "meta": { + "status": 200, + "msg": "OK" + } + */ + +public class GiphyMeta { + + private int status; + private String msg; + + // parse data from the incoming json + + public GiphyMeta(JSONObject json) { + + if(json.containsKey("status")) { + this.status = (Integer) json.get("status"); + } + + if(json.containsKey("msg")) { + this.msg = (String) json.get("msg"); + } + } + + // getters to access private fields + + public int getStatus() { + return status; + } + + public String getMsg() { + return msg; + } +} diff --git a/src/nyc/c4q/ramonaharrison/model/Message.java b/src/nyc/c4q/ramonaharrison/model/Message.java index 8ea24bd..f85ad13 100644 --- a/src/nyc/c4q/ramonaharrison/model/Message.java +++ b/src/nyc/c4q/ramonaharrison/model/Message.java @@ -73,4 +73,5 @@ public String getUser() { public List getAttachments() { return attachments; } + } diff --git a/src/nyc/c4q/ramonaharrison/model/Profile.java b/src/nyc/c4q/ramonaharrison/model/Profile.java new file mode 100644 index 0000000..e55a943 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Profile.java @@ -0,0 +1,70 @@ +package nyc.c4q.ramonaharrison.model; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +public class Profile { + + private String firstName; + private String lastName; + private String realName; + private String email; + private String skype; + private String phone; + 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("phone")) + this.phone = (String) json.get("phone"); + + if (json.containsKey("image24")) { + this.image24 = (String) json.get("image_24"); + } + + 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"); + } + + } +} diff --git a/src/nyc/c4q/ramonaharrison/model/User.java b/src/nyc/c4q/ramonaharrison/model/User.java index bc309eb..748430d 100644 --- a/src/nyc/c4q/ramonaharrison/model/User.java +++ b/src/nyc/c4q/ramonaharrison/model/User.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 @@ -28,9 +32,131 @@ public class User { // "two_factor_type" // "has_files" + private String id; + private String name; + private String deleted; + private String color; + private List userProfile; + private boolean isAdmin; + private boolean isOwner; + private boolean isPrimaryOwner; + private boolean isRestricted; + private boolean isUltraRestricted; + private boolean has2Fa; + private String twoFactorType; + private boolean hasFiles; + public User(JSONObject json) { - // TODO: parse a user from the incoming json + // 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 = (String) json.get("deleted"); + + if (json.containsKey("color")) + this.color = (String) json.get("color"); + + if (json.containsKey("userProfile")) { + JSONArray jsonUserProfile = (JSONArray) json.get("userProfile"); + this.userProfile = new ArrayList(); + for (int i = 0; i < jsonUserProfile.size(); i++) { + Profile profile = new Profile((JSONObject) jsonUserProfile.get(i)); + this.userProfile.add(profile); + } + } + + if (json.containsKey("isAdmin")) { + this.isAdmin = (boolean) json.get("isAdmin"); + } + + if (json.containsKey("isOwner")) { + this.isOwner = (boolean) json.get("isOwner"); + } + + if (json.containsKey("isPrimaryOwner")) { + this.isPrimaryOwner = (boolean) json.get("isPrimaryOwner"); + } + + if (json.containsKey("isRestricted")) { + this.isRestricted = (boolean) json.get("is_Restricted"); + } + + if (json.containsKey("isUltraRestricted")) { + this.isUltraRestricted = (boolean) json.get("isUltraRestricted"); + } + + if (json.containsKey("has2Fa")) { + this.has2Fa = (boolean) json.get("has2Fa"); + } + + if (json.containsKey("twoFactorType")) { + this.twoFactorType = (String) json.get("twoFactorType"); + } + + if (json.containsKey("hasFiles")) { + this.hasFiles = (boolean) json.get("hasFiles"); + } + } + + // getters to access private fields + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public String getDeleted() { + return deleted; } - // TODO add getters to access private fields + public String getColor() { + return color; + } + + public List getUserProfile() { + return userProfile; + } + + public boolean isAdmin() { + return isAdmin; + } + + public boolean isOwner() { + return isOwner; + } + + public boolean isPrimaryOwner() { + return isPrimaryOwner; + } + + public boolean isRestricted() { + return isRestricted; + } + + public boolean isUltraRestricted() { + return isUltraRestricted; + } + + public boolean isHas2Fa() { + return has2Fa; + } + + public String isTwoFactorType() { + return twoFactorType; + } + + public boolean isHasFiles() { + return hasFiles; + } + + public void get(String user) { + } } diff --git a/src/nyc/c4q/ramonaharrison/network/Giphy.java b/src/nyc/c4q/ramonaharrison/network/Giphy.java new file mode 100644 index 0000000..343561a --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/network/Giphy.java @@ -0,0 +1,56 @@ +package nyc.c4q.ramonaharrison.network; + +import java.net.URL; +import java.util.List; +import java.util.Random; +import java.util.Scanner; +import nyc.c4q.ramonaharrison.model.GiphyData; +import nyc.c4q.ramonaharrison.model.GiphyMeta; +import nyc.c4q.ramonaharrison.model.Message; +import nyc.c4q.ramonaharrison.network.response.GiphyResponse; +import nyc.c4q.ramonaharrison.network.response.ListMessagesResponse; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + + +public class Giphy { + + // RANDOM GIPHY PULLED FROM QUERY TAG + // http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=american+psycho + + private static final String BASE_URL = "https://api.giphy.com/"; + private static final String ENDPOINT_TEST = "v1/gifs/"; + private static final String API = "dc6zaTOxFJmzC"; + private static final String UNFURL_MEDIA = "true"; + + public static String giphySearch() { + + ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ADPS5MK"); + List messages = listMessagesResponse.getMessages(); + + for (Message message : messages) { + String query = message.getText(); + String substr = "+", regex = "\\s"; + query = query.replaceAll(regex, substr); + + URL giphyURL = HTTPS.stringToURL(BASE_URL + ENDPOINT_TEST + "random?" + "api_key=" + API + "&tag=" + query); + System.out.println(giphyURL); + + JSONObject giphyJson = HTTPS.get(giphyURL); + + 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 null; + } + return " "; + } +} + +// Scanner input = new Scanner(System.in); +// System.out.println("enter a word: "); +// String query = input.next(); \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/network/Memes.java b/src/nyc/c4q/ramonaharrison/network/Memes.java new file mode 100644 index 0000000..1dbd772 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/network/Memes.java @@ -0,0 +1,24 @@ +package nyc.c4q.ramonaharrison.network; + +import java.net.URL; +import java.util.Scanner; +import java.util.Random; + + +/** + http://www.memes.com/img/13 + http://www.memes.com/img/1190510 + */ +public class Memes { + private static final String BASE_URL = "http://www.memes.com/"; + private static final String ENDPOINT_TEST = "img/"; + private static final String UNFURL_LINK = "true"; + private static final String UNFURL_MEDIA = "true"; + + public static URL memeSearch() { + Random number = new Random(); + int query = number.nextInt(1190510) + 13; + + return HTTPS.stringToURL(BASE_URL + ENDPOINT_TEST + query); + } +} diff --git a/src/nyc/c4q/ramonaharrison/network/Slack.java b/src/nyc/c4q/ramonaharrison/network/Slack.java index 87521bd..787bec0 100644 --- a/src/nyc/c4q/ramonaharrison/network/Slack.java +++ b/src/nyc/c4q/ramonaharrison/network/Slack.java @@ -1,6 +1,7 @@ package nyc.c4q.ramonaharrison.network; 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.JSONObject; @@ -21,14 +22,26 @@ public class Slack { private static final String API_KEY = Token.findApiToken(); + private static final String CAT_API_KEY = "xoxp-71191384642-71441371842-79184766036-f9ac6a4d4f"; private static final String BASE_URL = "https://slack.com/api/"; private static final String ENDPOINT_TEST = "api.test"; private static final String ENDPOINT_LIST_CHANNELS = "channels.list"; private static final String ENDPOINT_LIST_MESSAGES = "channels.history"; private static final String ENDPOINT_POST_MESSAGE = "chat.postMessage"; + private static final String ENDPOINT_SEARCH_MESSAGE = "search.messages"; private static final String ENDPOINT_DELETE_MESSAGE = "chat.delete"; + private static final String USERS_INFO = "users.info"; + private static final String UNFURL_LINK = "true"; + private static final String UNFURL_MEDIA = "true"; - public static final String BOTS_CHANNEL_ID = "C2ABKERFT"; + private static final String USERNAME = "messybot"; + private static final String AS_USER = "false"; + private static final String IMAGE_URL = "https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3osxYpRxjzvPOtwfF6%2Fgiphy.gif"; + public static final String BOTS_CHANNEL_ID = "C2ADPS5MK"; + + public static String getUSERNAME() { + return USERNAME; + } /** * Static method to test the Slack API. @@ -68,6 +81,13 @@ public static ListMessagesResponse listMessages(String channelId) { return new ListMessagesResponse(HTTPS.get(listMessagesUrl)); } + public static SearchMessagesResponse searchMessages(String channelId, String query) { + + URL querySearch = HTTPS.stringToURL(BASE_URL + ENDPOINT_SEARCH_MESSAGE + "?token=" + CAT_API_KEY + "&channel=" + channelId + "&query=" + query + "&pretty=1"); + + return new SearchMessagesResponse(HTTPS.get(querySearch)); + } + /** * Static method to send a message to the #bots channel. * @@ -82,7 +102,7 @@ public static SendMessageResponse sendMessage(String messageText) { throw new RuntimeException(e); } - URL sendMessageUrl = HTTPS.stringToURL(BASE_URL + ENDPOINT_POST_MESSAGE + "?token=" + API_KEY + "&channel=" + BOTS_CHANNEL_ID + "&text=" + messageText); + URL sendMessageUrl = HTTPS.stringToURL(BASE_URL + ENDPOINT_POST_MESSAGE + "?token=" + API_KEY + "&channel=" + BOTS_CHANNEL_ID + "&text=" + messageText + "&unfurl_links=" + UNFURL_LINK + "&unfurl_media=" + UNFURL_MEDIA + "&username=" + USERNAME + "&as_user=" + AS_USER + "&icon_url=" + IMAGE_URL + "&pretty=1"); return new SendMessageResponse(HTTPS.get(sendMessageUrl)); } @@ -111,4 +131,38 @@ public static DeleteMessageResponse deleteMessage(String messageTs) { return new DeleteMessageResponse(HTTPS.get(deleteMessageUrl)); } + + public static Response addReaction() { + String emojiName = "hankey"; + + ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ADPS5MK"); + List messages = listMessagesResponse.getMessages(); + + for (Message message : messages) { + String timestamp = message.getTs(); + + URL reaction = HTTPS.stringToURL(BASE_URL + "reactions.add" + "?token=" + API_KEY + "&name=" + emojiName + "&channel=" + BOTS_CHANNEL_ID + "×tamp=" + timestamp + "&pretty=1"); + + return new Response(HTTPS.get(reaction)); + } + return addReaction(); + } + +// public static String userInfo() { +// URL username = HTTPS.stringToURL(BASE_URL + USERS_INFO + "?token=" + API_KEY + "&user=" + userId + "&pretty=1"); +// +// JSONObject json = HTTPS.get(username); +// +// if (json.containsKey("data")) { +// +// JSONObject myObj = (JSONObject) json.get("data"); +// String userId = (String) myObj.get("name"); +// System.out.println(userId); +// +// return userId; +// } +// return null; +// } } + + diff --git a/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java b/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java new file mode 100644 index 0000000..4e9e049 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java @@ -0,0 +1,41 @@ +package nyc.c4q.ramonaharrison.network; + +import com.sun.org.apache.regexp.internal.RE; +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.JSONObject; + +import java.io.UnsupportedEncodingException; +import java.net.URL; +import java.net.URLEncoder; +import java.util.List; +import java.util.Scanner; + + +public class UrbanDictionary { + + private static final String BASE_URL = "http://www.urbandictionary.com/"; + private static final String ENDPOINT_TEST = "define.php"; + private static final String TERM = "?term="; + private static final String UNFURL_LINK = "true"; + private static final String UNFURL_MEDIA = "true"; + + public static URL wordSearch() { + ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ADPS5MK"); + List messages = listMessagesResponse.getMessages(); + + for (Message message : messages) { + String query = message.getText(); + String substr = "+", regex = "\\s"; + query = query.replaceAll(regex,substr); + return HTTPS.stringToURL(BASE_URL + ENDPOINT_TEST + "?term=" + query + "&unfurl_links=" + UNFURL_LINK); + } + return wordSearch(); + } +} + +// Scanner input = new Scanner(System.in); +// System.out.println("enter a word: "); +// query = input.next(); \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/network/response/GiphyResponse.java b/src/nyc/c4q/ramonaharrison/network/response/GiphyResponse.java new file mode 100644 index 0000000..d9b50a7 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/network/response/GiphyResponse.java @@ -0,0 +1,48 @@ +package nyc.c4q.ramonaharrison.network.response; + +import nyc.c4q.ramonaharrison.model.*; +import nyc.c4q.ramonaharrison.network.Slack; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by CATWONG on 9/17/16. + */ + +public class GiphyResponse{ + + private String data; + private String url; + private String fixed_height_downsampled_url; + + + public GiphyResponse(JSONObject json) { + + if (json.get("data") != null) { + this.data = (String) json.get("data"); + } + + if (json.get("url") != null) { + this.url = (String) json.get("url"); + } + + if (json.get("fixed_height_downsampled_url") != null){ + this.fixed_height_downsampled_url = (String) json.get("url"); + } + } + + public String getData() { + return data; + } + public String getUrl() { + return url; + } + public String getFixed_height_downsampled_url() { + return fixed_height_downsampled_url; + } +} + + diff --git a/src/nyc/c4q/ramonaharrison/network/response/ListMessagesResponse.java b/src/nyc/c4q/ramonaharrison/network/response/ListMessagesResponse.java index 7edaca0..c80122c 100644 --- a/src/nyc/c4q/ramonaharrison/network/response/ListMessagesResponse.java +++ b/src/nyc/c4q/ramonaharrison/network/response/ListMessagesResponse.java @@ -19,6 +19,8 @@ public class ListMessagesResponse extends Response { private List messages; + private String user; + public ListMessagesResponse(JSONObject json) { super(json); @@ -26,8 +28,12 @@ public ListMessagesResponse(JSONObject json) { if (json.containsKey("messages")) { JSONArray array = (JSONArray) json.get("messages"); + if (json.containsKey("user")){ + this.user = (String) json.get("user"); + } + this.messages = new ArrayList(); - for (int i = 0; i < array.size(); i++) { + for (int i = 0; i < 1; i++) { this.messages.add(new Message((JSONObject) array.get(i))); } } @@ -36,4 +42,8 @@ public ListMessagesResponse(JSONObject json) { public List getMessages() { return messages; } + + public String getUser() { + return user; + } } diff --git a/src/nyc/c4q/ramonaharrison/network/response/Response.java b/src/nyc/c4q/ramonaharrison/network/response/Response.java index 20f66ff..1a9336a 100644 --- a/src/nyc/c4q/ramonaharrison/network/response/Response.java +++ b/src/nyc/c4q/ramonaharrison/network/response/Response.java @@ -2,6 +2,9 @@ import org.json.simple.JSONObject; +import java.net.MalformedURLException; +import java.net.URL; + /** * Created by Ramona Harrison * on 8/26/16 @@ -33,4 +36,5 @@ public boolean isOk() { public String getError() { return error; } + } diff --git a/src/nyc/c4q/ramonaharrison/network/response/SearchMessagesResponse.java b/src/nyc/c4q/ramonaharrison/network/response/SearchMessagesResponse.java new file mode 100644 index 0000000..1614313 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/network/response/SearchMessagesResponse.java @@ -0,0 +1,42 @@ +package nyc.c4q.ramonaharrison.network.response; + +import nyc.c4q.ramonaharrison.model.Message; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +// CANNOT SEARCH MESSAGES AS BOT. COULD NOT USE PUBLIC GENERATOR KEY +public class SearchMessagesResponse extends Response { + + private List messages; + private String user; + + public SearchMessagesResponse(JSONObject json) { + super(json); + + if (json.containsKey("messages")) { + JSONArray array = (JSONArray) json.get("messages"); + + if (json.containsKey("user")){ + this.user = (String) json.get("user"); + } + + this.messages = new ArrayList(); + for (int i = 0; i < 1; i++) { + this.messages.add(new Message((JSONObject) array.get(i))); + } + } + } + + public List getMessages() { + return messages; + } + + public String getUser() { + return user; + } +} + + diff --git a/src/nyc/c4q/ramonaharrison/network/response/UserInfoResponse.java b/src/nyc/c4q/ramonaharrison/network/response/UserInfoResponse.java new file mode 100644 index 0000000..17ecde9 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/network/response/UserInfoResponse.java @@ -0,0 +1,38 @@ +package nyc.c4q.ramonaharrison.network.response; + +import org.json.simple.JSONObject; +import nyc.c4q.ramonaharrison.model.User; + + +public class UserInfoResponse extends Response { + + private User user; + private User name; + + public UserInfoResponse(JSONObject json) { + super(json); + + if (json.containsKey("user")) { + this.user = new User((JSONObject) json.get("user")); + } + + if (json.containsKey("name")) { + this.name = new User((JSONObject) json.get("name")); + } + } + + public User getUser() { + return user; + } + + public User getName() { + return name; + } +} + + + + + + + From d7c0bbeb2eed56f074aed5f9b4700bd39df629a7 Mon Sep 17 00:00:00 2001 From: Cat Wong Date: Sat, 17 Sep 2016 17:33:58 -0400 Subject: [PATCH 2/8] first commit --- src/nyc/c4q/ramonaharrison/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nyc/c4q/ramonaharrison/Main.java b/src/nyc/c4q/ramonaharrison/Main.java index ef14469..a3ead2b 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -31,7 +31,7 @@ public static void main(String[] args){ // myBot.listMessages(Slack.BOTS_CHANNEL_ID); // myBot.respondDictionary("C2ADPS5MK"); - myBot.respondMeme("C2ADPS5MK"); +// myBot.respondMeme("C2ADPS5MK"); // myBot.respondGiphy("C2ADPS5MK"); // myBot.sendMessageToBotsChannel(":poop: messybot will be under maintenance."); From 867cb32ec4aac51c5e1eac8dd168fd06b34bed37 Mon Sep 17 00:00:00 2001 From: Cat Wong Date: Sat, 17 Sep 2016 21:44:10 -0400 Subject: [PATCH 3/8] second commit --- src/nyc/c4q/ramonaharrison/Bot.java | 51 ++++++++++++------- src/nyc/c4q/ramonaharrison/Main.java | 31 ++++++----- .../c4q/ramonaharrison/model/Attachment.java | 2 +- src/nyc/c4q/ramonaharrison/model/User.java | 2 +- src/nyc/c4q/ramonaharrison/network/Giphy.java | 2 +- .../network/UrbanDictionary.java | 8 ++- .../response/ListMessagesResponse.java | 2 +- 7 files changed, 61 insertions(+), 37 deletions(-) diff --git a/src/nyc/c4q/ramonaharrison/Bot.java b/src/nyc/c4q/ramonaharrison/Bot.java index 1761a89..e5098fb 100644 --- a/src/nyc/c4q/ramonaharrison/Bot.java +++ b/src/nyc/c4q/ramonaharrison/Bot.java @@ -18,8 +18,7 @@ public class Bot { - boolean checkGreeting = false; - + Slack myBot; public Bot() { } @@ -61,22 +60,30 @@ public void respondGreeting(String channelId) { ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); if (listMessagesResponse.isOk()) { - List messages = listMessagesResponse.getMessages(); - Slack myBot = new Slack(); + List messages = listMessagesResponse.getMessages(); - for (Message message : messages) { + myBot = new Slack(); + + for (Message message : messages) { - String whatDidYouSay = message.getText(); + String whatDidYouSay = message.getText(); - if (whatDidYouSay.contains("@U2ADRJVK9") || whatDidYouSay.contains("messybot")) { - myBot.sendMessage("Hello " + message.getUser().toString()); - myBot.sendMessage("I am " + Slack.getUSERNAME()); - myBot.sendMessage(":poop::poop::poop:"); - myBot.sendMessage("Give me a word and I'll find it for you."); + if (whatDidYouSay.contains("@U2ADRJVK9") || whatDidYouSay.contains("messybot")) { + if (whatDidYouSay.contains("Give me a word and I'll find it for you.")) { + + } else { + if (message.getUser() != null) { + + myBot.sendMessage("Hello " + message.getUser().toString()); + } + myBot.sendMessage("I am " + Slack.getUSERNAME()); + myBot.sendMessage(":poop::poop::poop:"); + myBot.sendMessage("Did you say " + whatDidYouSay + "?"); + } + } } - } - } else { + }else { System.err.print("Error listing messages: " + listMessagesResponse.getError()); } } @@ -97,7 +104,7 @@ public void respondAll(String channelId) { String word = message.getText(); // while (word != null) { - if (word == word) { + URL slangSearch = urbanDictionary.wordSearch(); slangSearch.toString(); @@ -110,14 +117,13 @@ public void respondAll(String channelId) { myBot.sendMessage(memeSearch.toString()); myBot.sendMessage(giphySearch); // } - } + } } else { System.err.print("Error listing messages: " + listMessagesResponse.getError()); } } - public void respondDictionary(String channelId) { ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); @@ -128,11 +134,18 @@ public void respondDictionary(String channelId) { UrbanDictionary urbanDictionary = new UrbanDictionary(); for (Message message : messages) { - - String word = message.getText(); String user = message.getUser(); + String word = messages.get(0).getText(); + + String[] parts = word.split(" "); + String part1 = parts[0]; // @messybot + String part2 = parts[1]; // query word + System.out.println(part2); + + StringBuilder builder = new StringBuilder(); + builder.append(part2); - if (word == word) { + if (builder.indexOf("Give me a word") == -1) { URL slangSearch = urbanDictionary.wordSearch(); slangSearch.toString(); diff --git a/src/nyc/c4q/ramonaharrison/Main.java b/src/nyc/c4q/ramonaharrison/Main.java index a3ead2b..1a33ff3 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -9,26 +9,31 @@ public class Main { public static void main(String[] args){ - Bot myBot = new Bot(); - // myBot.testApi(); -// myBot.deleteMessageInBotsChannel("1474042536.000733"); + Bot myBot = new Bot(); - myBot.listChannels(); - myBot.listMessages(Slack.BOTS_CHANNEL_ID); +// myBot.testApi(); +// myBot.deleteMessageInBotsChannel("1474042536.000733"); - myBot.addReaction(); + myBot.listChannels(); + myBot.listMessages(Slack.BOTS_CHANNEL_ID); - myBot.respondGreeting("C2ADPS5MK"); + myBot.addReaction(); - myBot.addReaction(); + myBot.respondGreeting("C2ADPS5MK"); + + myBot.addReaction(); + + myBot.listMessages(Slack.BOTS_CHANNEL_ID); + + myBot.respondAll("C2ADPS5MK"); + + myBot.addReaction(); + + myBot.listMessages(Slack.BOTS_CHANNEL_ID); - myBot.listMessages(Slack.BOTS_CHANNEL_ID); -// myBot.respondAll("C2ADPS5MK"); -// -// myBot.addReaction(); // -// myBot.listMessages(Slack.BOTS_CHANNEL_ID); +// myBot.listMessages(Slack.BOTS_CHANNEL_ID); // myBot.respondDictionary("C2ADPS5MK"); // myBot.respondMeme("C2ADPS5MK"); diff --git a/src/nyc/c4q/ramonaharrison/model/Attachment.java b/src/nyc/c4q/ramonaharrison/model/Attachment.java index 8cda3f3..cf0a259 100644 --- a/src/nyc/c4q/ramonaharrison/model/Attachment.java +++ b/src/nyc/c4q/ramonaharrison/model/Attachment.java @@ -17,7 +17,7 @@ public class Attachment { - // TODO: implement private fields for each of the following attachment JSON keys: + // implement private fields for each of the following attachment JSON keys: // "fallback" // "color" // "pretext" diff --git a/src/nyc/c4q/ramonaharrison/model/User.java b/src/nyc/c4q/ramonaharrison/model/User.java index 748430d..2d9b7c0 100644 --- a/src/nyc/c4q/ramonaharrison/model/User.java +++ b/src/nyc/c4q/ramonaharrison/model/User.java @@ -17,7 +17,7 @@ public class User { - // TODO: implement private fields for each of the following user JSON keys: + // implement private fields for each of the following user JSON keys: // "id" // "name" // "deleted" diff --git a/src/nyc/c4q/ramonaharrison/network/Giphy.java b/src/nyc/c4q/ramonaharrison/network/Giphy.java index 343561a..30aa76e 100644 --- a/src/nyc/c4q/ramonaharrison/network/Giphy.java +++ b/src/nyc/c4q/ramonaharrison/network/Giphy.java @@ -44,7 +44,7 @@ public static String giphySearch() { String giphyString = myObj.get("fixed_height_downsampled_url").toString(); System.out.println(giphyString); return giphyString; - }else + } else return null; } return " "; diff --git a/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java b/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java index 4e9e049..e418c52 100644 --- a/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java +++ b/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java @@ -27,9 +27,15 @@ public static URL wordSearch() { List messages = listMessagesResponse.getMessages(); for (Message message : messages) { - String query = message.getText(); + String query = messages.get(0).getText();; String substr = "+", regex = "\\s"; query = query.replaceAll(regex,substr); + + StringBuilder builder = new StringBuilder(); + builder.append(query); + + query = query.substring(builder.indexOf("@U2ADRJVK9") + 12); + return HTTPS.stringToURL(BASE_URL + ENDPOINT_TEST + "?term=" + query + "&unfurl_links=" + UNFURL_LINK); } return wordSearch(); diff --git a/src/nyc/c4q/ramonaharrison/network/response/ListMessagesResponse.java b/src/nyc/c4q/ramonaharrison/network/response/ListMessagesResponse.java index c80122c..81e5df9 100644 --- a/src/nyc/c4q/ramonaharrison/network/response/ListMessagesResponse.java +++ b/src/nyc/c4q/ramonaharrison/network/response/ListMessagesResponse.java @@ -21,7 +21,6 @@ public class ListMessagesResponse extends Response { private List messages; private String user; - public ListMessagesResponse(JSONObject json) { super(json); @@ -35,6 +34,7 @@ public ListMessagesResponse(JSONObject json) { this.messages = new ArrayList(); for (int i = 0; i < 1; i++) { this.messages.add(new Message((JSONObject) array.get(i))); + } } } From 63dd10dc5a4524e25e3487ab164620ea7719ce13 Mon Sep 17 00:00:00 2001 From: Cat Wong Date: Sat, 17 Sep 2016 21:52:35 -0400 Subject: [PATCH 4/8] third commit. updated Giphy url query --- src/nyc/c4q/ramonaharrison/network/Giphy.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/nyc/c4q/ramonaharrison/network/Giphy.java b/src/nyc/c4q/ramonaharrison/network/Giphy.java index 30aa76e..4f25602 100644 --- a/src/nyc/c4q/ramonaharrison/network/Giphy.java +++ b/src/nyc/c4q/ramonaharrison/network/Giphy.java @@ -33,6 +33,11 @@ public static String giphySearch() { String substr = "+", regex = "\\s"; query = query.replaceAll(regex, substr); + StringBuilder builder = new StringBuilder(); + builder.append(query); + + query = query.substring(builder.indexOf("@U2ADRJVK9") + 12); + URL giphyURL = HTTPS.stringToURL(BASE_URL + ENDPOINT_TEST + "random?" + "api_key=" + API + "&tag=" + query); System.out.println(giphyURL); From 7be2f2bd701a34ccc83721d6b1a1f4995255044b Mon Sep 17 00:00:00 2001 From: Cat Wong Date: Sat, 17 Sep 2016 22:02:43 -0400 Subject: [PATCH 5/8] fourth commit. made more changes to Bot.java class in responseAll method --- src/nyc/c4q/ramonaharrison/Bot.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nyc/c4q/ramonaharrison/Bot.java b/src/nyc/c4q/ramonaharrison/Bot.java index e5098fb..74ccc56 100644 --- a/src/nyc/c4q/ramonaharrison/Bot.java +++ b/src/nyc/c4q/ramonaharrison/Bot.java @@ -77,9 +77,9 @@ public void respondGreeting(String channelId) { myBot.sendMessage("Hello " + message.getUser().toString()); } - myBot.sendMessage("I am " + Slack.getUSERNAME()); + myBot.sendMessage("I am messybot"); myBot.sendMessage(":poop::poop::poop:"); - myBot.sendMessage("Did you say " + whatDidYouSay + "?"); + myBot.sendMessage("You asked for " + whatDidYouSay); } } } From aca410ac895773f06ff393ecacbbeeb40ca79eb7 Mon Sep 17 00:00:00 2001 From: Cat Wong Date: Sat, 17 Sep 2016 22:36:12 -0400 Subject: [PATCH 6/8] fourth commit --- src/nyc/c4q/ramonaharrison/Bot.java | 27 +++++++++++++-------------- src/nyc/c4q/ramonaharrison/Main.java | 2 -- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/nyc/c4q/ramonaharrison/Bot.java b/src/nyc/c4q/ramonaharrison/Bot.java index 74ccc56..9635edd 100644 --- a/src/nyc/c4q/ramonaharrison/Bot.java +++ b/src/nyc/c4q/ramonaharrison/Bot.java @@ -67,22 +67,18 @@ public void respondGreeting(String channelId) { for (Message message : messages) { - String whatDidYouSay = message.getText(); - - if (whatDidYouSay.contains("@U2ADRJVK9") || whatDidYouSay.contains("messybot")) { - if (whatDidYouSay.contains("Give me a word and I'll find it for you.")) { - - } else { - if (message.getUser() != null) { - - myBot.sendMessage("Hello " + message.getUser().toString()); + String whatDidYouSay = ""; + whatDidYouSay = message.getText(); + if (whatDidYouSay.contains("@U2ADRJVK9") || whatDidYouSay.contains("messybot")) { + if (message.getUser() != null) { + + myBot.sendMessage("Hello " + message.getUser().toString()); + } + myBot.sendMessage("I am messybot"); + myBot.sendMessage(":poop::poop::poop:"); + myBot.sendMessage("You asked for " + whatDidYouSay); } - myBot.sendMessage("I am messybot"); - myBot.sendMessage(":poop::poop::poop:"); - myBot.sendMessage("You asked for " + whatDidYouSay); } - } - } }else { System.err.print("Error listing messages: " + listMessagesResponse.getError()); } @@ -114,8 +110,11 @@ public void respondAll(String channelId) { String giphySearch = giphy.giphySearch(); myBot.sendMessage(slangSearch.toString()); + Slack.addReaction(); myBot.sendMessage(memeSearch.toString()); + Slack.addReaction(); myBot.sendMessage(giphySearch); + Slack.addReaction(); // } } diff --git a/src/nyc/c4q/ramonaharrison/Main.java b/src/nyc/c4q/ramonaharrison/Main.java index 1a33ff3..47a272c 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -27,8 +27,6 @@ public static void main(String[] args){ myBot.respondAll("C2ADPS5MK"); - myBot.addReaction(); - myBot.listMessages(Slack.BOTS_CHANNEL_ID); From ad289bb40af53b79dd831ac7610666129027a55e Mon Sep 17 00:00:00 2001 From: Cat Wong Date: Sun, 18 Sep 2016 15:54:11 -0400 Subject: [PATCH 7/8] final commit --- src/nyc/c4q/ramonaharrison/Bot.java | 42 +++++++------------ src/nyc/c4q/ramonaharrison/Main.java | 13 ++++-- src/nyc/c4q/ramonaharrison/network/Giphy.java | 4 +- src/nyc/c4q/ramonaharrison/network/Slack.java | 4 +- .../network/UrbanDictionary.java | 4 +- 5 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/nyc/c4q/ramonaharrison/Bot.java b/src/nyc/c4q/ramonaharrison/Bot.java index 9635edd..22e44b5 100644 --- a/src/nyc/c4q/ramonaharrison/Bot.java +++ b/src/nyc/c4q/ramonaharrison/Bot.java @@ -99,24 +99,20 @@ public void respondAll(String channelId) { String word = message.getText(); -// while (word != null) { + URL slangSearch = urbanDictionary.wordSearch(); + slangSearch.toString(); - URL slangSearch = urbanDictionary.wordSearch(); - slangSearch.toString(); - - URL memeSearch = memes.memeSearch(); - memeSearch.toString(); - - String giphySearch = giphy.giphySearch(); + URL memeSearch = memes.memeSearch(); + memeSearch.toString(); - myBot.sendMessage(slangSearch.toString()); - Slack.addReaction(); - myBot.sendMessage(memeSearch.toString()); - Slack.addReaction(); - myBot.sendMessage(giphySearch); - Slack.addReaction(); -// } + String giphySearch = giphy.giphySearch(); + myBot.sendMessage(slangSearch.toString()); + Slack.addReaction(); + myBot.sendMessage(memeSearch.toString()); + Slack.addReaction(); + myBot.sendMessage(giphySearch); + Slack.addReaction(); } } else { System.err.print("Error listing messages: " + listMessagesResponse.getError()); @@ -134,17 +130,9 @@ public void respondDictionary(String channelId) { for (Message message : messages) { String user = message.getUser(); - String word = messages.get(0).getText(); - - String[] parts = word.split(" "); - String part1 = parts[0]; // @messybot - String part2 = parts[1]; // query word - System.out.println(part2); - - StringBuilder builder = new StringBuilder(); - builder.append(part2); + String word = message.getText(); - if (builder.indexOf("Give me a word") == -1) { + if (word.contains("ud")) { URL slangSearch = urbanDictionary.wordSearch(); slangSearch.toString(); @@ -168,7 +156,7 @@ public void respondMeme(String channelId) { for (Message message : messages) { String aMeme = message.getText(); - if (aMeme == aMeme) { + if (aMeme.contains("meme")) { URL memeSearch = memes.memeSearch(); memeSearch.toString(); myBot.sendMessage(memeSearch.toString()); @@ -191,7 +179,7 @@ public void respondGiphy(String channelId) { for (Message message : messages) { String aGiphy = message.getText(); - if (aGiphy == aGiphy) { + if (aGiphy.contains("giphy")) { String giphySearch = giphy.giphySearch(); myBot.sendMessage(giphySearch.toString()); } diff --git a/src/nyc/c4q/ramonaharrison/Main.java b/src/nyc/c4q/ramonaharrison/Main.java index 47a272c..4f5e7dc 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -19,16 +19,23 @@ public static void main(String[] args){ myBot.addReaction(); - myBot.respondGreeting("C2ADPS5MK"); + myBot.respondGreeting("C2ABKERFT"); myBot.addReaction(); myBot.listMessages(Slack.BOTS_CHANNEL_ID); - myBot.respondAll("C2ADPS5MK"); +// myBot.respondAll("C2ABKERFT"); + myBot.respondDictionary("C2ABKERFT"); + myBot.addReaction(); - myBot.listMessages(Slack.BOTS_CHANNEL_ID); + myBot.respondMeme("C2ABKERFT"); + myBot.addReaction(); + + myBot.respondGiphy("C2ABKERFT"); + myBot.addReaction(); + myBot.listMessages(Slack.BOTS_CHANNEL_ID); // // myBot.listMessages(Slack.BOTS_CHANNEL_ID); diff --git a/src/nyc/c4q/ramonaharrison/network/Giphy.java b/src/nyc/c4q/ramonaharrison/network/Giphy.java index 4f25602..a99bc98 100644 --- a/src/nyc/c4q/ramonaharrison/network/Giphy.java +++ b/src/nyc/c4q/ramonaharrison/network/Giphy.java @@ -25,7 +25,7 @@ public class Giphy { public static String giphySearch() { - ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ADPS5MK"); + ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ABKERFT"); List messages = listMessagesResponse.getMessages(); for (Message message : messages) { @@ -36,7 +36,7 @@ public static String giphySearch() { StringBuilder builder = new StringBuilder(); builder.append(query); - query = query.substring(builder.indexOf("@U2ADRJVK9") + 12); + query = query.substring(builder.indexOf("@U2ADRJVK9") + 18); URL giphyURL = HTTPS.stringToURL(BASE_URL + ENDPOINT_TEST + "random?" + "api_key=" + API + "&tag=" + query); System.out.println(giphyURL); diff --git a/src/nyc/c4q/ramonaharrison/network/Slack.java b/src/nyc/c4q/ramonaharrison/network/Slack.java index 787bec0..17a08d9 100644 --- a/src/nyc/c4q/ramonaharrison/network/Slack.java +++ b/src/nyc/c4q/ramonaharrison/network/Slack.java @@ -37,7 +37,7 @@ public class Slack { private static final String USERNAME = "messybot"; private static final String AS_USER = "false"; private static final String IMAGE_URL = "https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3osxYpRxjzvPOtwfF6%2Fgiphy.gif"; - public static final String BOTS_CHANNEL_ID = "C2ADPS5MK"; + public static final String BOTS_CHANNEL_ID = "C2ABKERFT"; public static String getUSERNAME() { return USERNAME; @@ -135,7 +135,7 @@ public static DeleteMessageResponse deleteMessage(String messageTs) { public static Response addReaction() { String emojiName = "hankey"; - ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ADPS5MK"); + ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ABKERFT"); List messages = listMessagesResponse.getMessages(); for (Message message : messages) { diff --git a/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java b/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java index e418c52..9153ec8 100644 --- a/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java +++ b/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java @@ -34,7 +34,9 @@ public static URL wordSearch() { StringBuilder builder = new StringBuilder(); builder.append(query); - query = query.substring(builder.indexOf("@U2ADRJVK9") + 12); + query = query.substring(builder.indexOf("@U2ADRJVK9") + 15); +// query = query.substring(builder.lastIndexOf("@U2ADRJVK9 ud") + 5); + return HTTPS.stringToURL(BASE_URL + ENDPOINT_TEST + "?term=" + query + "&unfurl_links=" + UNFURL_LINK); } From ecfa24e00ad3da89524982967b60b2bac9dba0d2 Mon Sep 17 00:00:00 2001 From: Cat Wong Date: Sat, 24 Sep 2016 19:15:54 -0400 Subject: [PATCH 8/8] bug fix/changes - added userInfo(), minor refactoring, started Yelp API and Buttons --- src/nyc/c4q/ramonaharrison/Bot.java | 205 ++++++++---------- src/nyc/c4q/ramonaharrison/Main.java | 52 ++--- src/nyc/c4q/ramonaharrison/model/Action.java | 35 +++ .../c4q/ramonaharrison/model/Attachment.java | 16 +- src/nyc/c4q/ramonaharrison/model/Channel.java | 2 - .../ramonaharrison/model/Confirmation.java | 7 + src/nyc/c4q/ramonaharrison/model/Field.java | 1 - .../c4q/ramonaharrison/model/GiphyData.java | 67 +++--- .../c4q/ramonaharrison/model/GiphyMeta.java | 16 +- src/nyc/c4q/ramonaharrison/model/Message.java | 48 ++-- src/nyc/c4q/ramonaharrison/model/Profile.java | 4 +- src/nyc/c4q/ramonaharrison/model/Purpose.java | 17 +- src/nyc/c4q/ramonaharrison/model/Topic.java | 17 +- src/nyc/c4q/ramonaharrison/model/User.java | 7 +- src/nyc/c4q/ramonaharrison/network/Giphy.java | 24 +- src/nyc/c4q/ramonaharrison/network/HTTPS.java | 2 +- src/nyc/c4q/ramonaharrison/network/Memes.java | 12 +- src/nyc/c4q/ramonaharrison/network/Slack.java | 66 +++--- .../network/UrbanDictionary.java | 25 +-- .../response/DeleteMessageResponse.java | 3 +- .../network/response/GiphyResponse.java | 12 +- .../response/ListChannelsResponse.java | 3 +- .../response/ListMessagesResponse.java | 5 +- .../network/response/Response.java | 6 +- .../response/SearchMessagesResponse.java | 2 +- .../network/response/SendMessageResponse.java | 3 +- .../network/response/UserInfoResponse.java | 38 ---- src/nyc/c4q/ramonaharrison/util/Token.java | 2 +- 28 files changed, 309 insertions(+), 388 deletions(-) create mode 100644 src/nyc/c4q/ramonaharrison/model/Action.java create mode 100644 src/nyc/c4q/ramonaharrison/model/Confirmation.java delete mode 100644 src/nyc/c4q/ramonaharrison/network/response/UserInfoResponse.java diff --git a/src/nyc/c4q/ramonaharrison/Bot.java b/src/nyc/c4q/ramonaharrison/Bot.java index 22e44b5..88dfe97 100644 --- a/src/nyc/c4q/ramonaharrison/Bot.java +++ b/src/nyc/c4q/ramonaharrison/Bot.java @@ -1,24 +1,19 @@ package nyc.c4q.ramonaharrison; import nyc.c4q.ramonaharrison.model.Channel; -import nyc.c4q.ramonaharrison.model.GiphyData; import nyc.c4q.ramonaharrison.model.Message; - -import nyc.c4q.ramonaharrison.model.User; -import nyc.c4q.ramonaharrison.network.*; +import nyc.c4q.ramonaharrison.network.Giphy; +import nyc.c4q.ramonaharrison.network.Memes; +import nyc.c4q.ramonaharrison.network.Slack; +import nyc.c4q.ramonaharrison.network.UrbanDictionary; import nyc.c4q.ramonaharrison.network.response.*; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; - -import java.net.MalformedURLException; import java.net.URL; import java.util.List; -import java.util.Random; -import java.util.regex.Pattern; public class Bot { Slack myBot; + public Bot() { } @@ -28,11 +23,6 @@ public void addReaction() { System.out.println(reaction); } -// public void userInfo() { -// UserInfoResponse user = Slack.userInfo("U23CZAXQS"); -// System.out.println(user); -// } - /** * Sample method: prints up to the last 100 messages from a given channel. Prints an error message on failure. * or failure. @@ -57,31 +47,29 @@ public void listMessages(String channelId) { } public void respondGreeting(String channelId) { - ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); - if (listMessagesResponse.isOk()) { + ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); + + if (listMessagesResponse.isOk()) { List messages = listMessagesResponse.getMessages(); myBot = new Slack(); - for (Message message : messages) { - - String whatDidYouSay = ""; - whatDidYouSay = message.getText(); - if (whatDidYouSay.contains("@U2ADRJVK9") || whatDidYouSay.contains("messybot")) { - if (message.getUser() != null) { - - myBot.sendMessage("Hello " + message.getUser().toString()); - } - myBot.sendMessage("I am messybot"); - myBot.sendMessage(":poop::poop::poop:"); - myBot.sendMessage("You asked for " + whatDidYouSay); - } - } - }else { - System.err.print("Error listing messages: " + listMessagesResponse.getError()); - } + String whatDidYouSay = messages.get(0).getText(); + Message userMessage = messages.get(0); + + if (whatDidYouSay.contains("@U2ADRJVK9") || whatDidYouSay.contains("messybot")) { + if (userMessage.getUser() != null) { + myBot.sendMessage("Hello " + myBot.userInfo()); + } + myBot.sendMessage("I am " + myBot.getUSERNAME()); + myBot.sendMessage(":poop::poop::poop:"); + myBot.sendMessage("You asked for " + whatDidYouSay); + } + } else { + System.err.print("Error listing messages: " + listMessagesResponse.getError()); + } } public void respondAll(String channelId) { @@ -90,29 +78,31 @@ public void respondAll(String channelId) { if (listMessagesResponse.isOk()) { List messages = listMessagesResponse.getMessages(); - Slack myBot = new Slack(); + myBot = new Slack(); UrbanDictionary urbanDictionary = new UrbanDictionary(); Memes memes = new Memes(); Giphy giphy = new Giphy(); for (Message message : messages) { - String word = message.getText(); - URL slangSearch = urbanDictionary.wordSearch(); - slangSearch.toString(); + if (word.contains("give me")) { + + URL slangSearch = urbanDictionary.wordSearch(); + slangSearch.toString(); - URL memeSearch = memes.memeSearch(); - memeSearch.toString(); + URL memeSearch = memes.memeSearch(); + memeSearch.toString(); - String giphySearch = giphy.giphySearch(); + String giphySearch = giphy.giphySearch(); - myBot.sendMessage(slangSearch.toString()); - Slack.addReaction(); - myBot.sendMessage(memeSearch.toString()); - Slack.addReaction(); - myBot.sendMessage(giphySearch); - Slack.addReaction(); + myBot.sendMessage(slangSearch.toString()); + Slack.addReaction(); + myBot.sendMessage(memeSearch.toString()); + Slack.addReaction(); + myBot.sendMessage(giphySearch); + Slack.addReaction(); + } } } else { System.err.print("Error listing messages: " + listMessagesResponse.getError()); @@ -125,18 +115,16 @@ public void respondDictionary(String channelId) { if (listMessagesResponse.isOk()) { List messages = listMessagesResponse.getMessages(); - Slack myBot = new Slack(); + myBot = new Slack(); UrbanDictionary urbanDictionary = new UrbanDictionary(); for (Message message : messages) { - String user = message.getUser(); String word = message.getText(); if (word.contains("ud")) { URL slangSearch = urbanDictionary.wordSearch(); slangSearch.toString(); - - myBot.sendMessage(slangSearch.toString()); + myBot.sendMessage(slangSearch.toString()); } } } else { @@ -150,7 +138,7 @@ public void respondMeme(String channelId) { if (listMessagesResponse.isOk()) { List messages = listMessagesResponse.getMessages(); - Slack myBot = new Slack(); + myBot = new Slack(); Memes memes = new Memes(); for (Message message : messages) { @@ -159,7 +147,7 @@ public void respondMeme(String channelId) { if (aMeme.contains("meme")) { URL memeSearch = memes.memeSearch(); memeSearch.toString(); - myBot.sendMessage(memeSearch.toString()); + myBot.sendMessage(memeSearch.toString()); } } } else { @@ -173,7 +161,7 @@ public void respondGiphy(String channelId) { if (listMessagesResponse.isOk()) { List messages = listMessagesResponse.getMessages(); - Slack myBot = new Slack(); + myBot = new Slack(); Giphy giphy = new Giphy(); for (Message message : messages) { @@ -181,85 +169,68 @@ public void respondGiphy(String channelId) { if (aGiphy.contains("giphy")) { String giphySearch = giphy.giphySearch(); - myBot.sendMessage(giphySearch.toString()); + myBot.sendMessage(giphySearch); } } - } else { System.err.print("Error listing messages: " + listMessagesResponse.getError()); } } - /** - * Sample method: tests the Slack API. Prints a message indicating success or failure. - */ - public void testApi () { - Response apiTest = Slack.testApi(); - System.out.println("API OK: " + apiTest.isOk() + "\n"); - } - /** - * Sample method: prints all public AccessCode3-3 channel names and ids. Prints an error message on failure. - */ - public void listChannels () { - ListChannelsResponse listChannelsResponse = Slack.listChannels(); - - if (listChannelsResponse.isOk()) { - List channels = listChannelsResponse.getChannels(); + /** + * Sample method: tests the Slack API. Prints a message indicating success or failure. + */ + public void testApi() { + Response apiTest = Slack.testApi(); + System.out.println("API OK: " + apiTest.isOk() + "\n"); + } - System.out.println("\nChannels: "); - for (Channel channel : channels) { - System.out.println("name: " + channel.getName() + ", id:" + channel.getId()); - } - } else { - System.err.print("Error listing channels: " + listChannelsResponse.getError()); - } - } + /** + * Sample method: prints all public AccessCode3-3 channel names and ids. Prints an error message on failure. + */ + public void listChannels() { + ListChannelsResponse listChannelsResponse = Slack.listChannels(); - /** - * Sample method: sends a plain text message to the #bots channel. Prints a message indicating success or failure. - * - * @param text message text. - */ - public void sendMessageToBotsChannel (String text){ - SendMessageResponse sendMessageResponse = Slack.sendMessage(text); + if (listChannelsResponse.isOk()) { + List channels = listChannelsResponse.getChannels(); - if (sendMessageResponse.isOk()) { - System.out.println("Message sent successfully!"); - } else { - System.err.print("Error sending message: " + sendMessageResponse.getError()); + System.out.println("\nChannels: "); + for (Channel channel : channels) { + System.out.println("name: " + channel.getName() + ", id:" + channel.getId()); } + } else { + System.err.print("Error listing channels: " + listChannelsResponse.getError()); } + } - /** - * Sample method: deletes a message from the #bots channel. Prints a message indicating success or failure. - * - * @param messageTs unique timestamp of the message to be deleted. - */ - public void deleteMessageInBotsChannel (String messageTs){ - DeleteMessageResponse deleteMessageResponse = Slack.deleteMessage(messageTs); + /** + * Sample method: sends a plain text message to the #bots channel. Prints a message indicating success or failure. + * + * @param text message text. + */ + public void sendMessageToBotsChannel(String text) { + SendMessageResponse sendMessageResponse = Slack.sendMessage(text); - if (deleteMessageResponse.isOk()) { - System.out.println("Message deleted successfully!"); - } else { - System.err.print("Error sending message: " + deleteMessageResponse.getError()); - } + if (sendMessageResponse.isOk()) { + System.out.println("Message sent successfully!"); + } else { + System.err.print("Error sending message: " + sendMessageResponse.getError()); } + } - public void searchMessages (String channelId, String query){ - SearchMessagesResponse querySearch = Slack.searchMessages(channelId, query); - - if (querySearch.isOk()) { - List messages = querySearch.getMessages(); - System.out.println("You searched for: " + querySearch); + /** + * Sample method: deletes a message from the #bots channel. Prints a message indicating success or failure. + * + * @param messageTs unique timestamp of the message to be deleted. + */ + public void deleteMessageInBotsChannel(String messageTs) { + DeleteMessageResponse deleteMessageResponse = Slack.deleteMessage(messageTs); - for (Message queryWord : messages) { - System.out.println(); - System.out.println("Timestamp: " + queryWord.getTs()); - System.out.println("Message: " + queryWord.getText()); - System.out.println("User: " + queryWord.getUser()); - } - } else { - System.err.print("Error listing messages: " + querySearch.getError()); - } + if (deleteMessageResponse.isOk()) { + System.out.println("Message deleted successfully!"); + } else { + System.err.print("Error sending message: " + deleteMessageResponse.getError()); } } + +} diff --git a/src/nyc/c4q/ramonaharrison/Main.java b/src/nyc/c4q/ramonaharrison/Main.java index 4f5e7dc..01ceee0 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -1,54 +1,34 @@ package nyc.c4q.ramonaharrison; -import nyc.c4q.ramonaharrison.network.Giphy; -import nyc.c4q.ramonaharrison.network.UrbanDictionary; import nyc.c4q.ramonaharrison.network.Slack; - public class Main { - public static void main(String[] args){ + // 9/23 - retrieved username from last message. did some refactoring (deleted unused methods). changed Giphy url to read + // actual url instead of small height fixed url. continue working on respondAll() and started working + // on Yelp API integration. + + public static void main(String[] args) { - Bot myBot = new Bot(); + Bot myBot = new Bot(); // myBot.testApi(); // myBot.deleteMessageInBotsChannel("1474042536.000733"); - myBot.listChannels(); - myBot.listMessages(Slack.BOTS_CHANNEL_ID); - - myBot.addReaction(); - - myBot.respondGreeting("C2ABKERFT"); - - myBot.addReaction(); - - myBot.listMessages(Slack.BOTS_CHANNEL_ID); - -// myBot.respondAll("C2ABKERFT"); - myBot.respondDictionary("C2ABKERFT"); - myBot.addReaction(); - - myBot.respondMeme("C2ABKERFT"); - myBot.addReaction(); - - myBot.respondGiphy("C2ABKERFT"); - myBot.addReaction(); - + myBot.listChannels(); myBot.listMessages(Slack.BOTS_CHANNEL_ID); -// -// myBot.listMessages(Slack.BOTS_CHANNEL_ID); - -// myBot.respondDictionary("C2ADPS5MK"); -// myBot.respondMeme("C2ADPS5MK"); -// myBot.respondGiphy("C2ADPS5MK"); +// myBot.addReaction(); + myBot.respondGreeting("C2ERNHJ5D"); + myBot.respondDictionary("C2ERNHJ5D"); + myBot.respondMeme("C2ERNHJ5D"); + myBot.respondGiphy("C2ERNHJ5D"); -// myBot.sendMessageToBotsChannel(":poop: messybot will be under maintenance."); +// myBot.listMessages(Slack.BOTS_CHANNEL_ID); +// myBot.sendMessageToBotsChannel(":poop: messybot will be under maintenance."); - // non working method-calls. NEED TO REVISIT OR DELETE -// myBot.respondMeme("C2ADPS5MK"); -// myBot.searchMessages(Slack.BOTS_CHANNEL_ID, "messybot"); +// I am working on code below. +// myBot.respondAll("C2ERNHJ5D"); } } \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/model/Action.java b/src/nyc/c4q/ramonaharrison/model/Action.java new file mode 100644 index 0000000..3dbed78 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Action.java @@ -0,0 +1,35 @@ +package nyc.c4q.ramonaharrison.model; + +import org.json.simple.JSONObject; + +public class Action { + + private String name; + private String text; + private String style; + private String type; + private String value; + + public Action(JSONObject json) { + + if (json.containsKey("name")) { + this.name = (String) json.get("name"); + } + + if (json.containsKey("text")) { + this.text = (String) json.get("text"); + } + + if (json.containsKey("style")) { + this.style = (String) json.get("style"); + } + + if (json.containsKey("type")) { + this.type = (String) json.get("type"); + } + + if (json.containsKey("value")) { + this.value = (String) json.get("value"); + } + } +} diff --git a/src/nyc/c4q/ramonaharrison/model/Attachment.java b/src/nyc/c4q/ramonaharrison/model/Attachment.java index cf0a259..cfba741 100644 --- a/src/nyc/c4q/ramonaharrison/model/Attachment.java +++ b/src/nyc/c4q/ramonaharrison/model/Attachment.java @@ -9,10 +9,8 @@ /** * Created by Ramona Harrison * on 8/26/16 - * * A class representing a message attachment. * See https://api.slack.com/docs/message-attachments - * */ public class Attachment { @@ -34,8 +32,10 @@ public class Attachment { // "footer_icon" // "ts" + List actions; private String fallback; private String color; + private String callback_id; private String pretext; private String author_name; private String author_link; @@ -52,7 +52,6 @@ public class Attachment { public Attachment(JSONObject json) { - // parse a user from the incoming json if (json.containsKey("fallback")) { @@ -111,7 +110,7 @@ public Attachment(JSONObject json) { 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)); + Field field = new Field((JSONObject) jsonFields.get(i)); this.fields.add(field); } } @@ -119,6 +118,15 @@ public Attachment(JSONObject json) { if (json.containsKey("ts")) { this.ts = (String) json.get("ts"); } + + if (json.containsKey("actions")) { + JSONArray jsonActions = (JSONArray) json.get("actions"); + this.actions = new ArrayList(); + for (int i = 0; i < jsonActions.size(); i++) { + Action action = new Action((JSONObject) jsonActions.get(i)); + this.actions.add(action); + } + } } // getters to access private fields diff --git a/src/nyc/c4q/ramonaharrison/model/Channel.java b/src/nyc/c4q/ramonaharrison/model/Channel.java index baf5c83..02c3848 100644 --- a/src/nyc/c4q/ramonaharrison/model/Channel.java +++ b/src/nyc/c4q/ramonaharrison/model/Channel.java @@ -9,10 +9,8 @@ /** * Created by Ramona Harrison * on 8/26/16 - * * A class representing a channel object, which contains information about a team channel. * See https://api.slack.com/types/channel - * */ public class Channel { diff --git a/src/nyc/c4q/ramonaharrison/model/Confirmation.java b/src/nyc/c4q/ramonaharrison/model/Confirmation.java new file mode 100644 index 0000000..f825e3b --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Confirmation.java @@ -0,0 +1,7 @@ +package nyc.c4q.ramonaharrison.model; + +/** + * Created by CATWONG on 9/19/16. + */ +public class Confirmation { +} diff --git a/src/nyc/c4q/ramonaharrison/model/Field.java b/src/nyc/c4q/ramonaharrison/model/Field.java index 6cae253..f502e82 100644 --- a/src/nyc/c4q/ramonaharrison/model/Field.java +++ b/src/nyc/c4q/ramonaharrison/model/Field.java @@ -1,6 +1,5 @@ package nyc.c4q.ramonaharrison.model; -import org.json.simple.JSONArray; import org.json.simple.JSONObject; public class Field { diff --git a/src/nyc/c4q/ramonaharrison/model/GiphyData.java b/src/nyc/c4q/ramonaharrison/model/GiphyData.java index 8c4a9ec..be00029 100644 --- a/src/nyc/c4q/ramonaharrison/model/GiphyData.java +++ b/src/nyc/c4q/ramonaharrison/model/GiphyData.java @@ -2,39 +2,38 @@ 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" - } - } + * { + * "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 GiphyData { @@ -64,7 +63,7 @@ public class GiphyData { public GiphyData(JSONObject json) { // parse data from the incoming json - if (json.containsKey("type")){ + if (json.containsKey("type")) { this.type = (String) json.get("type"); } @@ -243,6 +242,4 @@ public String getFixed_width_small_width() { public String getFixed_width_small_height() { return fixed_width_small_height; } - - } diff --git a/src/nyc/c4q/ramonaharrison/model/GiphyMeta.java b/src/nyc/c4q/ramonaharrison/model/GiphyMeta.java index f03cf9d..749ebf5 100644 --- a/src/nyc/c4q/ramonaharrison/model/GiphyMeta.java +++ b/src/nyc/c4q/ramonaharrison/model/GiphyMeta.java @@ -1,16 +1,12 @@ package nyc.c4q.ramonaharrison.model; -import org.json.simple.JSONArray; import org.json.simple.JSONObject; -import java.util.ArrayList; -import java.util.List; - /** - "meta": { - "status": 200, - "msg": "OK" - } + * "meta": { + * "status": 200, + * "msg": "OK" + * } */ public class GiphyMeta { @@ -22,11 +18,11 @@ public class GiphyMeta { public GiphyMeta(JSONObject json) { - if(json.containsKey("status")) { + if (json.containsKey("status")) { this.status = (Integer) json.get("status"); } - if(json.containsKey("msg")) { + if (json.containsKey("msg")) { this.msg = (String) json.get("msg"); } } diff --git a/src/nyc/c4q/ramonaharrison/model/Message.java b/src/nyc/c4q/ramonaharrison/model/Message.java index f85ad13..708c8fe 100644 --- a/src/nyc/c4q/ramonaharrison/model/Message.java +++ b/src/nyc/c4q/ramonaharrison/model/Message.java @@ -9,31 +9,32 @@ /** * Created by Ramona Harrison * on 8/26/16 - * + *

* A class representing a message. * See https://api.slack.com/docs/message-formatting - * + *

* Example JSON: - * - * { - * "text": "I am a test message http://slack.com", - * "ts": "1358546515.000008", - * "user": "U2147483896", - * "attachments": [ - * { - * "text": "And here's an attachment!" - * } - * ] - * } - * + *

+ * { + * "text": "I am a test message http://slack.com", + * "ts": "1358546515.000008", + * "user": "U2147483896", + * "attachments": [ + * { + * "text": "And here's an attachment!" + * } + * ] + * } */ public class Message { + List attachments; private String text; private String ts; + private String name; private String user; - List attachments; + private String response_type; public Message(JSONObject json) { if (json.get("text") != null) { @@ -48,6 +49,14 @@ public Message(JSONObject json) { this.user = (String) json.get("user"); } + if (json.get("name") != null) { + this.name = (String) json.get("name"); + } + + if (json.get("response_type") != null) { + this.response_type = (String) json.get("response_type"); + } + if (json.get("attachments") != null) { JSONArray attachmentsArray = (JSONArray) json.get("attachments"); this.attachments = new ArrayList(); @@ -55,7 +64,6 @@ public Message(JSONObject json) { this.attachments.add(new Attachment((JSONObject) attachmentsArray.get(i))); } } - } public String getText() { @@ -66,10 +74,18 @@ public String getTs() { return ts; } + public String getName() { + return name; + } + public String getUser() { return user; } + public String getResponse_type() { + return response_type; + } + public List getAttachments() { return attachments; } diff --git a/src/nyc/c4q/ramonaharrison/model/Profile.java b/src/nyc/c4q/ramonaharrison/model/Profile.java index e55a943..d4a8c44 100644 --- a/src/nyc/c4q/ramonaharrison/model/Profile.java +++ b/src/nyc/c4q/ramonaharrison/model/Profile.java @@ -1,6 +1,5 @@ package nyc.c4q.ramonaharrison.model; -import org.json.simple.JSONArray; import org.json.simple.JSONObject; public class Profile { @@ -18,7 +17,7 @@ public class Profile { private String image192; private String image512; - public Profile (JSONObject json){ + public Profile(JSONObject json) { if (json.containsKey("firstName")) { this.firstName = (String) json.get("firstName"); } @@ -65,6 +64,5 @@ public Profile (JSONObject json){ if (json.containsKey("image512")) { this.image512 = (String) json.get("image512"); } - } } diff --git a/src/nyc/c4q/ramonaharrison/model/Purpose.java b/src/nyc/c4q/ramonaharrison/model/Purpose.java index 42b1828..4c2ad7d 100644 --- a/src/nyc/c4q/ramonaharrison/model/Purpose.java +++ b/src/nyc/c4q/ramonaharrison/model/Purpose.java @@ -5,18 +5,17 @@ /** * Created by Ramona Harrison * on 8/26/16 - * + *

* A class representing a channel purpose. * See https://api.slack.com/types/channel - * + *

* Example JSON: - * - * { - * "value": "This channel is for fun", - * "creator": "U024BE7LV", - * "last_set": 1369677212 - * } - * + *

+ * { + * "value": "This channel is for fun", + * "creator": "U024BE7LV", + * "last_set": 1369677212 + * } */ public class Purpose { diff --git a/src/nyc/c4q/ramonaharrison/model/Topic.java b/src/nyc/c4q/ramonaharrison/model/Topic.java index fd2cee9..5cda780 100644 --- a/src/nyc/c4q/ramonaharrison/model/Topic.java +++ b/src/nyc/c4q/ramonaharrison/model/Topic.java @@ -5,18 +5,17 @@ /** * Created by Ramona Harrison * on 8/26/16 - * + *

* A class representing a channel topic. * See https://api.slack.com/types/channel - * + *

* Example JSON: - * - * { - * "value": "Fun times", - * "creator": "U024BE7LV", - * "last_set": 1369677212 - * } - * + *

+ * { + * "value": "Fun times", + * "creator": "U024BE7LV", + * "last_set": 1369677212 + * } */ public class Topic { diff --git a/src/nyc/c4q/ramonaharrison/model/User.java b/src/nyc/c4q/ramonaharrison/model/User.java index 2d9b7c0..e16204b 100644 --- a/src/nyc/c4q/ramonaharrison/model/User.java +++ b/src/nyc/c4q/ramonaharrison/model/User.java @@ -9,10 +9,9 @@ /** * Created by Ramona Harrison * on 8/26/16 - * + *

* A class representing a user. * See https://api.slack.com/types/user - * */ public class User { @@ -64,9 +63,9 @@ public User(JSONObject json) { if (json.containsKey("userProfile")) { JSONArray jsonUserProfile = (JSONArray) json.get("userProfile"); this.userProfile = new ArrayList(); - for (int i = 0; i < jsonUserProfile.size(); i++) { + for (int i = 0; i < jsonUserProfile.size(); i++) { Profile profile = new Profile((JSONObject) jsonUserProfile.get(i)); - this.userProfile.add(profile); + this.userProfile.add(profile); } } diff --git a/src/nyc/c4q/ramonaharrison/network/Giphy.java b/src/nyc/c4q/ramonaharrison/network/Giphy.java index a99bc98..522a9be 100644 --- a/src/nyc/c4q/ramonaharrison/network/Giphy.java +++ b/src/nyc/c4q/ramonaharrison/network/Giphy.java @@ -1,17 +1,12 @@ package nyc.c4q.ramonaharrison.network; -import java.net.URL; -import java.util.List; -import java.util.Random; -import java.util.Scanner; -import nyc.c4q.ramonaharrison.model.GiphyData; -import nyc.c4q.ramonaharrison.model.GiphyMeta; import nyc.c4q.ramonaharrison.model.Message; -import nyc.c4q.ramonaharrison.network.response.GiphyResponse; import nyc.c4q.ramonaharrison.network.response.ListMessagesResponse; -import org.json.simple.JSONArray; import org.json.simple.JSONObject; +import java.net.URL; +import java.util.List; + public class Giphy { @@ -21,11 +16,10 @@ public class Giphy { private static final String BASE_URL = "https://api.giphy.com/"; private static final String ENDPOINT_TEST = "v1/gifs/"; private static final String API = "dc6zaTOxFJmzC"; - private static final String UNFURL_MEDIA = "true"; public static String giphySearch() { - ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ABKERFT"); + ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ERNHJ5D"); List messages = listMessagesResponse.getMessages(); for (Message message : messages) { @@ -39,15 +33,15 @@ public static String giphySearch() { query = query.substring(builder.indexOf("@U2ADRJVK9") + 18); URL giphyURL = HTTPS.stringToURL(BASE_URL + ENDPOINT_TEST + "random?" + "api_key=" + API + "&tag=" + query); - System.out.println(giphyURL); + System.out.println(giphyURL); JSONObject giphyJson = HTTPS.get(giphyURL); if (giphyJson.containsKey("data")) { JSONObject myObj = (JSONObject) giphyJson.get("data"); - String giphyString = myObj.get("fixed_height_downsampled_url").toString(); - System.out.println(giphyString); + String giphyString = myObj.get("url").toString(); + System.out.println(giphyString); return giphyString; } else return null; @@ -55,7 +49,3 @@ public static String giphySearch() { return " "; } } - -// Scanner input = new Scanner(System.in); -// System.out.println("enter a word: "); -// String query = input.next(); \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/network/HTTPS.java b/src/nyc/c4q/ramonaharrison/network/HTTPS.java index 7da0c8d..da69d31 100644 --- a/src/nyc/c4q/ramonaharrison/network/HTTPS.java +++ b/src/nyc/c4q/ramonaharrison/network/HTTPS.java @@ -59,7 +59,7 @@ public static URL stringToURL(String string) { */ public static JSONObject get(URL url) { try { - final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); final InputStreamReader reader = new InputStreamReader(connection.getInputStream()); try { return stringToJSON(readAll(reader)); diff --git a/src/nyc/c4q/ramonaharrison/network/Memes.java b/src/nyc/c4q/ramonaharrison/network/Memes.java index 1dbd772..c565c05 100644 --- a/src/nyc/c4q/ramonaharrison/network/Memes.java +++ b/src/nyc/c4q/ramonaharrison/network/Memes.java @@ -1,24 +1,20 @@ package nyc.c4q.ramonaharrison.network; import java.net.URL; -import java.util.Scanner; import java.util.Random; /** - http://www.memes.com/img/13 - http://www.memes.com/img/1190510 + * http://www.memes.com/img/13 + * http://www.memes.com/img/1190510 */ public class Memes { private static final String BASE_URL = "http://www.memes.com/"; private static final String ENDPOINT_TEST = "img/"; - private static final String UNFURL_LINK = "true"; - private static final String UNFURL_MEDIA = "true"; - public static URL memeSearch() { + public static URL memeSearch() { Random number = new Random(); int query = number.nextInt(1190510) + 13; - - return HTTPS.stringToURL(BASE_URL + ENDPOINT_TEST + query); + return HTTPS.stringToURL(BASE_URL + ENDPOINT_TEST + query); } } diff --git a/src/nyc/c4q/ramonaharrison/network/Slack.java b/src/nyc/c4q/ramonaharrison/network/Slack.java index 17a08d9..d5483d2 100644 --- a/src/nyc/c4q/ramonaharrison/network/Slack.java +++ b/src/nyc/c4q/ramonaharrison/network/Slack.java @@ -14,15 +14,14 @@ /** * Created by Ramona Harrison * on 8/26/16 - * + *

* A class for interacting with Slack's Web API. - * */ public class Slack { + public static final String BOTS_CHANNEL_ID = "C2ERNHJ5D"; private static final String API_KEY = Token.findApiToken(); - private static final String CAT_API_KEY = "xoxp-71191384642-71441371842-79184766036-f9ac6a4d4f"; private static final String BASE_URL = "https://slack.com/api/"; private static final String ENDPOINT_TEST = "api.test"; private static final String ENDPOINT_LIST_CHANNELS = "channels.list"; @@ -33,11 +32,9 @@ public class Slack { private static final String USERS_INFO = "users.info"; private static final String UNFURL_LINK = "true"; private static final String UNFURL_MEDIA = "true"; - private static final String USERNAME = "messybot"; private static final String AS_USER = "false"; private static final String IMAGE_URL = "https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3osxYpRxjzvPOtwfF6%2Fgiphy.gif"; - public static final String BOTS_CHANNEL_ID = "C2ABKERFT"; public static String getUSERNAME() { return USERNAME; @@ -49,7 +46,7 @@ public static String getUSERNAME() { * @return the Response indicating ok/error or null if the connection failed. */ public static Response testApi() { - URL testUrl = HTTPS.stringToURL(BASE_URL + ENDPOINT_TEST + "?token=" + API_KEY); + URL testUrl = HTTPS.stringToURL(BASE_URL + ENDPOINT_TEST + "?token=" + API_KEY); JSONObject object = HTTPS.get(testUrl); @@ -71,7 +68,7 @@ public static ListChannelsResponse listChannels() { /** * Static method to list the last 100 message on a given channel. * - * @param channelId the id of the channel from which to list messages. + * @param channelId the id of the channel from which to list messages. * @return the ListMessagesResponse indicating ok/error or null if the connection failed. */ public static ListMessagesResponse listMessages(String channelId) { @@ -81,17 +78,10 @@ public static ListMessagesResponse listMessages(String channelId) { return new ListMessagesResponse(HTTPS.get(listMessagesUrl)); } - public static SearchMessagesResponse searchMessages(String channelId, String query) { - - URL querySearch = HTTPS.stringToURL(BASE_URL + ENDPOINT_SEARCH_MESSAGE + "?token=" + CAT_API_KEY + "&channel=" + channelId + "&query=" + query + "&pretty=1"); - - return new SearchMessagesResponse(HTTPS.get(querySearch)); - } - /** * Static method to send a message to the #bots channel. * - * @param messageText the message text. + * @param messageText the message text. * @return the SendMessageResponse indicating ok/error or null if the connection failed. */ public static SendMessageResponse sendMessage(String messageText) { @@ -110,20 +100,22 @@ public static SendMessageResponse sendMessage(String messageText) { /** * Static method to send a message with one or more attachments to the #bots channel. * - * @param messageText the message text. - * @param attachments a list of one of more attachments to be parsed to a JSON-encoded URL string parameter. + * @param messageText the message text. + * @param attachments a list of one of more attachments to be parsed to a JSON-encoded URL string parameter. * @return the SendMessageResponse indicating ok/error or null if the connection failed. */ 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!"); + +// https://slack.com/api/chat.postMessage?token=xoxp-71191384642-71441371842-79184766036-f9ac6a4d4f&channel=C2ADPS5MK&text=What%20would%20you%20like%20%40messybot%20to%20look%20up%3F&attachments=%5B%20%20%20%20%20%20%20%20%20%7B%20%09%09%09%22text%22%3A%20%22%3Apoop%3A%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%22fallback%22%3A%20%22That%20is%20not%20a%20word%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%22callback_id%22%3A%20%22messybot%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%22color%22%3A%20%22%233AA3E3%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%22attachment_type%22%3A%20%22default%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%22actions%22%3A%20%5B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Urban%20Dictionary%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Urban%20Dictionary%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22type%22%3A%20%22button%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22value%22%3A%20%22Urban%20Dictionary%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Meme%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Meme%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22type%22%3A%20%22button%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22value%22%3A%20%22Meme%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Giphy%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Giphy%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22type%22%3A%20%22button%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22value%22%3A%20%22Giphy%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22confirm%22%3A%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Are%20you%20sure%3F%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Let%27s%20find%20it!%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22ok_text%22%3A%20%22Yes%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22dismiss_text%22%3A%20%22No%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%5D%20%7D&unfurl_links=true&unfurl_media=true&username=messybot&as_user=false&icon_url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3osxYpRxjzvPOtwfF6%2Fgiphy.gif&pretty=1 } /** * Static method to delete an existing message from the #bots channel. * - * @param messageTs the message timestamp. + * @param messageTs the message timestamp. * @return the DeleteMessageResponse indicating ok/error or null if the connection failed. */ public static DeleteMessageResponse deleteMessage(String messageTs) { @@ -135,7 +127,7 @@ public static DeleteMessageResponse deleteMessage(String messageTs) { public static Response addReaction() { String emojiName = "hankey"; - ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ABKERFT"); + ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ERNHJ5D"); List messages = listMessagesResponse.getMessages(); for (Message message : messages) { @@ -148,21 +140,27 @@ public static Response addReaction() { return addReaction(); } -// public static String userInfo() { -// URL username = HTTPS.stringToURL(BASE_URL + USERS_INFO + "?token=" + API_KEY + "&user=" + userId + "&pretty=1"); -// -// JSONObject json = HTTPS.get(username); -// -// if (json.containsKey("data")) { -// -// JSONObject myObj = (JSONObject) json.get("data"); -// String userId = (String) myObj.get("name"); -// System.out.println(userId); -// -// return userId; -// } -// return null; -// } + public static String userInfo() { + ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ERNHJ5D"); + List messages = listMessagesResponse.getMessages(); + + for (Message message : messages) { + String name = message.getUser(); + + URL username = HTTPS.stringToURL(BASE_URL + USERS_INFO + "?token=" + API_KEY + "&user=" + name + "&pretty=1"); + JSONObject json = HTTPS.get(username); + + if (json.containsKey("user")) { + + JSONObject myObj = (JSONObject) json.get("user"); + name = (String) myObj.get("name"); + System.out.println(name); + + return name; + } + } + return null; + } } diff --git a/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java b/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java index 9153ec8..d3cb90a 100644 --- a/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java +++ b/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java @@ -1,49 +1,34 @@ package nyc.c4q.ramonaharrison.network; -import com.sun.org.apache.regexp.internal.RE; -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.JSONObject; +import nyc.c4q.ramonaharrison.network.response.ListMessagesResponse; -import java.io.UnsupportedEncodingException; import java.net.URL; -import java.net.URLEncoder; import java.util.List; -import java.util.Scanner; - public class UrbanDictionary { private static final String BASE_URL = "http://www.urbandictionary.com/"; private static final String ENDPOINT_TEST = "define.php"; - private static final String TERM = "?term="; private static final String UNFURL_LINK = "true"; - private static final String UNFURL_MEDIA = "true"; public static URL wordSearch() { - ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ADPS5MK"); + ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ERNHJ5D"); List messages = listMessagesResponse.getMessages(); for (Message message : messages) { - String query = messages.get(0).getText();; + String query = messages.get(0).getText(); + ; String substr = "+", regex = "\\s"; - query = query.replaceAll(regex,substr); + query = query.replaceAll(regex, substr); StringBuilder builder = new StringBuilder(); builder.append(query); query = query.substring(builder.indexOf("@U2ADRJVK9") + 15); -// query = query.substring(builder.lastIndexOf("@U2ADRJVK9 ud") + 5); - return HTTPS.stringToURL(BASE_URL + ENDPOINT_TEST + "?term=" + query + "&unfurl_links=" + UNFURL_LINK); } return wordSearch(); } } - -// Scanner input = new Scanner(System.in); -// System.out.println("enter a word: "); -// query = input.next(); \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/network/response/DeleteMessageResponse.java b/src/nyc/c4q/ramonaharrison/network/response/DeleteMessageResponse.java index 6bb60d6..ad71a4b 100644 --- a/src/nyc/c4q/ramonaharrison/network/response/DeleteMessageResponse.java +++ b/src/nyc/c4q/ramonaharrison/network/response/DeleteMessageResponse.java @@ -5,10 +5,9 @@ /** * Created by Ramona Harrison * on 9/10/16 - * + *

* A class representing the response from chat.delete * See https://api.slack.com/methods/chat.delete - * */ public class DeleteMessageResponse extends Response { diff --git a/src/nyc/c4q/ramonaharrison/network/response/GiphyResponse.java b/src/nyc/c4q/ramonaharrison/network/response/GiphyResponse.java index d9b50a7..8a188ae 100644 --- a/src/nyc/c4q/ramonaharrison/network/response/GiphyResponse.java +++ b/src/nyc/c4q/ramonaharrison/network/response/GiphyResponse.java @@ -1,18 +1,12 @@ package nyc.c4q.ramonaharrison.network.response; -import nyc.c4q.ramonaharrison.model.*; -import nyc.c4q.ramonaharrison.network.Slack; -import org.json.simple.JSONArray; import org.json.simple.JSONObject; -import java.util.ArrayList; -import java.util.List; - /** * Created by CATWONG on 9/17/16. */ -public class GiphyResponse{ +public class GiphyResponse { private String data; private String url; @@ -29,7 +23,7 @@ public GiphyResponse(JSONObject json) { this.url = (String) json.get("url"); } - if (json.get("fixed_height_downsampled_url") != null){ + if (json.get("fixed_height_downsampled_url") != null) { this.fixed_height_downsampled_url = (String) json.get("url"); } } @@ -37,9 +31,11 @@ public GiphyResponse(JSONObject json) { public String getData() { return data; } + public String getUrl() { return url; } + public String getFixed_height_downsampled_url() { return fixed_height_downsampled_url; } diff --git a/src/nyc/c4q/ramonaharrison/network/response/ListChannelsResponse.java b/src/nyc/c4q/ramonaharrison/network/response/ListChannelsResponse.java index e6946fe..19ec4b9 100644 --- a/src/nyc/c4q/ramonaharrison/network/response/ListChannelsResponse.java +++ b/src/nyc/c4q/ramonaharrison/network/response/ListChannelsResponse.java @@ -10,10 +10,9 @@ /** * Created by Ramona Harrison * on 8/26/16 - * + *

* A class representing the response from channels.list * See https://api.slack.com/methods/channels.list - * */ public class ListChannelsResponse extends Response { diff --git a/src/nyc/c4q/ramonaharrison/network/response/ListMessagesResponse.java b/src/nyc/c4q/ramonaharrison/network/response/ListMessagesResponse.java index 81e5df9..fea64dd 100644 --- a/src/nyc/c4q/ramonaharrison/network/response/ListMessagesResponse.java +++ b/src/nyc/c4q/ramonaharrison/network/response/ListMessagesResponse.java @@ -10,10 +10,9 @@ /** * Created by Ramona Harrison * on 8/26/16 - * + *

* A class representing the response from channels.history * See https://api.slack.com/methods/channels.history - * */ public class ListMessagesResponse extends Response { @@ -27,7 +26,7 @@ public ListMessagesResponse(JSONObject json) { if (json.containsKey("messages")) { JSONArray array = (JSONArray) json.get("messages"); - if (json.containsKey("user")){ + if (json.containsKey("user")) { this.user = (String) json.get("user"); } diff --git a/src/nyc/c4q/ramonaharrison/network/response/Response.java b/src/nyc/c4q/ramonaharrison/network/response/Response.java index 1a9336a..9a5967b 100644 --- a/src/nyc/c4q/ramonaharrison/network/response/Response.java +++ b/src/nyc/c4q/ramonaharrison/network/response/Response.java @@ -2,16 +2,12 @@ import org.json.simple.JSONObject; -import java.net.MalformedURLException; -import java.net.URL; - /** * Created by Ramona Harrison * on 8/26/16 - * + *

* A class representing a response from Slack's Web API. * See https://api.slack.com/methods/api.test - * */ public class Response { diff --git a/src/nyc/c4q/ramonaharrison/network/response/SearchMessagesResponse.java b/src/nyc/c4q/ramonaharrison/network/response/SearchMessagesResponse.java index 1614313..7c63cc6 100644 --- a/src/nyc/c4q/ramonaharrison/network/response/SearchMessagesResponse.java +++ b/src/nyc/c4q/ramonaharrison/network/response/SearchMessagesResponse.java @@ -19,7 +19,7 @@ public SearchMessagesResponse(JSONObject json) { if (json.containsKey("messages")) { JSONArray array = (JSONArray) json.get("messages"); - if (json.containsKey("user")){ + if (json.containsKey("user")) { this.user = (String) json.get("user"); } diff --git a/src/nyc/c4q/ramonaharrison/network/response/SendMessageResponse.java b/src/nyc/c4q/ramonaharrison/network/response/SendMessageResponse.java index e183efd..8b2165a 100644 --- a/src/nyc/c4q/ramonaharrison/network/response/SendMessageResponse.java +++ b/src/nyc/c4q/ramonaharrison/network/response/SendMessageResponse.java @@ -6,10 +6,9 @@ /** * Created by Ramona Harrison * on 8/26/16 - * + *

* A class representing the response from chat.postMessage * See https://api.slack.com/methods/chat.postMessage - * */ public class SendMessageResponse extends Response { diff --git a/src/nyc/c4q/ramonaharrison/network/response/UserInfoResponse.java b/src/nyc/c4q/ramonaharrison/network/response/UserInfoResponse.java deleted file mode 100644 index 17ecde9..0000000 --- a/src/nyc/c4q/ramonaharrison/network/response/UserInfoResponse.java +++ /dev/null @@ -1,38 +0,0 @@ -package nyc.c4q.ramonaharrison.network.response; - -import org.json.simple.JSONObject; -import nyc.c4q.ramonaharrison.model.User; - - -public class UserInfoResponse extends Response { - - private User user; - private User name; - - public UserInfoResponse(JSONObject json) { - super(json); - - if (json.containsKey("user")) { - this.user = new User((JSONObject) json.get("user")); - } - - if (json.containsKey("name")) { - this.name = new User((JSONObject) json.get("name")); - } - } - - public User getUser() { - return user; - } - - public User getName() { - return name; - } -} - - - - - - - diff --git a/src/nyc/c4q/ramonaharrison/util/Token.java b/src/nyc/c4q/ramonaharrison/util/Token.java index 3f0c788..c8add72 100644 --- a/src/nyc/c4q/ramonaharrison/util/Token.java +++ b/src/nyc/c4q/ramonaharrison/util/Token.java @@ -7,7 +7,7 @@ /** * Created by Ramona Harrison * on 8/24/16 - * + *

* A utility class for retrieving the API token. */