diff --git a/src/nyc/c4q/ramonaharrison/Bot.java b/src/nyc/c4q/ramonaharrison/Bot.java index 6a52dd4..8b230ca 100644 --- a/src/nyc/c4q/ramonaharrison/Bot.java +++ b/src/nyc/c4q/ramonaharrison/Bot.java @@ -2,10 +2,14 @@ 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.ArrayList; import java.util.List; +import java.util.Queue; /** * Created by Ramona Harrison @@ -16,7 +20,38 @@ public class Bot { // TODO: implement your bot logic! - public Bot() { + + Words words = new Words(); + public Bot() {} + + + + public void replaceWord(String channelId){ + ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); + + if (listMessagesResponse.isOk()) { + + + List messages = listMessagesResponse.getMessages(); + + + for(int i=messages.size()-1;i>=0;i--){//for (Message message : messages) { + + for(String replaceable: words.getmWord().keySet()){ + if( messages.get(i).getText().contains(replaceable.toLowerCase())){ + String output= messages.get(i).getText().replace(replaceable,words.getmWord().get(replaceable)); + sendMessageToBotsChannel(output); +// System.out.println("\nMessages: "); +// System.out.println(); +// System.out.println("Timestamp: " + message.getTs()); +// System.out.println("Message: " + output); + + } + } + } + } else { + System.err.print("Error listing messages: " + listMessagesResponse.getError()); + } } @@ -98,4 +133,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..d7c16a3 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -1,24 +1,63 @@ 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) { - Bot myBot = new Bot(); - myBot.testApi(); + public static void main(String[] args) throws IOException, ParseException { + + + //JSONObject obj = new JSONObject(); + JSONParser parser = new JSONParser(); + + + + Object obj = parser.parse(new FileReader("/Users/Millochka/Desktop/accesscodeClass/SlackBot/src/nyc/c4q/ramonaharrison/model/Attachment.json")); + + JSONArray jsonObject = (JSONArray) obj; + + Slack.sendMessageWithAttachments(jsonObject); + + + + + Bot myBot = new Bot(); + + myBot.replaceWord(Slack.BOTS_CHANNEL_ID); + + //} + + + //myBot.testApi(); - myBot.listChannels(); + //myBot.listChannels(); - myBot.listMessages(Slack.BOTS_CHANNEL_ID); - // Post "Hello, world!" to the #bots channel - //myBot.sendMessage("Hello, world!"); + // Post "Hello, world!" to the #bots channel + //myBot.sendMessageToBotsChannel("Test2!"); - // Post a pineapple photo to the #bots channel - //myBot.sendMessage("http://weknowyourdreams.com/images/pineapple/pineapple-07.jpg"); + // Post a pineapple photo to the #bots channel + //Slack.sendMessage("http://weknowyourdreams.com/images/pineapple/pineapple-07.jpg"); - } -} \ No newline at end of file + } + } \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/model/Attachment.java b/src/nyc/c4q/ramonaharrison/model/Attachment.java index 8b9d1ef..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/Attachment.json b/src/nyc/c4q/ramonaharrison/model/Attachment.json new file mode 100644 index 0000000..d717052 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/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/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/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..8a1b7d2 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. @@ -68,6 +76,13 @@ public static ListMessagesResponse listMessages(String 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 +97,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 +105,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" + + }; +}