code: private void getImChannels(String slackToken, int limit, String nextCursor) {
          try {
              Event event = restTemplate.getForEntity(slackApiEndpoints.getImListApi(), Event.class,
                      slackToken, limit, nextCursor).getBody();
              imChannelIds.addAll(Arrays.stream(event.getIms()).map(Channel::getId).collect(Collectors.toList()));
              if (event.getResponseMetadata() != null &&
                      !StringUtils.isEmpty(event.getResponseMetadata().getNextCursor())) {
                  Thread.sleep(5000L); // sleep because its a tier 2 api which allows only 20 calls per minute
                  getImChannels(slackToken, limit, event.getResponseMetadata().getNextCursor());
              }
          } catch (Exception e) {
              logger.error("Error fetching im channels for the bot: ", e);
          }
      }
docs: Fetch all im channels to determine direct message to the bot.
  
  @param slackToken slack token which you get from slack for the integration you create
  @param limit number of channels to fetch in one call
  @param nextCursor cursor for the next call