Exemple #1
0
  @RequestMapping(
      value = "/api/broadcast",
      method = RequestMethod.POST,
      produces = "application/json")
  public List<BroadcastGroup> broadcastMessage(
      @RequestParam("name") String name,
      @RequestParam("set") Integer setId,
      @RequestParam("message") String message) {
    BroadcastGroup group = broadcastRepository.findOne(name);
    if (group != null) {
      Optional<BroadcastSet> setOptional =
          group.getBroadcastSets().stream().filter(set -> set.getId() == setId).findFirst();
      if (setOptional.isPresent()) {
        BroadcastSet set = setOptional.get();
        set.getMessages().add(new BroadcastMessage(message, new Date()));
        broadcastRepository.save(group);
        executorService.execute(
            () -> {
              set.getUsers()
                  .forEach(
                      userId -> {
                        SlackUser user = session.findUserById(userId);
                        session.sendMessageToUser(user, message, null);
                      });
            });
      } else {
        log.error("Can't find set with id {}", setId);
      }

    } else {
      log.error("Can't find group with name {}", name);
    }
    return broadcastRepository.findAll();
  }
 private static SlackChannelUnarchived extractChannelUnarchiveEvent(
     SlackSession slackSession, JSONObject obj) {
   String channelId = (String) obj.get("channel");
   String userId = (String) obj.get("user");
   return new SlackChannelUnarchivedImpl(
       slackSession.findChannelById(channelId), slackSession.findUserById(userId));
 }
 @Override
 public void run() {
   try {
     if (!gerritChangeInfoService.projectExists(projectId)) {
       session.sendMessage(
           channelToSubscribe,
           "Could not find project name *`"
               + projectId
               + "`*, check that this project name is valid and that it is active",
           null,
           SlackChatConfiguration.getConfiguration().asUser());
       return;
     }
     subscriptionService.unsubscribeOnProject(projectId, channelToSubscribe.getId());
     session.sendMessage(
         channelToSubscribe,
         "This channel will not publish any more review requests from project *`"
             + projectId
             + "`*",
         null,
         SlackChatConfiguration.getConfiguration().asUser());
   } catch (IOException e) {
     session.sendMessage(
         channelToSubscribe,
         "Too bad, an unexpected error occurred...",
         null,
         SlackChatConfiguration.getConfiguration().asUser());
     e.printStackTrace();
   }
 }
 private static SlackMessagePostedImpl parseMessagePublished(
     JSONObject obj, SlackChannel channel, String ts, SlackSession slackSession) {
   String text = (String) obj.get("text");
   String userId = (String) obj.get("user");
   String botId = (String) obj.get("bot_id");
   SlackUser user = userId != null ? slackSession.findUserById(userId) : null;
   SlackBot bot = botId != null ? slackSession.findBotById(botId) : null;
   return new SlackMessagePostedImpl(text, bot, user, channel, ts);
 }
  public SlackConnector(String slackAuthToken) throws IOException {
    log.info("opening slack connection...");
    slackConnection = SlackSessionFactory.createWebSocketSlackSession(slackAuthToken);

    slackConnection.connect();
    log.info("slack connection opened");

    slackConnection.addMessagePostedListener(this::onMessagePosted);

    messageListeners = new ArrayList<>();
  }
Exemple #6
0
 @RequestMapping(value = "/api/group", method = RequestMethod.POST, produces = "application/json")
 public List<BroadcastGroup> createGroup(
     @RequestParam("name") String name, @RequestParam("setSize") Integer setSize) {
   if (broadcastRepository.findOne(name) == null) {
     AtomicInteger counter = new AtomicInteger(0);
     List<String> userIds =
         session
             .getUsers()
             .stream()
             .filter(slackUser -> !slackUser.isBot())
             .map(SlackPersona::getId)
             .collect(Collectors.toList());
     List<BroadcastSet> broadcastSets =
         Lists.partition(userIds, setSize)
             .stream()
             .map(
                 set ->
                     new BroadcastSet(
                         counter.incrementAndGet(), set, new ArrayList<BroadcastMessage>()))
             .collect(Collectors.toList());
     broadcastRepository.save(new BroadcastGroup(name, broadcastSets, new Date()));
   } else {
     log.error("Group with name {} already exist", name);
   }
   return broadcastRepository.findAll();
 }
Exemple #7
0
 @RequestMapping(
     value = "/api/community",
     method = RequestMethod.GET,
     produces = "application/json")
 public Map<String, Integer> getCommunitySize() {
   return ImmutableMap.of("size", session.getUsers().size());
 }
 private static SlackChannelCreated extractChannelCreatedEvent(
     SlackSession slackSession, JSONObject obj) {
   JSONObject channelJSONObject = (JSONObject) obj.get("channel");
   SlackChannel channel = parseChannelDescription(channelJSONObject);
   String creatorId = (String) channelJSONObject.get("creator");
   SlackUser user = slackSession.findUserById(creatorId);
   return new SlackChannelCreatedImpl(channel, user);
 }
 private static SlackChannel getChannel(SlackSession slackSession, String channelId) {
   if (channelId != null) {
     if (channelId.startsWith("D")) {
       // direct messaging, on the fly channel creation
       return new SlackChannelImpl(channelId, channelId, "", "", true);
     } else {
       return slackSession.findChannelById(channelId);
     }
   }
   return null;
 }
 @RequestMapping(
     value = "/slack/availability",
     method = RequestMethod.GET,
     produces = MediaType.TEXT_PLAIN_VALUE)
 @ResponseBody
 public String getAvailability(@Valid SlashSlackMessage message, Model model) {
   log.debug("Incoming Slack request: {}", message.toString());
   if (!message.getToken().equals(slackProperties.getSlashCommandToken())) {
     throw new AccessDeniedException(
         "Token in request " + message.getToken() + " did not match configured token.");
   }
   return slackMessageService.respondToMessage(
       Optional.of(slackSession)
           .map(slackSession -> slackSession.findUserById(message.getUserId())),
       message.getText());
 }
  private void onMessagePosted(SlackMessagePosted event, SlackSession session) {
    log.info("message received");
    String prefix = "<@" + session.sessionPersona().getId() + ">";
    String message = event.getMessageContent().trim();
    if (!message.startsWith(prefix)) {
      return;
    } else {
      message = message.substring(prefix.length()).trim();
    }

    for (SantraConnector.MessageListener listener : messageListeners) {
      listener.onMessage(
          event.getSender().getUserName(),
          message,
          new SlackMessageSession(session, event.getChannel()));
    }
  }
 private static SlackChannelDeleted extractChannelDeletedEvent(
     SlackSession slackSession, JSONObject obj) {
   String channelId = (String) obj.get("channel");
   return new SlackChannelDeletedImpl(slackSession.findChannelById(channelId));
 }
 private static SlackChannelRenamed extractChannelRenamedEvent(
     SlackSession slackSession, JSONObject obj) {
   String channelId = (String) obj.get("channel");
   String newName = (String) obj.get("name");
   return new SlackChannelRenamedImpl(slackSession.findChannelById(channelId), newName);
 }
 @Override
 public void close() throws Exception {
   slackConnection.disconnect();
 }
 @Override
 public void sendMessage(String message) {
   slackSession.sendMessage(slackChannel, message, null);
 }