diff --git a/Attachment.json b/Attachment.json new file mode 100644 index 0000000..d717052 --- /dev/null +++ b/Attachment.json @@ -0,0 +1,27 @@ + [ + { + "fallback": "Required plain-text summary of the attachment.", + "color": "#ff0066", + "pretext": "Optional text that appears above the attachment block", + "author_name": "Mila Urakhchinskaya", + "author_link": "http://flickr.com/bobby/", + "author_icon": "http://static5.depositphotos.com/1043279/481/i/950/depositphotos_4812581-Banyan-or-ficus-bonsai-tree.jpg", + "title": "Polite Words and Expressions", + "title_link": "http://www.macmillandictionary.com/us/thesaurus-category/american/polite-words-and-expressions", + "text": "Please, familiarize yourself with Dictionary of Polite word ", + "fields": [ + { + "title": "Priority", + "value": "High", + "short": false + } + ], + "image_url": "http://codropspz.tympanus.netdna-cdn.com/codrops/wp-content/uploads/2016/04/preview.gif", + "thumb_url": "http://example.com/path/to/thumb.png", + "footer": "Educational Opportunity", + "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png", + "ts": 123456789 + } + ] + + diff --git a/src/nyc/c4q/ramonaharrison/AnimalGifs.java b/src/nyc/c4q/ramonaharrison/AnimalGifs.java new file mode 100644 index 0000000..fd03296 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/AnimalGifs.java @@ -0,0 +1,82 @@ +package nyc.c4q.ramonaharrison; + +import java.util.Random; + +/** + * Created by jordansmith on 9/16/16. + */ +public class AnimalGifs { + + + public static String catPics(){ + String [] catPicsArray = new String [28]; + catPicsArray [0] = "http://i.imgur.com/zcP8RYL.gif"; + catPicsArray [1] = "http://i.imgur.com/4dSv0jH.gif"; + catPicsArray [2] = "http://i.imgur.com/vmExtY7.gif"; + catPicsArray [3] = "http://i.imgur.com/NIeyIQP.gif"; + catPicsArray [4] = "http://i.imgur.com/sHQQJG5.gif"; + catPicsArray [5] = "http://i.imgur.com/JKH5xBZ.gif"; + catPicsArray [6] = "http://i.imgur.com/U0aCPSv.gif"; + catPicsArray [7] = "http://i.imgur.com/FH8J8R3.gif"; + catPicsArray [8] = "http://i.imgur.com/7AtUnqc.gif"; + catPicsArray [9] = "http://i.imgur.com/OOVmdwa.gif"; + catPicsArray [10] = "http://i.imgur.com/nfKM2dN.gif"; + catPicsArray [11] = "http://i.imgur.com/nfKM2dN.gif"; + catPicsArray [12] = "http://i.imgur.com/svQyujn.gif"; + catPicsArray [13] = "http://i.imgur.com/fDiFnbO.gif"; + catPicsArray [14] = "http://i.imgur.com/bPd4Kgh.gif"; + catPicsArray [15] = "http://i.imgur.com/AOSdhQe.gif"; + catPicsArray [16] = "http://i.imgur.com/nflEpRJ.gif"; + catPicsArray [17] = "http://i.imgur.com/AOSdhQe.gif"; + catPicsArray [18] = "http://i.imgur.com/JvyQd.gif"; + catPicsArray [19] = "http://i.imgur.com/mMrUT.gif"; + catPicsArray [20] = "http://i.imgur.com/2aJRS.gif"; + catPicsArray [21] = "http://i.imgur.com/5xfNq82.gif"; + catPicsArray [22] = "http://i.imgur.com/nLwiod0.gif"; + catPicsArray [23] = "http://i.imgur.com/4nmgRLR.gif"; + catPicsArray [24] = "http://i.imgur.com/KqgdiBH.gif"; + catPicsArray [25] = "http://i.imgur.com/j3PYjtJ.gif"; + catPicsArray [26] = "http://i.imgur.com/kNIBvRs.gif"; + catPicsArray [27] = "http://i.imgur.com/EH7PhPo.gif"; + + + Random rand = new Random(); + int randomint = rand.nextInt(28); + return catPicsArray[randomint]; + + } + + public static String dogPics(){ + String [] dogPicsArray = new String [21]; + dogPicsArray[0] = "http://i.imgur.com/mJRa4GD.gif"; + dogPicsArray[1] = "http://i.imgur.com/cOoXLpI.gif"; + dogPicsArray[2] = "http://i.imgur.com/DI4bf50.gif"; + dogPicsArray[3] = "http://i.imgur.com/9nROuQm.gif"; + dogPicsArray[4] = "http://i.imgur.com/4mMMYPF.gif"; + dogPicsArray[5] = "http://i.imgur.com/zyk2jFL.gif"; + dogPicsArray[6] = "http://i.imgur.com/R9ovCJh.gif"; + dogPicsArray[7] = "http://i.imgur.com/ow2o1bM.gif"; + dogPicsArray[8] = "http://i.imgur.com/hRUJSRv.gif"; + dogPicsArray[9] = "http://i.imgur.com/m6mZY6K.gif"; + dogPicsArray[10] = "http://i.imgur.com/pCmV1uv.gif"; + dogPicsArray[11] = "http://i.imgur.com/QRixBl4.gif"; + dogPicsArray[12] = "http://i.imgur.com/cUYg2a8.gif"; + dogPicsArray[13] = "http://i.imgur.com/yFcHOv4.gif"; + dogPicsArray[14] = "http://i.imgur.com/DJvGKg9.gif"; + dogPicsArray[15] = "http://i.imgur.com/2FMwZuV.gif"; + dogPicsArray[16] = "http://i.imgur.com/jggkck1.gif"; + dogPicsArray[17] = "http://i.imgur.com/R3ZnXeR.gif"; + dogPicsArray[18] = "http://i.imgur.com/FcWJwYM.gif"; + dogPicsArray[19] = "http://i.imgur.com/pbHDghc.gif"; + dogPicsArray[20] = "http://i.imgur.com/RgxRyUu.gif"; + + Random rand = new Random(); + int randomint = rand.nextInt(21); + return dogPicsArray[randomint]; + + + + } + + +} diff --git a/src/nyc/c4q/ramonaharrison/Bot.java b/src/nyc/c4q/ramonaharrison/Bot.java index 6a52dd4..bb266d1 100644 --- a/src/nyc/c4q/ramonaharrison/Bot.java +++ b/src/nyc/c4q/ramonaharrison/Bot.java @@ -2,10 +2,12 @@ import nyc.c4q.ramonaharrison.model.Channel; import nyc.c4q.ramonaharrison.model.Message; +import nyc.c4q.ramonaharrison.model.User; import nyc.c4q.ramonaharrison.network.*; import nyc.c4q.ramonaharrison.network.response.*; +import nyc.c4q.ramonaharrison.util.Words; -import java.util.List; +import java.util.*; /** * Created by Ramona Harrison @@ -16,10 +18,154 @@ public class Bot { // TODO: implement your bot logic! - public Bot() { + + Words words = new Words(); + public Bot() {} + + + + + public void pleaseBotv2(){ + while (true){ + ListMessagesResponse listMessagesResponse = Slack.listMessages(Slack.BOTS_CHANNEL_ID); + if (listMessagesResponse.isOk()) { + Bot bot = new Bot(); + List messages = listMessagesResponse.getMessages(); + String originalMessage = messages.get(0).getText(); + if(originalMessage.toLowerCase().contains("please")){ + bot.sendMessageToBotsChannel("Someone said please. Here's a cat gif"); + bot.sendMessageToBotsChannel(AnimalGifs.catPics()); + } + + + } + else{ System.err.print("Error listing messages: " + listMessagesResponse.getError()); + break; + + } + + + } + + } + + + + + public void pleaseBotv4(){ + int count = 0; + while (true){ + ListMessagesResponse listMessagesResponse = Slack.listMessages(Slack.BOTSTEST_CHANNEL_ID); + if (listMessagesResponse.isOk()) { + Bot bot = new Bot(); + List messages = listMessagesResponse.getMessages(); + String originalMessage = messages.get(0).getText(); + if(count <= 3 && originalMessage.toLowerCase().contains("please")){ + bot.sendMessageToBotsChannel("Someone said please. Here's a cat gif"); + bot.sendMessageToBotsChannel(AnimalGifs.catPics()); + count++; + } + if(count >= 4 && count <= 6 && originalMessage.toLowerCase().contains("please")){ + bot.sendMessageToBotsChannel("That's enough cats. Here's a dog gif"); + bot.sendMessageToBotsChannel(AnimalGifs.dogPics()); + count++; + } + if(count > 6){ + count = 0; + } + + + + + } + else{ System.err.print("Error listing messages: " + listMessagesResponse.getError()); + break; + + } + + + } } + + + public void pleaseBotv3() { + int count = 0; + while (true) { + ListMessagesResponse listMessagesResponse = Slack.listMessages(Slack.BOTSTEST_CHANNEL_ID); + if (listMessagesResponse.isOk()) { + Bot bot = new Bot(); + List messages = listMessagesResponse.getMessages(); + String originalMessage = messages.get(0).getText(); + while (count <= 5) { + if (originalMessage.toLowerCase().contains("please")) { + bot.sendMessageToBotsChannel("Someone said the magic word. Here's a cat gif"); + bot.sendMessageToBotsChannel(AnimalGifs.catPics()); + count++; + break; + } + else{ + break; + } + } + while (count == 6) { + if (originalMessage.toLowerCase().contains("please")) { + bot.sendMessageToBotsChannel("That's enough Cats. Here's a dog gif instead"); + bot.sendMessageToBotsChannel(AnimalGifs.dogPics()); + count++; + break; + } + else{ + break; + } + } + while (count >= 7 && count <= 10) { + if (originalMessage.toLowerCase().contains("please")) { + bot.sendMessageToBotsChannel("Someone said the magic word. Here's a dog gif"); + bot.sendMessageToBotsChannel(AnimalGifs.dogPics()); + count++; + break; + } + else{ + break; + } + + } + while (count == 11) { + if (originalMessage.toLowerCase().contains("please")) { + bot.sendMessageToBotsChannel("That's enough dogs. Here's a cat gif instead"); + bot.sendMessageToBotsChannel(AnimalGifs.catPics()); + count = 1; + break; + } + else{ + break; + } + + } + + } + + + else{ + System.err.print("Error listing messages: " + listMessagesResponse.getError()); + break; + + } + + + } + + } + + + + + + + + /** * Sample method: tests the Slack API. Prints a message indicating success or failure. */ @@ -98,4 +244,21 @@ public void deleteMessageInBotsChannel(String messageTs) { System.err.print("Error sending message: " + deleteMessageResponse.getError()); } } + + public void listUsers(String channelId) { + ListUsers listUsers = Slack.listUsers(channelId); + + if (listUsers.isOk()) { + List users = listUsers.getUser(); + + System.out.println("\nUsers: "); + for (User user : users) { + System.out.println(); + + System.out.println("User: " + user.getName()); + } + } else { + System.err.print("Error listing messages: " + listUsers.getError()); + } + } } diff --git a/src/nyc/c4q/ramonaharrison/Main.java b/src/nyc/c4q/ramonaharrison/Main.java index 933e92b..313b764 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -1,24 +1,40 @@ package nyc.c4q.ramonaharrison; +import com.sun.tools.classfile.Opcode; +import com.sun.tools.javac.util.List; +import nyc.c4q.ramonaharrison.model.Attachment; import nyc.c4q.ramonaharrison.network.Slack; +import nyc.c4q.ramonaharrison.util.Words; +import org.json.simple.JSONObject; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + public class Main { - public static void main(String[] args) { + public static void main(String[] args) throws Exception { Bot myBot = new Bot(); - myBot.testApi(); - - myBot.listChannels(); + JSONParser parser = new JSONParser(); + Object obj = parser.parse(new FileReader("Attachment.json")); + JSONArray jsonObject = (JSONArray) obj; - myBot.listMessages(Slack.BOTS_CHANNEL_ID); + //Slack.sendMessageWithAttachments(jsonObject); - // Post "Hello, world!" to the #bots channel - //myBot.sendMessage("Hello, world!"); + // myBot.pleaseBotv2(); + myBot.replaceWord(Slack.BOTS_CHANNEL_ID); - // Post a pineapple photo to the #bots channel - //myBot.sendMessage("http://weknowyourdreams.com/images/pineapple/pineapple-07.jpg"); } } \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/model/Articles.java b/src/nyc/c4q/ramonaharrison/model/Articles.java new file mode 100644 index 0000000..0022a7f --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Articles.java @@ -0,0 +1,62 @@ +package nyc.c4q.ramonaharrison.model; + +import org.json.simple.JSONObject; + +/** + * Created by jordansmith on 9/19/16. + */ +public class Articles { + + String author; + String title; + String description; + String url; + String urlToImage; + String publishedAt; + + public Articles(JSONObject json){ + if(json.containsKey("author")){ + this.author=(String)json.get("author"); + } + if(json.containsKey("title")){ + this.title=(String)json.get("title"); + } + if(json.containsKey("description")){ + this.description=(String)json.get("description"); + } + if(json.containsKey("url")){ + this.url=(String)json.get("url"); + } + if(json.containsKey("urlToImage")){ + this.urlToImage=(String)json.get("urlToImage"); + } + if(json.containsKey("publishedAt")){ + this.publishedAt=(String)json.get("publishedAt"); + } + + } + + public String getAuthor() { + return author; + } + + public String getTitle() { + return title; + } + + public String getDescription() { + return description; + } + + public String getUrl() { + return url; + } + + public String getUrlToImage() { + return urlToImage; + } + + public String getPublishedAt() { + return publishedAt; + } +} diff --git a/src/nyc/c4q/ramonaharrison/model/Attachment.java b/src/nyc/c4q/ramonaharrison/model/Attachment.java index 8b9d1ef..0a6316f 100644 --- a/src/nyc/c4q/ramonaharrison/model/Attachment.java +++ b/src/nyc/c4q/ramonaharrison/model/Attachment.java @@ -1,6 +1,11 @@ package nyc.c4q.ramonaharrison.model; +import org.json.simple.JSONArray; import org.json.simple.JSONObject; +import org.omg.CORBA.Object; + +import java.util.ArrayList; +import java.util.List; /** * Created by Ramona Harrison @@ -13,27 +18,160 @@ public class Attachment { - // TODO: implement private fields for each of the following attachment JSON keys: - // "fallback" - // "color" - // "pretext" - // "author_name" - // "author_link" - // "author_icon" - // "title" - // "title_link" - // "text" - // "fields" - // "image_url" - // "thumb_url" - // "footer" - // "footer_icon" - // "ts" + private String fallback; + private String 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 image_url; + private String thumb_url; + private String footer; + private String footer_icon; + private long ts; public Attachment(JSONObject json) { - // TODO: parse an attachment from the incoming json + + + + if(json.get("color")!=null){ + this.fallback=(String)json.get("fallback"); + } + + if(json.containsKey("fields")){ + + JSONArray jasonFields = (JSONArray) json.get("fields"); + this.fields = new ArrayList(); + for(int i=0; i 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 long 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..038c441 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Field.java @@ -0,0 +1,43 @@ +package nyc.c4q.ramonaharrison.model; + +import org.json.simple.JSONObject; + +/** + * Created by Millochka on 9/11/16. + */ +public class Field { + + + private String title= "Priority"; + private String value = "High"; + private boolean isShort=false; + + + 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("Short")){ + this.isShort=(Boolean) json.get("Short"); + } + + + } + + + public String getTitle() { + return title; + } + + public String getValue() { + return value; + } + + public boolean isShort() { + return isShort; + } +} + diff --git a/src/nyc/c4q/ramonaharrison/model/News.java b/src/nyc/c4q/ramonaharrison/model/News.java new file mode 100644 index 0000000..9d4d1f2 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/News.java @@ -0,0 +1,128 @@ +package nyc.c4q.ramonaharrison.model; + +import nyc.c4q.ramonaharrison.Bot; +import nyc.c4q.ramonaharrison.network.HTTPS; +import nyc.c4q.ramonaharrison.network.Slack; +import nyc.c4q.ramonaharrison.network.response.ListMessagesResponse; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +/** + * Created by jordansmith on 9/19/16. + */ +public class News { + + List articles; + + List author = new ArrayList<>(); + List title = new ArrayList<>(); + List description = new ArrayList<>(); + List url = new ArrayList<>(); + List urlToImage = new ArrayList<>(); + List publishedAt = new ArrayList<>(); + + public static String vergeUrl = "https://newsapi.org/v1/articles?source=the-verge&sortBy=top&apiKey=589ffd42ff994295802a4ccef02aa18e"; + public static String espnURl = "https://newsapi.org/v1/articles?source=espn&sortBy=top&apiKey=589ffd42ff994295802a4ccef02aa18e"; + public static String polygonURL = "https://newsapi.org/v1/articles?source=polygon&sortBy=top&apiKey=589ffd42ff994295802a4ccef02aa18e"; + +// public News(JSONObject json){ +// if(json.containsKey("articles")){ +// JSONArray jsonArticles = (JSONArray) json.get("articles"); +// this.articles = new ArrayList(); +// for (int i = 0; i < jsonArticles.size() ; i++) { +// JSONObject mjsonObject = (JSONObject) jsonArticles.get(i); +// Articles mArticles = new Articles(mjsonObject); +// author.add(mArticles.getAuthor()); +// title.add(mArticles.getTitle()); +// description.add(mArticles.getDescription()); +// url.add(mArticles.getUrl()); +// urlToImage.add(mArticles.getUrlToImage()); +// publishedAt.add(mArticles.getPublishedAt()); +// } +// System.out.println(author.get(0)); +// System.out.println(title.get(0)); +// System.out.println(author.get(0)); +// System.out.println(description.get(0)); +// } +// } + + public void newsReaderBot(String apiURL){ + JSONObject json = HTTPS.get(HTTPS.stringToURL(apiURL)); + if(json.containsKey("articles")){ + JSONArray jsonArticles = (JSONArray) json.get("articles"); + for (int i = 0; i < jsonArticles.size() ; i++) { + JSONObject mjsonObject = (JSONObject) jsonArticles.get(i); + Articles mArticles = new Articles(mjsonObject); + author.add(mArticles.getAuthor()); + title.add(mArticles.getTitle()); + description.add(mArticles.getDescription()); + url.add(mArticles.getUrl()); + urlToImage.add(mArticles.getUrlToImage()); + publishedAt.add(mArticles.getPublishedAt()); + } + + } + + } + + public void sendNews(){ + while (true){ + ListMessagesResponse listMessagesResponse = Slack.listMessages(Slack.BOTS_CHANNEL_ID); + if (listMessagesResponse.isOk()) { + Bot bot = new Bot(); + List messages = listMessagesResponse.getMessages(); + String originalMessage = messages.get(0).getText(); + Message senderMessage = messages.get(0); + if(originalMessage.contains("<@U2ADC0PLL> give me tech news")){ + newsReaderBot(vergeUrl); + bot.sendMessageToBotsChannel("Alright sure " + "<@" + senderMessage.getUser() + ">" + + ". Check out this random story from the Verge:"); + Random rand = new Random(); + int value = rand.nextInt(title.size()-1); + bot.sendMessageToBotsChannel("*" + title.get(value) +"*"); + bot.sendMessageToBotsChannel("_" + description.get(value) + "_"); + bot.sendMessageToBotsChannel(urlToImage.get(value)); + } + if(originalMessage.contains("<@U2ADC0PLL> give me sports news")){ + newsReaderBot(espnURl); + bot.sendMessageToBotsChannel("Alright sure " + "<@" + senderMessage.getUser() + ">" + + ". Check out this random story from the ESPN:"); + Random rand = new Random(); + int value = rand.nextInt(title.size()-1); + bot.sendMessageToBotsChannel("*" + title.get(value) +"*"); + bot.sendMessageToBotsChannel("_" + description.get(value) + "_"); + bot.sendMessageToBotsChannel(urlToImage.get(value)); + } + if(originalMessage.contains("<@U2ADC0PLL> give me gaming news")){ + newsReaderBot(polygonURL); + bot.sendMessageToBotsChannel("Alright sure " + "<@" + senderMessage.getUser() + ">" + + ". Check out this random story from the Polygon:"); + Random rand = new Random(); + int value = rand.nextInt(title.size()-1); + bot.sendMessageToBotsChannel("*" + title.get(value) +"*"); + bot.sendMessageToBotsChannel("_" + description.get(value) + "_"); + bot.sendMessageToBotsChannel(urlToImage.get(value)); + } + + + + } + else{ System.err.print("Error listing messages: " + listMessagesResponse.getError()); + break; + + } + + + } + + } + + + + + +} diff --git a/src/nyc/c4q/ramonaharrison/model/Profile.java b/src/nyc/c4q/ramonaharrison/model/Profile.java new file mode 100644 index 0000000..733483e --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Profile.java @@ -0,0 +1,112 @@ +package nyc.c4q.ramonaharrison.model; + +import org.json.simple.JSONObject; + +/** + * Created by Millochka on 9/11/16. + */ +public class Profile { + + private String first_name ="Bobby"; + private String last_name = "Tables"; + private String real_name ="Bobby Tables"; + private String email= "bobby@slack.com"; + private String skype = "my-skype-name"; + private String phone = "+1 (123) 456 7890"; + private String image_24= "https:\\/\\/..."; + private String image_32= "https:\\/\\/..."; + private String image_48= "https:\\/\\/..."; + private String image_72= "https:\\/\\/..."; + private String image_192= "https:\\/\\/..."; + private String image_512= "https:\\/\\/..."; + + public Profile(JSONObject json){ + + if(json.containsKey("first_name")){ + this.first_name=(String) json.get("first_name"); + } + if(json.containsKey("last_name")){ + this.last_name=(String) json.get("last_name"); + } + if(json.containsKey("real_name")){ + this.real_name=(String) json.get("real_name"); + } + 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("image_24")){ + this.image_24=(String) json.get("image_24"); + } + if(json.containsKey("image_32")){ + this.image_32=(String) json.get("image_32"); + } + if(json.containsKey("image_48")){ + this.image_48=(String) json.get("image_48"); + } + if(json.containsKey("image_72")){ + this.image_72=(String) json.get("image_72"); + } + if(json.containsKey("image_192")){ + this.image_192=(String) json.get("image_192"); + } + if(json.containsKey("image_512")){ + this.image_512=(String) json.get("image_512"); + } + + } + + + public String getFirst_name() { + return first_name; + } + + public String getLast_name() { + return last_name; + } + + public String getReal_name() { + return real_name; + } + + public String getEmail() { + return email; + } + + public String getSkype() { + return skype; + } + + public String getPhone() { + return phone; + } + + public String getImage_24() { + return image_24; + } + + public String getImage_32() { + return image_32; + } + + public String getImage_48() { + return image_48; + } + + public String getImage_72() { + return image_72; + } + + public String getImage_192() { + return image_192; + } + + public String getImage_512() { + return image_512; + } +} diff --git a/src/nyc/c4q/ramonaharrison/model/User.java b/src/nyc/c4q/ramonaharrison/model/User.java index bc309eb..7fb1b54 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 @@ -13,24 +17,126 @@ 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 List profile; + private boolean is_admin; + private boolean is_owner; + private boolean is_primary_owner; + private boolean is_restricted; + private boolean is_ultra_restricted; + private boolean has_2fa; + private String two_factor_type; + private boolean has_files; public User(JSONObject json) { - // TODO: 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("color")) { + this.color = (String) json.get("color"); + } + if (json.containsKey("two_factor_type")) { + this.two_factor_type = (String) json.get("two_factor_type"); + } + if (json.containsKey("deleted")) { + this.deleted = (boolean) json.get("deleted"); + } + if (json.containsKey("is_admin")) { + this.is_admin = (boolean) json.get("is_admin"); + } + if (json.containsKey("is_owner")) { + this.is_owner = (boolean) json.get("is_owner"); + } + if (json.containsKey("is_primary_owner")) { + this.is_primary_owner = (boolean) json.get("is_primary_owner"); + } + if (json.containsKey("is_restricted")) { + this.is_restricted = (boolean) json.get("is_restricted"); + } + if (json.containsKey("is_ultra_restricted")) { + this.is_ultra_restricted = (boolean) json.get("is_ultra_restricted"); + } + if (json.containsKey("has_2fa")) { + this.has_2fa = (boolean) json.get("has_2fa"); + } + if (json.containsKey("has_files")) { + this.has_files = (boolean) json.get("has_files"); + } + + if (json.containsKey("profile")) { + + JSONArray jasonFields = (JSONArray) json.get("profile"); + this.profile = new ArrayList(); + for (int i = 0; i < jasonFields.size(); i++) { + + Profile profile = new Profile((JSONObject) jasonFields.get(i)); + this.profile.add(profile); + + } + + + } + } + + + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public boolean isDeleted() { + return deleted; + } + + public String getColor() { + return color; + } + + public List getProfile() { + return profile; + } + + public boolean is_admin() { + return is_admin; + } + + public boolean is_owner() { + return is_owner; + } + + public boolean is_primary_owner() { + return is_primary_owner; } - // TODO add getters to access private fields + public boolean is_restricted() { + return is_restricted; + } + + public boolean is_ultra_restricted() { + return is_ultra_restricted; + } + + public boolean isHas_2fa() { + return has_2fa; + } + + public String getTwo_factor_type() { + return two_factor_type; + } + + public boolean isHas_files() { + return has_files; + } } diff --git a/src/nyc/c4q/ramonaharrison/network/Slack.java b/src/nyc/c4q/ramonaharrison/network/Slack.java index 87521bd..5df5a10 100644 --- a/src/nyc/c4q/ramonaharrison/network/Slack.java +++ b/src/nyc/c4q/ramonaharrison/network/Slack.java @@ -1,14 +1,18 @@ package nyc.c4q.ramonaharrison.network; +import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin; import nyc.c4q.ramonaharrison.model.Attachment; import nyc.c4q.ramonaharrison.network.response.*; import nyc.c4q.ramonaharrison.util.Token; +import org.json.simple.JSONArray; import org.json.simple.JSONObject; +import org.json.simple.JSONArray; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLEncoder; import java.util.List; +//import org.apache.commons.codec.binary.Hex; /** * Created by Ramona Harrison @@ -21,6 +25,8 @@ public class Slack { private static final String API_KEY = Token.findApiToken(); + private static final String USER_NAME= "Smartbot"; + private static final String USER_ICON="http://i50.photobucket.com/albums/f329/liudmilaura/Yin_zpshsyvcsod.jpg"; 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"; @@ -29,6 +35,8 @@ public class Slack { private static final String ENDPOINT_DELETE_MESSAGE = "chat.delete"; public static final String BOTS_CHANNEL_ID = "C2ABKERFT"; + public static final String BOTSTEST_CHANNEL_ID = "C2ATALHQQ"; + /** * Static method to test the Slack API. @@ -64,10 +72,16 @@ public static ListChannelsResponse listChannels() { public static ListMessagesResponse listMessages(String channelId) { URL listMessagesUrl = HTTPS.stringToURL(BASE_URL + ENDPOINT_LIST_MESSAGES + "?token=" + API_KEY + "&channel=" + channelId); - return new ListMessagesResponse(HTTPS.get(listMessagesUrl)); } + public static ListUsers listUsers(String channelId) { + + URL listUsersUrl = HTTPS.stringToURL(BASE_URL + ENDPOINT_LIST_MESSAGES + "?token=" + API_KEY + "&channel=" + channelId ); + + return new ListUsers(HTTPS.get(listUsersUrl)); + } + /** * Static method to send a message to the #bots channel. * @@ -82,7 +96,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 +"&icon_url="+ USER_ICON+"&username="+USER_NAME +"&channel=" + BOTS_CHANNEL_ID + "&text=" + messageText); return new SendMessageResponse(HTTPS.get(sendMessageUrl)); } @@ -90,14 +104,27 @@ 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) { + public static SendMessageResponse sendMessageWithAttachments(JSONArray obj) throws UnsupportedEncodingException { + + String attachment =obj.toJSONString(); + + //try { + String messageText = URLEncoder.encode(attachment); + +// } catch (UnsupportedEncodingException e) { +// throw new RuntimeException(e); +// } + //System.out.println(messageText); + URL sendMessageUrl = HTTPS.stringToURL(BASE_URL + ENDPOINT_POST_MESSAGE + "?token=" + API_KEY + "&icon_url="+ USER_ICON+"&username="+USER_NAME+"&channel=" + BOTS_CHANNEL_ID + "&text="+ "Text"+"&attachments=" + messageText ); + + return new SendMessageResponse(HTTPS.get(sendMessageUrl)); // TODO (optional): implement this method! See https://api.slack.com/docs/message-attachments - throw new RuntimeException("Method not implemented!"); + //throw new RuntimeException("Method not implemented!"); } /** diff --git a/src/nyc/c4q/ramonaharrison/network/response/ListUsers.java b/src/nyc/c4q/ramonaharrison/network/response/ListUsers.java new file mode 100644 index 0000000..513f05e --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/network/response/ListUsers.java @@ -0,0 +1,35 @@ +package nyc.c4q.ramonaharrison.network.response; + +import nyc.c4q.ramonaharrison.model.Message; +import nyc.c4q.ramonaharrison.model.User; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Millochka on 9/15/16. + */ +public class ListUsers extends Response { + + + private List user; + + public ListUsers(JSONObject json) { + super(json); + + if (json.containsKey("user")) { + JSONArray array = (JSONArray) json.get("user"); + + this.user = new ArrayList(); + for (int i = 0; i < array.size(); i++) { + this.user.add(new User((JSONObject) array.get(i))); + } + } + } + + public List getUser() { + return user; + } +} diff --git a/src/nyc/c4q/ramonaharrison/util/Words.java b/src/nyc/c4q/ramonaharrison/util/Words.java new file mode 100644 index 0000000..26471e6 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/util/Words.java @@ -0,0 +1,149 @@ +package nyc.c4q.ramonaharrison.util; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by Millochka on 9/14/16. + */ +public class Words { + + public Words(){ + + this.initOfReplaceable(this.getReplaceable()); + this.initOfReplacement(this.getReplacemnet()); + + this.prepareReplacement(mReplaceable, mReplacement); + + } + + private Map mWord= new HashMap<>(); + private List mReplaceable= new ArrayList<>(); + + String[] s={}; + + private List mReplacement=new ArrayList<>(); + + public List getmReplaceable(){ + + return mReplaceable; + } + + public List getmReplacement(){ + + return mReplacement; + } + + public Map getmWord(){ + + return mWord; + } + + + + public String[] getReplaceable(){ + return replaceable; + } + + public String[] getReplacemnet(){ + return replacemnet; + } + + + public void initOfReplacement(String[] replacement){ + for(int i=0;i mReplaceable, List mReplacement){ + //if(mReplaceable.size()==mReplacement.size()){ + for(int i=0; i< mReplaceable.size();i++) + mWord.put(mReplaceable.get(i),mReplacement.get(i));//} + +// else{ +// System.out.println("Please make sure your Array of replaceable items is the same size with Array of replacemnets"); +// } + + } + + + + private String[] replaceable = {"really", + "crap" , + "father" , + "dad", + "mom", + "mother", + "come", + "buck", + "minute", + "gun", + "sure", + "pregnant", + "big", + "hard", + "tight", + "wet", + "head" , + "balls", + "ball ", + "shaft" , + "justin bieber" , + "jj" , + "6 ", + "women" , + "thing" , + "weed" , + "punch" , + "sex ", + "while" , + "dirty "}; + + + private String[] replacemnet = { + "mad", + "shiiiii", + "Baby Daddy", + "Baby Daddy", + "Baby Momma", + "Baby Momma", + "skeet", + "$100 Dollars", + "long time", + "chopper", + "Fo Shizzle", + "Prego", + "pause", + "pause", + "pause", + "pause", + "pause", + "pause", + "pause", + "pause", + "She Rocks", + "Hey!!!", + "Drake", + "Chick", + "joint", + "joint", + "knock", + "smash", +"A minute", + "grimey" + + }; +}