From 76b66993926a2b7f51015f3ab0a39c86355fbb53 Mon Sep 17 00:00:00 2001 From: Roy Curtis Date: Fri, 8 Apr 2016 19:43:49 +0100 Subject: [PATCH] * Added option to log Slack response messages * Added ticket number to Slack messages --- .../ticketmaster/commands/CommandNew.java | 3 ++- .../ticketmaster/integrations/Slack.java | 22 ++++++++++--------- .../ticketmaster/utils/ConfigLoader.java | 10 +++++++++ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main/java/me/rafaskb/ticketmaster/commands/CommandNew.java b/src/main/java/me/rafaskb/ticketmaster/commands/CommandNew.java index 8a49b16..229161d 100644 --- a/src/main/java/me/rafaskb/ticketmaster/commands/CommandNew.java +++ b/src/main/java/me/rafaskb/ticketmaster/commands/CommandNew.java @@ -69,7 +69,8 @@ protected void run(CommandSender sender, String[] args) { ticket.getSubmitter(), ticket.getTicketLocation().getWorldName(), ticket.getTicketLocation().getX(), - ticket.getTicketLocation().getZ() + ticket.getTicketLocation().getZ(), + ticket.getId() ); } diff --git a/src/main/java/me/rafaskb/ticketmaster/integrations/Slack.java b/src/main/java/me/rafaskb/ticketmaster/integrations/Slack.java index db5ecf1..175193e 100644 --- a/src/main/java/me/rafaskb/ticketmaster/integrations/Slack.java +++ b/src/main/java/me/rafaskb/ticketmaster/integrations/Slack.java @@ -15,14 +15,14 @@ public class Slack { - public static void sendMessage(String message, String username, String world, double x , double z){ + public static void sendMessage(String message, String username, String world, double x , double z, int id){ URL url; try { url = new URL(ConfigLoader.getSlackwebhookurl()); Map params = new LinkedHashMap<>(); - params.put("payload", generateMessage(message,username,world,x,z) ); + params.put("payload", generateMessage(message,username,world,x,z,id) ); StringBuilder postData = new StringBuilder(); try { @@ -42,13 +42,15 @@ public static void sendMessage(String message, String username, String world, do conn.setDoOutput(true); conn.getOutputStream().write(postDataBytes); - Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); - String response = ""; - for ( int c = in.read(); c != -1; c = in.read() ) { - response +=(char) c; - } + if (ConfigLoader.isSlackDebug()) { + Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); + String response = ""; + for (int c = in.read(); c != -1; c = in.read()) { + response += (char) c; + } - TicketMaster.getInstance().getLogger().info("message from Slack server: "+response); + TicketMaster.getInstance().getLogger().info("message from Slack server: " + response); + } } catch (IOException e) { TicketMaster.getInstance().getLogger().warning("Ticket Master Slack integration IO error:"+e.getMessage()); @@ -60,10 +62,10 @@ public static void sendMessage(String message, String username, String world, do } - private static String generateMessage(String message, String username, String world, double x , double z){ + private static String generateMessage(String message, String username, String world, double x , double z, int id){ String m = "{"; - m+= "\"text\":\""+username+" filed a ticket: "+message+" "+generateDynmapURL(world,x,z)+"\","; + m+= "\"text\":\""+username+" filed ticket #"+id+": "+message+" "+generateDynmapURL(world,x,z)+"\","; m+= "\"username\":\"Ticket Master Bot\","; m+= "\"icon_url\":\"https://minotar.net/avatar/"+username+"/100.png\""; m+= "}"; diff --git a/src/main/java/me/rafaskb/ticketmaster/utils/ConfigLoader.java b/src/main/java/me/rafaskb/ticketmaster/utils/ConfigLoader.java index 67190c5..5ee1747 100644 --- a/src/main/java/me/rafaskb/ticketmaster/utils/ConfigLoader.java +++ b/src/main/java/me/rafaskb/ticketmaster/utils/ConfigLoader.java @@ -15,6 +15,14 @@ public static void setSlackEnable(boolean slackEnable) { ConfigLoader.slackEnable = slackEnable; } + public static boolean isSlackDebug() { + return slackDebug; + } + + public static void setSlackDebug(boolean slackDebug) { + ConfigLoader.slackDebug = slackDebug; + } + public static String getDynmapurl() { return dynmapurl; } @@ -32,6 +40,7 @@ public static void setSlackwebhookurl(String slackwebhookurl) { } private static boolean slackEnable; + private static boolean slackDebug; @@ -44,6 +53,7 @@ public static void reloadConfig(){ setDynmapurl(TicketMaster.getInstance().getConfig().getString("dynmapurl")); setSlackwebhookurl(TicketMaster.getInstance().getConfig().getString("slackwebhookURL")); setSlackEnable(TicketMaster.getInstance().getConfig().getBoolean("enableSlackintergration")); + setSlackDebug(TicketMaster.getInstance().getConfig().getBoolean("enableSlackDebug")); }