diff --git a/src/nyc/c4q/ramonaharrison/Bot.java b/src/nyc/c4q/ramonaharrison/Bot.java index 6a52dd4..6e51188 100644 --- a/src/nyc/c4q/ramonaharrison/Bot.java +++ b/src/nyc/c4q/ramonaharrison/Bot.java @@ -1,11 +1,14 @@ package nyc.c4q.ramonaharrison; +import nyc.c4q.ramonaharrison.model.Attachment; import nyc.c4q.ramonaharrison.model.Channel; import nyc.c4q.ramonaharrison.model.Message; import nyc.c4q.ramonaharrison.network.*; import nyc.c4q.ramonaharrison.network.response.*; +import java.util.ArrayList; import java.util.List; +import java.util.Random; /** * Created by Ramona Harrison @@ -16,6 +19,8 @@ public class Bot { // TODO: implement your bot logic! + ArrayList inspirationQuotes = new ArrayList<>(); + public Bot() { } @@ -25,7 +30,7 @@ public Bot() { */ public void testApi() { Response apiTest = Slack.testApi(); - System.out.println("API OK: " +apiTest.isOk() + "\n"); + System.out.println("API OK: " + apiTest.isOk() + "\n"); } /** @@ -55,6 +60,8 @@ public void listChannels() { public void listMessages(String channelId) { ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); + Bot myBot = new Bot(); + if (listMessagesResponse.isOk()) { List messages = listMessagesResponse.getMessages(); @@ -63,12 +70,44 @@ public void listMessages(String channelId) { System.out.println(); System.out.println("Timestamp: " + message.getTs()); System.out.println("Message: " + message.getText()); + + String saySomething = message.getText(); + + if (saySomething.contains("candy") || saySomething.contains("@U7JJ7B7K4")) { + myBot.sendMessageToBotsChannel("Random Quote: \n" + randomQuote()); + break; + + + } + + } } else { System.err.print("Error listing messages: " + listMessagesResponse.getError()); } } + public void respond(String channelId) { + ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); + + if (listMessagesResponse.isOk()) { + List messages = listMessagesResponse.getMessages(); + + Bot myBot = new Bot(); + + for (Message message : messages) { + + String saySomething = message.getText(); + + if (saySomething.contains("@U7JJ7B7K4") || saySomething.contains("candy")) { +// myBot.sendMessageToBotsChannel("Hello"); + System.out.println(randomQuote()); + + } + } + } + } + /** * Sample method: sends a plain text message to the #bots channel. Prints a message indicating success or failure. * @@ -82,6 +121,7 @@ public void sendMessageToBotsChannel(String text) { } else { System.err.print("Error sending message: " + sendMessageResponse.getError()); } + } /** @@ -98,4 +138,40 @@ public void deleteMessageInBotsChannel(String messageTs) { System.err.print("Error sending message: " + deleteMessageResponse.getError()); } } + + + + public String randomQuote() { + inspirationQuotes.add("If you have the opportunity to play this game of life \n " + "you need to appreciate every moment. \n" + "A lot of people don't appreciate the moment until it's passed.\n" + "-Kanye West"); + inspirationQuotes.add("Do not dwell in the past, do not dream of the future.\n" + "Concentrate the mind on the present moment\n" + "-Buddha"); + inspirationQuotes.add("Try to be a rainbow in someone's cloud\n -Maya Angelou"); + inspirationQuotes.add("Sometimes when you innovate, you make mistakes.\n" + "It is best to admit them quickly, and get on with improving your other innovations\n" + "-Steve Jobs"); + inspirationQuotes.add("People say nothing is impossible, but I do nothing everyday.\n" + "-A.A. Milne"); + inspirationQuotes.add("A diamond is merely a lump of coal that did well under pressure\n" + "-Unknown"); + inspirationQuotes.add("No one can make you feel inferior without your consent\n" + "-Eleanor Roosevelt"); + inspirationQuotes.add("Believe you can and you're halfway there\n" + "-T. Roosevelt"); + inspirationQuotes.add("Don't sweat the petty things and don't pet the sweaty things.\n" + "-George Carlin"); + inspirationQuotes.add("There isn't a way things should be. There's just what happens and what we do.\n" + "-Terry Pratchett"); + inspirationQuotes.add("More fuck yeah, less fuck that.\n" + "-Unknown"); + inspirationQuotes.add("Turn your wounds into wisdom\n" + "-Oprah"); + inspirationQuotes.add("Hope is a waking dream\n" + "-Aristotle"); + inspirationQuotes.add("Life is trying things to see if they work\n" + "-Ray Bradbury"); + inspirationQuotes.add("The obstacle is the path\n" + "-Unknown"); + inspirationQuotes.add("We become what we think about\n" + "-Earl Nightingale"); + inspirationQuotes.add("It does not matter how slowly you go as long as you do not stop\n" + "-Confucius"); + inspirationQuotes.add("A jug fills drop by drop\n" + "-Buddha"); + inspirationQuotes.add("Don't regret the past, just learn from it\n" + "-Ben Ipock"); + inspirationQuotes.add("Action is the foundational key to all success\n" + "-Pablo Picasso"); + inspirationQuotes.add("Whatever you are, be a good one\n" + "-Abraham Lincoln"); + + + Random r = new Random(); + + + String quote = inspirationQuotes.get(r.nextInt(inspirationQuotes.size())); + return quote; + } + + + } diff --git a/src/nyc/c4q/ramonaharrison/Main.java b/src/nyc/c4q/ramonaharrison/Main.java index 933e92b..0cf1323 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -2,6 +2,8 @@ import nyc.c4q.ramonaharrison.network.Slack; + + public class Main { public static void main(String[] args) { @@ -9,13 +11,28 @@ public static void main(String[] args) { Bot myBot = new Bot(); myBot.testApi(); - myBot.listChannels(); +// myBot.respondGreeting("@C7M6UM4LA"); + myBot.respond("@C7M6UM4LA"); + + myBot.listMessages(Slack.BOTS_CHANNEL_ID); + + +// myBot.sendMessageToBotsChannel(list); +// myBot.sendMessageToBotsChannel(myBot.randomTip()); +// myBot.sendMessageToBotsChannel("Yum yum!"); +// myBot.sendMessageToBotsChannel("So does this one: "); + +// myBot.sendMessageToBotsChannel(myBot.randomQuote()); +// myBot.sendMessageToBotsChannel("Yum yum!"); +// myBot.sendMessageToBotsChannel("So does this one: "); + + // Post "Hello, world!" to the #bots channel - //myBot.sendMessage("Hello, world!"); +// myBot.sendMessageToBotsChannel("Testing, Testing"); // Post a pineapple photo to the #bots channel //myBot.sendMessage("http://weknowyourdreams.com/images/pineapple/pineapple-07.jpg"); diff --git a/src/nyc/c4q/ramonaharrison/model/Attachment.java b/src/nyc/c4q/ramonaharrison/model/Attachment.java index 8b9d1ef..63c9d1b 100644 --- a/src/nyc/c4q/ramonaharrison/model/Attachment.java +++ b/src/nyc/c4q/ramonaharrison/model/Attachment.java @@ -11,29 +11,132 @@ * */ -public class Attachment { - - // TODO: implement private fields for each of the following attachment JSON keys: - // "fallback" - // "color" - // "pretext" - // "author_name" - // "author_link" - // "author_icon" - // "title" - // "title_link" - // "text" - // "fields" - // "image_url" - // "thumb_url" - // "footer" - // "footer_icon" - // "ts" - - public Attachment(JSONObject json) { - // TODO: parse an attachment from the incoming json - } - - // TODO add getters to access private fields - -} + public class Attachment { + // TODO: implement private fields for each of the following attachment JSON keys: + // “fallback” + private String fallback; + // “color” + private String color; + // “pretext” + private String pretext; + // “author_name” + private String author_name; + // “author_link” + private String author_link; + // “author_icon” + private String author_icon; + // “title” + private String title; + // “title_link” + private String title_link; + // “text” + private String text; + // “fields” + private String fields; + // “image_url” + private String image_url; + // “thumb_url” + private String thumb_url; + // “footer” + private String footer; + // “footer_icon” + private String footer_icon; + // “ts” + private String ts; + + public Attachment(JSONObject json) { + // TODO: parse an attachment from the incoming json + if (json.get("fallback") != null) { + this.fallback = (String) json.get("fallback"); + } + if (json.get("color") !=null) { + this.color = (String) json.get("color"); + } + if (json.get("pretext") != null) { + this.pretext = (String) json.get("pretext"); + } + if (json.get("author name")!=null){ + this.author_name = (String) json.get("author name"); + } + if (json.get("author link")!=null){ + this.author_link = (String) json.get("author link"); + } + if (json.get("author icon")!=null){ + this.author_icon =(String) json.get("author icon"); + } + if (json.get("title")!=null){ + this.title = (String) json.get ("title"); + } + if (json.get("title link")!=null){ + this.title_link = (String) json.get ("title link"); + } + if (json.get("text")!=null){ + this.text = (String) json.get ("text"); + } + if (json.get("fields")!=null){ + this.fields = (String) json.get ("fields"); + } + if (json.get("image url")!=null){ + this.image_url = (String) json.get ("image url"); + } + if (json.get("thumb url")!=null){ + this.thumb_url = (String) json.get ("thumb url"); + } + if (json.get("footer")!=null){ + this.footer= (String) json.get ("footer"); + } + if (json.get("ts")!=null){ + this.ts = (String) json.get ("ts"); + } + } + // TODO add 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 String getFields() { + return fields; + } + public String getImage_url() { + return image_url; + } + public String getThumb_url() { + return thumb_url; + } + public String getFooter() { + return footer; + } + public String getFooter_icon() { + return footer_icon; + } + public String getTs() { + return ts; + } + } + + + + diff --git a/src/nyc/c4q/ramonaharrison/model/User.java b/src/nyc/c4q/ramonaharrison/model/User.java index bc309eb..061b413 100644 --- a/src/nyc/c4q/ramonaharrison/model/User.java +++ b/src/nyc/c4q/ramonaharrison/model/User.java @@ -2,35 +2,131 @@ import org.json.simple.JSONObject; -/** - * 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: - // "id" - // "name" - // "deleted" - // "color" - // "profile" - // "is_admin" - // "is_owner" - // "is_primary_owner" - // "is_restricted" - // "is_ultra_restricted" - // "has_2fa" - // "two_factor_type" - // "has_files" + private String id; + private String name; + private boolean deleted; + private String color; + private String profile; + private boolean isAdmin; + private boolean isOwner; + private boolean isPrimaryOwner; + private boolean isRestricted; + private boolean has2fa; + private boolean twoFactorType; + private boolean hasFiles; + public User(JSONObject json) { - // TODO: parse a user from the incoming json + + if(json.get("id") != null) { + this.id = (String) json.get("id"); + } + + if(json.get("name") != null) { + this.name = (String) json.get("name"); + } + + if(json.get("deleted") != null) { + this.deleted = (Boolean) json.get("deleted"); + } + + if(json.get("color") != null) { + this.color = (String) json.get("color"); + } + + if(json.get("profile") != null) { + this.profile = (String) json.get("profile"); + } + + if(json.get("isAdmin") != null) { + this.isAdmin = (Boolean) json.get("isAdmin"); + } + + if(json.get("isOwner") != null) { + this.isOwner = (Boolean) json.get("isOwner"); + } + + if(json.get("isPrimaryOwner") != null) { + this.isPrimaryOwner = (Boolean) json.get("isPrimaryOwner"); + } + + if(json.get("isRestricted") != null) { + this.isRestricted = (Boolean) json.get("isRestricted"); + } + + if(json.get("has2fa") != null) { + this.has2fa = (Boolean) json.get("has2fa"); + } + + if(json.get("twoFactorType") != null) { + this.twoFactorType = (Boolean) json.get("twoFactorType"); + } + + if(json.get("hasFile") != null) { + this.hasFiles = (Boolean) json.get("hasFiles"); + } + + } + + public String getId() { + + return id; + } + + public String getName() { + + return name; + } + + public boolean deleted() { + + return deleted; + } + + public String getcolor() { + + return color; } - // TODO add getters to access private fields + public String getProfile() { + + return profile; + } + + public boolean IsAdmin() { + + return isAdmin; + } + + public boolean isOwner() { + + return isOwner; + } + + public boolean isPrimaryOwner() { + + return isPrimaryOwner; + } + + public boolean isRestricted() { + + return isRestricted; + } + + public boolean has2fa() { + + return has2fa; + } + + public boolean twoFactorType() { + + return twoFactorType; + } + + public boolean hasFiles() { + + return hasFiles; + } } diff --git a/src/nyc/c4q/ramonaharrison/network/HTTPS.java b/src/nyc/c4q/ramonaharrison/network/HTTPS.java index 7da0c8d..2f3f8e2 100644 --- a/src/nyc/c4q/ramonaharrison/network/HTTPS.java +++ b/src/nyc/c4q/ramonaharrison/network/HTTPS.java @@ -18,7 +18,10 @@ public class HTTPS { /** * Reads from 'reader' by 'blockSize' until end-of-stream, and returns its complete contents. + * */ + + private static String readAll(InputStreamReader reader, int blockSize) throws IOException { final char buffer[] = new char[blockSize]; StringBuilder builder = new StringBuilder(); diff --git a/src/nyc/c4q/ramonaharrison/network/Slack.java b/src/nyc/c4q/ramonaharrison/network/Slack.java index 87521bd..fa2ed3d 100644 --- a/src/nyc/c4q/ramonaharrison/network/Slack.java +++ b/src/nyc/c4q/ramonaharrison/network/Slack.java @@ -20,6 +20,7 @@ public class Slack { + public static final boolean as_user= true; 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"; @@ -28,7 +29,8 @@ public class Slack { private static final String ENDPOINT_POST_MESSAGE = "chat.postMessage"; private static final String ENDPOINT_DELETE_MESSAGE = "chat.delete"; - public static final String BOTS_CHANNEL_ID = "C2ABKERFT"; + public static final String BOTS_CHANNEL_ID = "C7KE0KTM4"; + /** * Static method to test the Slack API. @@ -82,7 +84,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+"&as_user="+as_user); return new SendMessageResponse(HTTPS.get(sendMessageUrl)); }