From c9e845053007e393a6d167696ad027d60c44a9c4 Mon Sep 17 00:00:00 2001 From: Francisco Andrade Date: Tue, 17 Oct 2017 19:10:29 -0400 Subject: [PATCH 1/2] git update --- src/nyc/c4q/ramonaharrison/Main.java | 6 +- .../c4q/ramonaharrison/model/Attachment.java | 135 +++++++++++++++++- src/nyc/c4q/ramonaharrison/model/User.java | 121 +++++++++++++++- src/nyc/c4q/ramonaharrison/network/Slack.java | 18 ++- 4 files changed, 276 insertions(+), 4 deletions(-) diff --git a/src/nyc/c4q/ramonaharrison/Main.java b/src/nyc/c4q/ramonaharrison/Main.java index 933e92b..d73249e 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -15,10 +15,14 @@ public static void main(String[] args) { myBot.listMessages(Slack.BOTS_CHANNEL_ID); // Post "Hello, world!" to the #bots channel - //myBot.sendMessage("Hello, world!"); + // Post a pineapple photo to the #bots channel //myBot.sendMessage("http://weknowyourdreams.com/images/pineapple/pineapple-07.jpg"); + + //Message with attachment + + myBot.sendMessageWithAttachments("Hello, world!", myBot.message.getAttachments() ); } } \ 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..5b4daa3 100644 --- a/src/nyc/c4q/ramonaharrison/model/Attachment.java +++ b/src/nyc/c4q/ramonaharrison/model/Attachment.java @@ -13,27 +13,160 @@ public class Attachment { + + // TODO: implement private fields for each of the following attachment JSON keys: + private String fallback; // "fallback" + + private String color; // "color" + + private String pretext; // "pretext" + + private String authorName; // "author_name" + + private String authorLink; // "author_link" + + private String authorIcon; // "author_icon" + + private String title; // "title" + + private String titleLink; // "title_link" + + private String text; // "text" + + private String fields; // "fields" + + private String imageUrl; // "image_url" + + private String thumbUrl; // "thumb_url" + + private String footer; // "footer" + + private String footerIcon; // "footer_icon" + + private String ts; // "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) { + color = (String) json.get("color"); + } + if (json.get("pretext") != null) { + pretext = (String) json.get("pretext"); + } + if (json.get("author_icon") != null) { + authorIcon = (String) json.get("author_icon"); + } + if (json.get("author_link") != null) { + authorLink = (String) json.get("author_link"); + } + if (json.get("author_name") != null) { + authorName = (String) json.get("author_name"); + } + if (json.get("title") != null) { + title = (String) json.get("title"); + } + if (json.get("title_link") != null) { + titleLink = (String) json.get("title_link"); + } + if (json.get("text") != null) { + text = (String) json.get("text"); + } + if (json.get("fields") != null) { + fields = (String) json.get("fields"); + } + if (json.get("image_url") != null) { + imageUrl = (String) json.get("image_url"); + } + if (json.get("thumb_url") != null) { + thumbUrl = (String) json.get("thumb_url"); + } + if (json.get("footer") != null) { + footer = (String) json.get("footer"); + } + if (json.get("footer_icon") != null) { + footerIcon = (String) json.get("footer_icon"); + } + if (json.get("ts") != null) { + 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 getAuthorIcon() { + return authorIcon; + } -} + public String getAuthorLink() { + return authorLink; + } + + public String getAuthorName() { + return authorName; + } + + public String getTitle(){ + return title; + } + + public String getTitleLink() { + return titleLink; + } + + public String getFields() { + return fields; + } + + public String getFooter() { + return footer; + } + + public String getFooterIcon() { + return footerIcon; + } + + public String getImageUrl() { + return imageUrl; + } + + public String getText() { + return text; + } + + public String getThumbUrl() { + return thumbUrl; + } + + public String getTs() { + return ts; + } +} \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/model/User.java b/src/nyc/c4q/ramonaharrison/model/User.java index bc309eb..594b1c1 100644 --- a/src/nyc/c4q/ramonaharrison/model/User.java +++ b/src/nyc/c4q/ramonaharrison/model/User.java @@ -1,5 +1,6 @@ package nyc.c4q.ramonaharrison.model; +import com.sun.tools.javac.jvm.Profile; import org.json.simple.JSONObject; /** @@ -14,23 +15,141 @@ public class User { // TODO: implement private fields for each of the following user JSON keys: + // "id" + private String id; + // "name" + private String name; + // "deleted" + private boolean deleted; + // "color" + private String color; + // "profile" + private Profile profile; + // "is_admin" + private boolean isAdmin; + // "is_owner" + private boolean isOwner; + // "is_primary_owner" + private boolean isPrimaryOwner; + // "is_restricted" + private boolean isRestricted; + // "is_ultra_restricted" + private boolean isUltraRestricted; + // "has_2fa" + private boolean has2fa; + // "two_factor_type" + private boolean twoFactorType; + // "has_files" + 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 = (Profile) json.get("profile"); + } + if (json.get("is_admin") != null) { + this.isAdmin = (boolean) json.get("is_admin"); + } + if (json.get("is_owner") != null) { + this.isOwner = (boolean) json.get("is_owner"); + } + if (json.get("is_primary_owner") != null) { + this.isPrimaryOwner = (boolean) json.get("is_primary_owner"); + } + if (json.get("is_restricted") != null) { + this.isRestricted = (boolean) json.get("is_restricted"); + } + if (json.get("is_ultra_restricted") != null) { + this.isUltraRestricted = (boolean) json.get("is_ultra_restricted"); + } + if (json.get("has_2fa") != null) { + this.has2fa = (boolean) json.get("has_2fa"); + } + if (json.get("two_factor_type") != null) { + this.twoFactorType = (boolean) json.get("two_factor_type"); + } + if (json.get("has_files") != null) { + this.hasFiles = (boolean) json.get("has_files"); + } } // TODO add getters to access private fields -} + + + public String getName() { + return name; + } + + public Profile getProfile() { + return profile; + } + + public String getColor() { + return color; + } + + public String getId() { + return id; + } + + public boolean isAdmin() { + return isAdmin; + } + + public boolean isDeleted() { + return deleted; + } + + public boolean isHas2fa() { + return has2fa; + } + + public boolean isHasFiles() { + return hasFiles; + } + + public boolean isOwner() { + return isOwner; + } + + public boolean isPrimaryOwner() { + return isPrimaryOwner; + } + + public boolean isRestricted() { + return isRestricted; + } + + public boolean isTwoFactorType() { + return twoFactorType; + } + + public boolean isUltraRestricted() { + return isUltraRestricted; + } +} \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/network/Slack.java b/src/nyc/c4q/ramonaharrison/network/Slack.java index 87521bd..adec4bc 100644 --- a/src/nyc/c4q/ramonaharrison/network/Slack.java +++ b/src/nyc/c4q/ramonaharrison/network/Slack.java @@ -97,7 +97,23 @@ public static SendMessageResponse sendMessage(String messageText) { 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!"); + + attachments.add(messageTex + + message(attachments); + + try { + messageText = URLEncoder.encode(messageText, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + + URL sendMessageUrl = HTTPS.stringToURL(BASE_URL + ENDPOINT_POST_MESSAGE + "?token=" + API_KEY + "&channel=" + BOTS_CHANNEL_ID + "&text=" + messageText); + + return new SendMessageResponse(HTTPS.get(sendMessageUrl)); + + + } /** From 87303b5bec1a443cf7ce33e62c7e7457daf58d2b Mon Sep 17 00:00:00 2001 From: Francisco Andrade Date: Tue, 17 Oct 2017 19:37:10 -0400 Subject: [PATCH 2/2] git update --- src/nyc/c4q/ramonaharrison/network/Slack.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nyc/c4q/ramonaharrison/network/Slack.java b/src/nyc/c4q/ramonaharrison/network/Slack.java index adec4bc..328c07b 100644 --- a/src/nyc/c4q/ramonaharrison/network/Slack.java +++ b/src/nyc/c4q/ramonaharrison/network/Slack.java @@ -21,7 +21,7 @@ public class Slack { private static final String API_KEY = Token.findApiToken(); - private static final String BASE_URL = "https://slack.com/api/"; + private static final String BASE_URL = "https://c4q-accesscode-4-4.slack.com/services/B7JFZS2E7"; 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";