DEV Community

Cover image for telescope-bot: A new user has joined the Telescope community
TD
TD

Posted on

telescope-bot: A new user has joined the Telescope community

My final contribution to an open-source project this year was enabling the existing Slack Bot to announce signups when a new user successfully creates a Telescope account.

As recommended by the project maintainer, I used Slack Bolt for Javascript to send a notification whenever there's new signup. I had to familiarize myself with the Slack Bolt API to use its client functionality to send messages.

Writing The Code

I had to revise my code several times to make log messages clear and make code optional, as we do not want the signup process to be halted in the use-case where we cannot send a Slack signup notification.

const { logger } = require('@senecacdot/satellite');
const { App } = require('@slack/bolt');

const { SLACK_BOT_CHANNEL_ID } = process.env;

const createSlackApp = () => {
  const { SLACK_BOT_TOKEN, SLACK_BOT_SIGNING_SECRET } = process.env;
  if (!(SLACK_BOT_TOKEN && SLACK_BOT_SIGNING_SECRET && SLACK_BOT_CHANNEL_ID)) {
    logger.info('Skipping Slack Bot integration, not configured');
    return false;
  }

  const app = new App({
    token: SLACK_BOT_TOKEN.trim(),
    signingSecret: SLACK_BOT_SIGNING_SECRET.trim(),
  });

  return app;
};

const constructMessageBlocks = (displayName, githubUsername, blogUrl) => {
  const messageBlocks = [
    {
      type: 'header',
      text: {
        type: 'plain_text',
        emoji: true,
        text: 'New Member Notification :tada:',
      },
    },
    {
      type: 'divider',
    },
    {
      type: 'context',
      elements: [
        {
          type: 'image',
          image_url: `https://github.com/${githubUsername}.png?size=64`,
          alt_text: 'avatar',
        },
        {
          type: 'mrkdwn',
          text: `*${displayName}* has joined the Telescope community. Be the first one to welcome them.`,
        },
        {
          type: 'mrkdwn',
          text: `<https://www.github.com/${githubUsername} | *Visit Github Profile*>`,
        },
        {
          type: 'mrkdwn',
          text: `<${blogUrl} | *Visit Blog*>`,
        },
      ],
    },
  ];
  return messageBlocks;
};

const publishSignUpMessage = async (displayName, githubUsername, blogUrl) => {
  if (!(displayName && githubUsername && blogUrl))
    throw new Error(
      'displayName, gitHubUsername, and blogUrl are required to send sign up announcement'
    );
  const app = createSlackApp();
  if (app) {
    try {
      const messageBlocks = constructMessageBlocks(displayName, githubUsername, blogUrl);

      const result = await app.client.chat.postMessage({
        channel: SLACK_BOT_CHANNEL_ID.trim(),
        text: `${displayName} has joined the Telescope community. Be the first one to welcome them.`,
        blocks: messageBlocks,
        unfurl_links: false,
      });
      logger.debug({ result }, 'Slack Bot message delivery successful');
    } catch (error) {
      logger.warn({ error }, 'Slack Bot message delivery unsuccessful');
    }
  }
};

module.exports = publishSignUpMessage;

Enter fullscreen mode Exit fullscreen mode

Problems Encountered

Since I have no background in working with projects using Docker, I was habitually looking for logging messages outputted to the terminal used to start the services. Using Docker Desktop client, I had to click on the container I was working with, the SSO container, to see the log messages.
Another problem was that even though I added the environment variables to the env files, I did not update the docker-compose, so I had to update it as follows:

sso:
    init: true
    container_name: 'sso'
    environment:
      # Slack Bolt config
      - SLACK_BOT_TOKEN
      - SLACK_BOT_SIGNING_SECRET
      - SLACK_BOT_CHANNEL_ID
Enter fullscreen mode Exit fullscreen mode

Now, when I start the project and inspect the SSO container using the Docker client, I see the variables required to configure the Slack Bolt app:

sso container inspect screenshot

Outcome

When a new user goes through the signup process, the telescope-bot should send an announcement notification in the #telescope-channel so the community members can welcome the new user.

sign up notification sample

Final Thoughts

Making the Slack bot announce signups issue was fun to work on, despite the technical difficulties with the e2e tests, when I got close to getting my PR merged.
One cannot always rely on technology, as occasional downtime is inevitable.

Top comments (0)