diff --git a/src/nyc/c4q/ramonaharrison/Bot.java b/src/nyc/c4q/ramonaharrison/Bot.java index 6a52dd4..88dfe97 100644 --- a/src/nyc/c4q/ramonaharrison/Bot.java +++ b/src/nyc/c4q/ramonaharrison/Bot.java @@ -2,30 +2,187 @@ import nyc.c4q.ramonaharrison.model.Channel; import nyc.c4q.ramonaharrison.model.Message; -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 java.net.URL; import java.util.List; -/** - * Created by Ramona Harrison - * on 8/26/16 - * - */ - public class Bot { - // TODO: implement your bot logic! + + Slack myBot; public Bot() { } + public void addReaction() { + Response reaction = Slack.addReaction(); + System.out.println(reaction); + } + + /** + * Sample method: prints up to the last 100 messages from a given channel. Prints an error message on failure. + * or failure. + * + * @param channelId id of the given channel. + */ + public void listMessages(String channelId) { + ListMessagesResponse listMessagesResponse = Slack.listMessages(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()); + } + } + + public void respondGreeting(String channelId) { + + ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); + + if (listMessagesResponse.isOk()) { + + List messages = listMessagesResponse.getMessages(); + + myBot = new Slack(); + + 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) { + ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); + + if (listMessagesResponse.isOk()) { + List messages = listMessagesResponse.getMessages(); + + myBot = new Slack(); + UrbanDictionary urbanDictionary = new UrbanDictionary(); + Memes memes = new Memes(); + Giphy giphy = new Giphy(); + + for (Message message : messages) { + String word = message.getText(); + + if (word.contains("give me")) { + + URL slangSearch = urbanDictionary.wordSearch(); + slangSearch.toString(); + + URL memeSearch = memes.memeSearch(); + memeSearch.toString(); + + 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()); + } + } + + public void respondDictionary(String channelId) { + ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); + + if (listMessagesResponse.isOk()) { + List messages = listMessagesResponse.getMessages(); + + myBot = new Slack(); + UrbanDictionary urbanDictionary = new UrbanDictionary(); + + for (Message message : messages) { + String word = message.getText(); + + if (word.contains("ud")) { + 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(); + + myBot = new Slack(); + Memes memes = new Memes(); + + for (Message message : messages) { + + String aMeme = message.getText(); + if (aMeme.contains("meme")) { + 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(); + + myBot = new Slack(); + Giphy giphy = new Giphy(); + + for (Message message : messages) { + String aGiphy = message.getText(); + + if (aGiphy.contains("giphy")) { + String giphySearch = giphy.giphySearch(); + 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"); + System.out.println("API OK: " + apiTest.isOk() + "\n"); } /** @@ -46,29 +203,6 @@ public void listChannels() { } } - /** - * Sample method: prints up to the last 100 messages from a given channel. Prints an error message on failure. - * or failure. - * - * @param channelId id of the given channel. - */ - public void listMessages(String channelId) { - ListMessagesResponse listMessagesResponse = Slack.listMessages(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()); - } - } 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. * @@ -98,4 +232,5 @@ public void deleteMessageInBotsChannel(String messageTs) { 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 933e92b..01ceee0 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -4,21 +4,31 @@ public class Main { + // 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(); - myBot.testApi(); +// myBot.testApi(); +// myBot.deleteMessageInBotsChannel("1474042536.000733"); myBot.listChannels(); - myBot.listMessages(Slack.BOTS_CHANNEL_ID); - // Post "Hello, world!" to the #bots channel - //myBot.sendMessage("Hello, world!"); +// myBot.addReaction(); + myBot.respondGreeting("C2ERNHJ5D"); + myBot.respondDictionary("C2ERNHJ5D"); + myBot.respondMeme("C2ERNHJ5D"); + myBot.respondGiphy("C2ERNHJ5D"); + +// myBot.listMessages(Slack.BOTS_CHANNEL_ID); +// 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"); +// 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 8b9d1ef..cfba741 100644 --- a/src/nyc/c4q/ramonaharrison/model/Attachment.java +++ b/src/nyc/c4q/ramonaharrison/model/Attachment.java @@ -1,19 +1,21 @@ 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 - * * A class representing a message attachment. * See https://api.slack.com/docs/message-attachments - * */ 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" @@ -30,10 +32,162 @@ 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; + 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"); + } + + 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 + + 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/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 new file mode 100644 index 0000000..f502e82 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Field.java @@ -0,0 +1,24 @@ +package nyc.c4q.ramonaharrison.model; + +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..be00029 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/GiphyData.java @@ -0,0 +1,245 @@ +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..749ebf5 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/GiphyMeta.java @@ -0,0 +1,39 @@ +package nyc.c4q.ramonaharrison.model; + +import org.json.simple.JSONObject; + +/** + * "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..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,11 +74,20 @@ 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 new file mode 100644 index 0000000..d4a8c44 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Profile.java @@ -0,0 +1,68 @@ +package nyc.c4q.ramonaharrison.model; + +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/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 bc309eb..e16204b 100644 --- a/src/nyc/c4q/ramonaharrison/model/User.java +++ b/src/nyc/c4q/ramonaharrison/model/User.java @@ -1,19 +1,22 @@ 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 - * + *

* A class representing a user. * See https://api.slack.com/types/user - * */ 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" @@ -28,9 +31,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..522a9be --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/network/Giphy.java @@ -0,0 +1,51 @@ +package nyc.c4q.ramonaharrison.network; + +import nyc.c4q.ramonaharrison.model.Message; +import nyc.c4q.ramonaharrison.network.response.ListMessagesResponse; +import org.json.simple.JSONObject; + +import java.net.URL; +import java.util.List; + + +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"; + + public static String giphySearch() { + + ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ERNHJ5D"); + List messages = listMessagesResponse.getMessages(); + + for (Message message : messages) { + String query = message.getText(); + String substr = "+", regex = "\\s"; + query = query.replaceAll(regex, substr); + + StringBuilder builder = new StringBuilder(); + builder.append(query); + + 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); + + JSONObject giphyJson = HTTPS.get(giphyURL); + + if (giphyJson.containsKey("data")) { + + JSONObject myObj = (JSONObject) giphyJson.get("data"); + String giphyString = myObj.get("url").toString(); + System.out.println(giphyString); + return giphyString; + } else + return null; + } + return " "; + } +} 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 new file mode 100644 index 0000000..c565c05 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/network/Memes.java @@ -0,0 +1,20 @@ +package nyc.c4q.ramonaharrison.network; + +import java.net.URL; +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/"; + + 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..d5483d2 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; @@ -13,22 +14,31 @@ /** * 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 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"; - - public static final String BOTS_CHANNEL_ID = "C2ABKERFT"; + 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 String getUSERNAME() { + return USERNAME; + } /** * Static method to test the Slack API. @@ -36,7 +46,7 @@ public class Slack { * @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); @@ -58,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) { @@ -71,7 +81,7 @@ public static ListMessagesResponse listMessages(String channelId) { /** * 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) { @@ -82,7 +92,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)); } @@ -90,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) { @@ -111,4 +123,44 @@ public static DeleteMessageResponse deleteMessage(String messageTs) { return new DeleteMessageResponse(HTTPS.get(deleteMessageUrl)); } + + public static Response addReaction() { + String emojiName = "hankey"; + + ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ERNHJ5D"); + 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() { + 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 new file mode 100644 index 0000000..d3cb90a --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/network/UrbanDictionary.java @@ -0,0 +1,34 @@ +package nyc.c4q.ramonaharrison.network; + +import nyc.c4q.ramonaharrison.model.Message; +import nyc.c4q.ramonaharrison.network.response.ListMessagesResponse; + +import java.net.URL; +import java.util.List; + +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 UNFURL_LINK = "true"; + + public static URL wordSearch() { + ListMessagesResponse listMessagesResponse = Slack.listMessages("C2ERNHJ5D"); + List messages = listMessagesResponse.getMessages(); + + for (Message message : messages) { + 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") + 15); + + return HTTPS.stringToURL(BASE_URL + ENDPOINT_TEST + "?term=" + query + "&unfurl_links=" + UNFURL_LINK); + } + return wordSearch(); + } +} 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 new file mode 100644 index 0000000..8a188ae --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/network/response/GiphyResponse.java @@ -0,0 +1,44 @@ +package nyc.c4q.ramonaharrison.network.response; + +import org.json.simple.JSONObject; + +/** + * 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/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 7edaca0..fea64dd 100644 --- a/src/nyc/c4q/ramonaharrison/network/response/ListMessagesResponse.java +++ b/src/nyc/c4q/ramonaharrison/network/response/ListMessagesResponse.java @@ -10,15 +10,15 @@ /** * 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 { private List messages; + private String user; public ListMessagesResponse(JSONObject json) { super(json); @@ -26,9 +26,14 @@ 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 +41,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..9a5967b 100644 --- a/src/nyc/c4q/ramonaharrison/network/response/Response.java +++ b/src/nyc/c4q/ramonaharrison/network/response/Response.java @@ -5,10 +5,9 @@ /** * 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 { @@ -33,4 +32,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..7c63cc6 --- /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/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/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. */