Node.js: Slack API

See: https://slack.dev/node-slack-sdk/web-api

const { WebClient, retryPolicies, LogLevel, ErrorCode } = require('@slack/web-api');

const token = process.env.SLACK_BOT_TOKEN;

if (!token) {
  console.error('Missing environment variable: SLACK_BOT_TOKEN');
  process.exit(1);
}

// https://slack.dev/node-slack-sdk/web-api
const slack = new WebClient(token, {
  retryConfig: retryPolicies.fiveRetriesInFiveMinutes,
  logLevel: LogLevel.INFO,
});

async function getChannelInfo(name) {
  let channel;
  const types = 'public_channel, private_channel';
  const exclude_archived = true;
  try {
    for await (const page of slack.paginate('conversations.list', { types, exclude_archived })) {
      channel = page.channels.find(c => c.name === name);
      if (channel) {
        break;
      }
    }
  } catch (e) {
    // Check the code property, and when its a PlatformError, log the whole response.
    if (e.code === ErrorCode.PlatformError) {
      console.error(e.data);
    } else {
      // Some other error, oh no!
      console.error('Well, that was unexpected.');
    }
  }
  return channel;
}

Related: Working with Slack Bot

Leave a Comment

Your email address will not be published. Required fields are marked *