@Override public SlackPersona.SlackPresence getPresence(SlackPersona persona) { HttpClient client = getHttpClient(); HttpPost request = new HttpPost("https://slack.com/api/users.getPresence"); List<NameValuePair> nameValuePairList = new ArrayList<>(); nameValuePairList.add(new BasicNameValuePair("token", authToken)); nameValuePairList.add(new BasicNameValuePair("user", persona.getId())); try { request.setEntity(new UrlEncodedFormEntity(nameValuePairList, "UTF-8")); HttpResponse response = client.execute(request); String jsonResponse = CharStreams.toString(new InputStreamReader(response.getEntity().getContent())); LOGGER.debug("PostMessage return: " + jsonResponse); JSONObject resultObject = parseObject(jsonResponse); SlackReplyImpl reply = SlackJSONReplyParser.decode(resultObject); if (!reply.isOk()) { return SlackPersona.SlackPresence.UNKNOWN; } String presence = (String) resultObject.get("presence"); if ("active".equals(presence)) { return SlackPersona.SlackPresence.ACTIVE; } if ("away".equals(presence)) { return SlackPersona.SlackPresence.AWAY; } } catch (Exception e) { // TODO : improve exception handling e.printStackTrace(); } return SlackPersona.SlackPresence.UNKNOWN; }
@Override public SlackMessageHandle sendMessageOverWebSocket( SlackChannel channel, String message, SlackAttachment attachment) { SlackMessageHandleImpl handle = new SlackMessageHandleImpl(getNextMessageId()); try { JSONObject messageJSON = new JSONObject(); messageJSON.put("type", "message"); messageJSON.put("channel", channel.getId()); messageJSON.put("text", message); if (attachment != null) { messageJSON.put("attachments", SlackJSONAttachmentFormatter.encodeAttachments(attachment)); } websocketSession.getBasicRemote().sendText(messageJSON.toJSONString()); } catch (Exception e) { // TODO : improve exception handling e.printStackTrace(); } return handle; }
private void postSlackCommand( Map<String, String> params, String command, SlackMessageHandleImpl handle) { HttpClient client = getHttpClient(); HttpPost request = new HttpPost(SLACK_API_HTTPS_ROOT + command); List<NameValuePair> nameValuePairList = new ArrayList<>(); for (Map.Entry<String, String> arg : params.entrySet()) { nameValuePairList.add(new BasicNameValuePair(arg.getKey(), arg.getValue())); } try { request.setEntity(new UrlEncodedFormEntity(nameValuePairList, "UTF-8")); HttpResponse response = client.execute(request); String jsonResponse = CharStreams.toString(new InputStreamReader(response.getEntity().getContent())); LOGGER.debug("PostMessage return: " + jsonResponse); SlackReplyImpl reply = SlackJSONReplyParser.decode(parseObject(jsonResponse)); handle.setSlackReply(reply); } catch (Exception e) { // TODO : improve exception handling e.printStackTrace(); } }