diff --git a/src/nyc/c4q/ramonaharrison/Bot.java b/src/nyc/c4q/ramonaharrison/Bot.java index 6a52dd4..1589428 100644 --- a/src/nyc/c4q/ramonaharrison/Bot.java +++ b/src/nyc/c4q/ramonaharrison/Bot.java @@ -1,5 +1,6 @@ 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.*; @@ -14,7 +15,40 @@ */ public class Bot { - // TODO: implement your bot logic! + + public static int money = 0; + public void checkMessagesAndRespond(String channelId) { + ListMessagesResponse listMessagesResponse = Slack.listMessages(channelId); + + if (listMessagesResponse.isOk()) { + List messages = listMessagesResponse.getMessages(); + for (Message message : messages) { + if (message.getText().contains("hamburger")) { + Slack.sendMessage("I love hamburgers"); + Slack.sendMessage("https://media.giphy.com/media/yYc5L3vhTWeBi/giphy.gif"); + break; + }else if(message.getText().contains("cat")){ + Slack.sendMessage("Cats are meh"); + Slack.sendMessage("https://media.giphy.com/media/MVQLSJt38RKJG/giphy.gif"); + break; + }else if (message.getText().contains("alien")){ + Slack.sendMessage("Aliens are scary"); + Slack.sendMessage("https://media.giphy.com/media/l3V0ma60jQqGCoJyM/giphy.gif"); + break; + }else if (message.getText().contains("ufo")){ + Slack.sendMessage("OMG UFOS"); + Slack.sendMessage("https://media.giphy.com/media/dKVvUk2oH8x2g/giphy.gif"); + break; + }else { + Slack.sendMessage("life is hard could you lend me some money?"); + break; + } + + + } + + } + } public Bot() { @@ -69,6 +103,31 @@ public void listMessages(String channelId) { } } + public String lastMessageText() { + ListMessagesResponse listMessagesResponse = Slack.listMessages(Slack.BOTS_CHANNEL_ID); + + if (listMessagesResponse.isOk()) { + List messages = listMessagesResponse.getMessages(); + + Message message = messages.get(0); + + return message.getText(); + } else { + System.err.print("Error listing messages: " + listMessagesResponse.getError()); + } + return ""; + } + + public Message lastMessage() { + ListMessagesResponse lmr = Slack.listMessages(Slack.BOTS_CHANNEL_ID); + if (lmr.isOk()) { + List messages = lmr.getMessages(); + return messages.get(0); + } else { + System.err.print("Error listing messages: " + lmr.getError()); + } + return null; + } /** * Sample method: sends a plain text message to the #bots channel. Prints a message indicating success or failure. * @@ -84,6 +143,27 @@ public void sendMessageToBotsChannel(String text) { } } + + public void sendMessageToBotsChannelAsPolice(String text) { + SendMessageResponse sendMessageResponse = Slack.sendMessageAsPolice(text); + + if (sendMessageResponse.isOk()) { + System.out.println("Message sent successfully!"); + } else { + System.err.print("Error sending message: " + sendMessageResponse.getError()); + } + } + + public void sendMessageWithAttachmentsToBotsChannel(String text, List attachments) { + SendMessageResponse sendMessageResponse = Slack.sendMessageWithAttachments(text,attachments); + + if (sendMessageResponse.isOk()) { + System.out.println("Message sent successfully!"); + } else { + System.err.print("Error sending message: " + sendMessageResponse.getError()); + } + } + /** * Sample method: deletes a message from the #bots channel. Prints a message indicating success or failure. * diff --git a/src/nyc/c4q/ramonaharrison/Main.java b/src/nyc/c4q/ramonaharrison/Main.java index 933e92b..f6927a8 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -1,24 +1,84 @@ package nyc.c4q.ramonaharrison; +import nyc.c4q.ramonaharrison.model.Attachment; +import nyc.c4q.ramonaharrison.model.Message; +import nyc.c4q.ramonaharrison.model.User; import nyc.c4q.ramonaharrison.network.Slack; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import java.util.ArrayList; +import java.util.List; public class Main { - public static void main(String[] args) { + public static void main(String[] args) throws InterruptedException { Bot myBot = new Bot(); - myBot.testApi(); + /* + List listOfAttachments = new ArrayList<>(); + Attachment attachment = new Attachment("Required plain-text summary of the attachment.","%230099ff","Do%20you%20haz%20MONEY","C4Q%20Stuff","http://flickr.com/bobby/","http://flickr.com/icons/bobby.jpg","Not%20C4Q%20API%20Documentation","https://api.slack.com/","I%20Love%20Money","http://my-website.com/path/to/image.jpg","http://example.com/path/to/thumb.png","Not%20C4Q%20API","https://platform.slack-edge.com/img/default_application_icon.png"); + Attachment attachment2 = new Attachment("Required plain-text summary of the attachment.","%23ff9900","Another%20attachment","More%20C4Q%20Stuff","http://flickr.com/bobby/","http://flickr.com/icons/bobby.jpg","Not%20C4Q%20API%20Documentation","https://api.slack.com/","I%20Love%20More%20Mulah","http://my-website.com/path/to/image.jpg","http://example.com/path/to/thumb.png","Not%20C4Q%20API","https://platform.slack-edge.com/img/default_application_icon.png"); + listOfAttachments.add(attachment); + listOfAttachments.add(attachment2); + myBot.sendMessageWithAttachmentsToBotsChannel("I'm%20BeggarBot",listOfAttachments); + */ + +// Slack.sendMessageWithButtons(); + + while(true) { + Message message = myBot.lastMessage(); + String user = message.getUser(); + String lastMessage = myBot.lastMessageText().toLowerCase(); + if(lastMessage.contains("poor")){ + myBot.sendMessageToBotsChannel("*POOR??* Did you hear that <@U23CPKGCV>?"); // Third feature -- tag someone in group + myBot.sendMessageToBotsChannel("..."); + myBot.sendMessageToBotsChannel("..."); + myBot.sendMessageToBotsChannel("Ugh Here's a dollar, Bye"); + Bot.money--; + myBot.sendMessageToBotsChannel("_beggarbot escapes with " + Bot.money + " dollars!_"); + Thread.sleep(6000); + myBot.sendMessageToBotsChannelAsPolice("WHERE DID HE GO! :police_car:"); + break; + } + if(!user.equals("beggarbot")) + stringCheck(lastMessage); + } + } + - myBot.listChannels(); + public static void stringCheck(String lastMessage){ + Bot myBot = new Bot(); + if(lastMessage.contains("Trump".toLowerCase())){ + myBot.sendMessageToBotsChannel("Trump? How about giving me a dollar and make Beggarbot rich again!"); + Bot.money++; + } - myBot.listMessages(Slack.BOTS_CHANNEL_ID); + else if(lastMessage.contains("Happy".toLowerCase())){ + myBot.sendMessageToBotsChannel("Feeling happy today? How about spreading the good vibes and give me a dollar?"); + Bot.money++; + } - // Post "Hello, world!" to the #bots channel - //myBot.sendMessage("Hello, world!"); + else if(lastMessage.contains("Money".toLowerCase())){ + myBot.sendMessageToBotsChannel("I could use some money, give me a dollar!"); + Bot.money++; + } - // Post a pineapple photo to the #bots channel - //myBot.sendMessage("http://weknowyourdreams.com/images/pineapple/pineapple-07.jpg"); + else if(lastMessage.contains("Sad".toLowerCase())){ + myBot.sendMessageToBotsChannel("I’m feeling down too. Giving me a dollar would make me feel better."); + Bot.money++; + } + else if(lastMessage.contains("Clinton".toLowerCase())){ + myBot.sendMessageToBotsChannel("I know where Hillary’s secret emails are. Give me a dollar and I’ll tell you where."); + Bot.money++; + } + else if(lastMessage.equalsIgnoreCase("sure")||lastMessage.equalsIgnoreCase("fine")||lastMessage.equalsIgnoreCase("okay")){ + myBot.sendMessageToBotsChannel("SURE?? You agree to give me a dollar!"); + Bot.money++; + } + else + myBot.sendMessageToBotsChannel("Give me a dollar, cmon"); } } \ 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..7f88a73 100644 --- a/src/nyc/c4q/ramonaharrison/model/Attachment.java +++ b/src/nyc/c4q/ramonaharrison/model/Attachment.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,27 +17,233 @@ 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 imageUrl; + private String thumb_url; + private String footer; + private String footer_icon; + private long ts; + + public Attachment() { + } + + public Attachment(String fallback, String color, String pretext, String author_name, String author_link, String author_icon, String title, String title_link, String text, String imageUrl, String thumb_url, String footer, String footer_icon) { + this.fallback = fallback; + this.color = color; + this.pretext = pretext; + this.author_name = author_name; + this.author_link = author_link; + this.author_icon = author_icon; + this.title = title; + this.title_link = title_link; + this.text = text; + this.imageUrl = imageUrl; + this.thumb_url = thumb_url; + this.footer = footer; + this.footer_icon = footer_icon; + } public Attachment(JSONObject json) { - // TODO: parse an attachment from the incoming json + + if (json.containsKey("fallback")) { + this.fallback = (String) json.get("fallback"); + } + + 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("fields")) { + JSONArray jsonFields = (JSONArray) json.get("fields"); + this.fields = new ArrayList(); + for (int i = 0; i < jsonFields.size(); i++) { + Field field = new Field((JSONObject) jsonFields.get(i)); + this.fields.add(field); + } + } + + if (json.containsKey("imageUrl")) { + this.imageUrl = (String) json.get("imageUrl"); + } + + 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"); + } + + if(json.containsKey("ts")) { + this.ts = (Long) 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 List getFields() { + + + + return fields; + } + + public String getImageUrl() { + return imageUrl; + } + + public String getThumb_url() { + return thumb_url; + } + + public String getFooter() { + return footer; + } + + public String getFooter_icon() { + return footer_icon; + } + + public long getTs() { + + return ts; + } + + + public void setFallback(String fallback) { + this.fallback = fallback; + } + + public void setColor(String color) { + this.color = color; + } + + public void setPretext(String pretext) { + this.pretext = pretext; + } + + public void setAuthor_name(String author_name) { + this.author_name = author_name; + } + + public void setAuthor_link(String author_link) { + this.author_link = author_link; + } + + public void setAuthor_icon(String author_icon) { + this.author_icon = author_icon; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setTitle_link(String title_link) { + this.title_link = title_link; + } + + public void setText(String text) { + this.text = text; + } + + public void setFields(List fields) { + this.fields = fields; + } + + public void setImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + } + + public void setThumb_url(String thumb_url) { + this.thumb_url = thumb_url; + } + + public void setFooter(String footer) { + this.footer = footer; + } + + public void setFooter_icon(String footer_icon) { + this.footer_icon = footer_icon; + } + + public void setTs(long ts) { + this.ts = 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..cf87a79 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Field.java @@ -0,0 +1,42 @@ +package nyc.c4q.ramonaharrison.model; + +import org.json.simple.JSONObject; + +/** + * Created by c4q on 9/11/16. + */ +public class Field { + private String title; + private String value; + private boolean isShort; + + + + 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/Message.java b/src/nyc/c4q/ramonaharrison/model/Message.java index 8ea24bd..10721db 100644 --- a/src/nyc/c4q/ramonaharrison/model/Message.java +++ b/src/nyc/c4q/ramonaharrison/model/Message.java @@ -48,6 +48,10 @@ public Message(JSONObject json) { this.user = (String) json.get("user"); } + if (json.get("username") != null) { + this.user = (String) json.get("username"); + } + if (json.get("attachments") != null) { JSONArray attachmentsArray = (JSONArray) json.get("attachments"); this.attachments = new ArrayList(); diff --git a/src/nyc/c4q/ramonaharrison/model/Profile.java b/src/nyc/c4q/ramonaharrison/model/Profile.java new file mode 100644 index 0000000..024214e --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Profile.java @@ -0,0 +1,113 @@ +package nyc.c4q.ramonaharrison.model; + +import org.json.simple.JSONObject; + +/** + * Created by c4q on 9/11/16. + */ +public class Profile { + + private String first_name; + private String last_name; + private String email; + private String skype; + private String phone; + private String image_24; + private String image_32; + private String image_48; + private String image_72; + private String image_192; + private String image_512; + + 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("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 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..b48f666 100644 --- a/src/nyc/c4q/ramonaharrison/model/User.java +++ b/src/nyc/c4q/ramonaharrison/model/User.java @@ -13,24 +13,128 @@ 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 Profile 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("deleted")) { + this.deleted = (boolean) json.get("deleted"); + } + + if (json.containsKey("color")) { + this.color = (String) json.get("color"); + } + + if (json.containsKey("profile")) { + this.profile = (Profile) json.get("profile"); + } + + 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("two_factor_type")) { + this.two_factor_type = (String) json.get("two_factor_type"); + } + + if (json.containsKey("has_files")) { + this.has_files = (boolean) json.get("has_files"); + } + + } + + public String getId() { + return id; + } + + public String getName() { + return name; } - // TODO add getters to access private fields + public boolean isDeleted() { + return deleted; + } + + public String getColor() { + return color; + } + + public Profile 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; + } + + 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..3d7f0fe 100644 --- a/src/nyc/c4q/ramonaharrison/network/Slack.java +++ b/src/nyc/c4q/ramonaharrison/network/Slack.java @@ -1,5 +1,6 @@ package nyc.c4q.ramonaharrison.network; +import nyc.c4q.ramonaharrison.Bot; import nyc.c4q.ramonaharrison.model.Attachment; import nyc.c4q.ramonaharrison.network.response.*; import nyc.c4q.ramonaharrison.util.Token; @@ -20,14 +21,17 @@ public class Slack { + private static final String USERNAME = "&username=beggarbot"; 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"; - private static final String ENDPOINT_LIST_CHANNELS = "channels.list"; + private static final String ENDPOINT_LIST_CHANNELS = "groups.list"; private static final String ENDPOINT_LIST_MESSAGES = "channels.history"; private static final String ENDPOINT_POST_MESSAGE = "chat.postMessage"; private static final String ENDPOINT_DELETE_MESSAGE = "chat.delete"; + private static final String ENDPOINT_URL_IMAGE = "&icon_url=http://orig02.deviantart.net/9689/f/2012/027/9/c/mr_bender______classy__by_sgtconker1r-d4nqpzu.png"; +// public static final String BOTS_CHANNEL_ID = "G2FBLTCGK"; public static final String BOTS_CHANNEL_ID = "C2ABKERFT"; /** @@ -82,7 +86,22 @@ 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 + ENDPOINT_URL_IMAGE + "&text=" + messageText + USERNAME); + + return new SendMessageResponse(HTTPS.get(sendMessageUrl)); + } + + public static SendMessageResponse sendMessageAsPolice(String messageText) { + + 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 + "&icon_url=http://images.clipartpanda.com/cop-clipart-POLICE-CARTOON-HOLDING-UP-ONE-HAND.jpg" + "&text=" + messageText + "&username=angrycop"); return new SendMessageResponse(HTTPS.get(sendMessageUrl)); } @@ -94,12 +113,33 @@ public static SendMessageResponse sendMessage(String messageText) { * @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) { + // first new feature + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("["); + for (Attachment attachment: attachments) { + if(attachment.equals(attachments.get(attachments.size()-1))){ + stringBuilder.append("{\"color\":%20\"%23" + attachment.getColor() + "\",%20\"pretext\":\"" + attachment.getPretext() + "\",\"author_name\":\"" + attachment.getAuthor_name() + "\",\"author_link\":\"" + attachment.getAuthor_link() + "\",\"author_icon\":\"" + attachment.getAuthor_icon() + "\",\"title\":\"" + attachment.getTitle() + "\",\"title_link\":\"" + attachment.getTitle_link() + "\",\"text\":\"" + attachment.getText() + "\",\"fields\":[{\"title\":\"Priority\",\"value\":\"High\",\"short\":false}],\"image_url\":\"" + attachment.getImageUrl() + "\",\"thumb_url\":\"" + attachment.getThumb_url() + "\",\"footer\":\"" + attachment.getFooter() + "\",\"footer_icon\":\"" + attachment.getFooter_icon() + "\",\"ts\":" + currentTs() + "}]"); + }else{ + + stringBuilder.append("{\"color\":%20\"%23" + attachment.getColor() + "\",%20\"pretext\":\"" + attachment.getPretext() + "\",\"author_name\":\"" + attachment.getAuthor_name() + "\",\"author_link\":\"" + attachment.getAuthor_link() + "\",\"author_icon\":\"" + attachment.getAuthor_icon() + "\",\"title\":\"" + attachment.getTitle() + "\",\"title_link\":\"" + attachment.getTitle_link() + "\",\"text\":\"" + attachment.getText() + "\",\"fields\":[{\"title\":\"Priority\",\"value\":\"High\",\"short\":false}],\"image_url\":\"" + attachment.getImageUrl() + "\",\"thumb_url\":\"" + attachment.getThumb_url() + "\",\"footer\":\"" + attachment.getFooter() + "\",\"footer_icon\":\"" + attachment.getFooter_icon() + "\",\"ts\":" + currentTs() + "},"); + } + } + + URL sendMessageUrl = HTTPS.stringToURL(BASE_URL + ENDPOINT_POST_MESSAGE + "?token=" + API_KEY + "&channel=" + BOTS_CHANNEL_ID + ENDPOINT_URL_IMAGE + "&text=" + messageText + "&attachments=" + stringBuilder.toString()); + 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!"); } + //Second new feature + public static SendMessageResponse sendMessageWithButtons() { + URL sendMessageUrl = HTTPS.stringToURL(BASE_URL + ENDPOINT_POST_MESSAGE + "?token=" + API_KEY + "&channel=" + BOTS_CHANNEL_ID + ENDPOINT_URL_IMAGE + "&text=sup%20homie?" + "&attachments=" + "[{\"text\":\"Choose%20a%20game%20to%20play\",\"fallback\":\"You%20are%20unable%20to%20choose%20a%20game\",\"callback_id\":\"wopr_game\",\"color\":\"%233AA3E3\",\"attachment_type\":\"default\",\"actions\":[{\"name\":\"chess\",\"text\":\"Chess\",\"type\":\"button\",\"value\":\"chess\"},{\"name\":\"maze\",\"text\":\"Falken's%20Maze\",\"type\":\"button\",\"value\":\"maze\"},{\"name\":\"war\",\"text\":\"Thermonuclear%20War\",\"style\":\"danger\",\"type\":\"button\",\"value\":\"war\",\"confirm\":{\"title\":\"Would%20you%20like%20to%20click%20a%20button?\",\"text\":\"Click%20one%20of%20a%20good%20game%20of%20chess?\",\"ok_text\":\"Yes\",\"dismiss_text\":\"No\"}}]}]"); + return new SendMessageResponse(HTTPS.get(sendMessageUrl)); + } + + /** * Static method to delete an existing message from the #bots channel. * @@ -111,4 +151,9 @@ public static DeleteMessageResponse deleteMessage(String messageTs) { return new DeleteMessageResponse(HTTPS.get(deleteMessageUrl)); } + + public static String currentTs() { + Long timestamp = System.currentTimeMillis() / 1000L; + return timestamp.toString(); + } }