Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion src/nyc/c4q/ramonaharrison/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Message> 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());
}

}

Expand Down Expand Up @@ -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<User> 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());
}
}
}
61 changes: 50 additions & 11 deletions src/nyc/c4q/ramonaharrison/Main.java
Original file line number Diff line number Diff line change
@@ -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");

}
}
}
}
174 changes: 156 additions & 18 deletions src/nyc/c4q/ramonaharrison/model/Attachment.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Field> 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<Field>();
for(int i=0; i<jasonFields.size();i++){

Field field=new Field((JSONObject)jasonFields.get(i));
this.fields.add(field);

}


}
if (json.containsKey("ts")){
this.ts=(Long)json.get("ts");
}

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_link");

}

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("text");

}
if(json.containsKey("image_url")){
this.image_url=(String)json.get("image_url");

}
if(json.containsKey("thumb_url")){
this.thumb_url=(String)json.get("thumb_url");

}
if(json.containsKey("footer")){
this.footer=(String)json.get("footer");

}
if(json.containsKey("footer_icon")){
this.footer_icon=(String)json.get("footer_icon");

}
}

// 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 List<Field> 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;
}



}
27 changes: 27 additions & 0 deletions src/nyc/c4q/ramonaharrison/model/Attachment.json
Original file line number Diff line number Diff line change
@@ -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
}
]


Loading